-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectory.cpp
More file actions
66 lines (57 loc) · 1.29 KB
/
Directory.cpp
File metadata and controls
66 lines (57 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Directory.h"
namespace fs
{
Directory::Directory(const User* user, const std::string& name, const std::string& parentPath)
{
setName(name);
setOwner(user->getUsername());
setParentPath(parentPath);
setType(SystemObjectType::directory);
}
Directory::Directory(std::istringstream& ifs)
{
ifs >> *this;
}
size_t Directory::getSize() const
{
size_t sum{};
for (const auto& [k, v] : m_children)
{
sum += v->getSize();
}
return sum;
}
const std::unordered_map<std::string, std::unique_ptr<SystemObject>>& Directory::getChildren() const
{
return m_children;
}
SystemObject* Directory::searchChildren(const std::string& key)
{
if (const auto it = m_children.find(key); it != m_children.end())
{
return &*it->second;
}
return nullptr;
}
bool Directory::addChildren(std::unique_ptr<SystemObject> so)
{
if (searchChildren(so->getName()))
{
return false;
}
m_children[so->getName()] = std::move(so);
return true;
}
void Directory::removeChild(const std::string& key)
{
m_children[key]->cascadeDelete();
m_children.erase(key);
}
void Directory::cascadeDelete()
{
for (auto& [k, v] : m_children)
{
v->cascadeDelete();
}
}
} // namespace fs