forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategory.cpp
More file actions
163 lines (144 loc) · 3.79 KB
/
category.cpp
File metadata and controls
163 lines (144 loc) · 3.79 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
#include <celutil/gettext.h>
#include <celutil/debug.h>
#include <celengine/astroobj.h>
#include "category.h"
UserCategory::UserCategory(const std::string &n, UserCategory *p, const std::string &domain) :
m_name(n),
m_parent(p)
{
#ifdef ENABLE_NLS
m_i18n = dgettext(m_name.c_str(), domain.c_str());
#endif
}
UserCategory::~UserCategory() { cleanup(); }
void UserCategory::setParent(UserCategory *c)
{
m_parent = c;
}
bool UserCategory::_addObject(Selection s)
{
if (s.empty() || m_objlist.count(s) > 0)
return false;
m_objlist.insert(s);
return true;
}
bool UserCategory::addObject(Selection s)
{
if (s.empty())
return false;
Selection s_ = s.object()->toSelection();
if (!_addObject(s_))
return false;
return s_.object()->_addToCategory(this);
}
bool UserCategory::removeObject(Selection s)
{
if (s.empty() || m_objlist.count(s) == 0)
return false;
if (!_removeObject(s))
return false;
return s.object()->_removeFromCategory(this);
}
bool UserCategory::_removeObject(Selection s)
{
m_objlist.erase(s);
return true;
}
UserCategory *UserCategory::createChild(const std::string &s, const std::string &domain)
{
UserCategory *c = newCategory(s, this, domain);
if (c == nullptr)
return nullptr;
m_catlist.insert(c);
return c;
}
bool UserCategory::deleteChild(UserCategory *c)
{
if (m_catlist.count(c) == 0)
return false;
m_catlist.erase(c);
m_allcats.erase(c->name());
delete c;
return true;
}
bool UserCategory::deleteChild(const std::string &s)
{
UserCategory *c = find(s);
if (c == nullptr)
return false;
return deleteChild(c);
}
bool UserCategory::hasChild(UserCategory *c) const
{
return m_catlist.count(c) > 0;
}
bool UserCategory::hasChild(const std::string &n) const
{
UserCategory *c = find(n);
if (c == nullptr)
return false;
return hasChild(c);
}
void UserCategory::cleanup()
{
DPRINTF(LOG_LEVEL_INFO, "UserCategory::cleanup()\n");
DPRINTF(LOG_LEVEL_INFO, " Objects: %i\n", m_objlist.size());
DPRINTF(LOG_LEVEL_INFO, " Categories: %i\n", m_catlist.size());
while(!m_objlist.empty())
{
auto it = m_objlist.begin();
DPRINTF(LOG_LEVEL_INFO, "Removing object: %s\n", it->getName());
removeObject(*it);
}
while(!m_catlist.empty())
{
auto it = m_catlist.begin();
DPRINTF(LOG_LEVEL_INFO, "Removing category: %s\n", (*it)->name());
deleteChild(*it);
}
}
UserCategory::CategoryMap UserCategory::m_allcats;
UserCategory::CategorySet UserCategory::m_roots;
UserCategory *UserCategory::newCategory(const std::string &s, UserCategory *p, const std::string &domain)
{
if (m_allcats.count(s) > 0)
return nullptr;
UserCategory *c = new UserCategory(s, p, domain);
m_allcats.insert(std::pair<const std::string, UserCategory*>(s, c));
if (p == nullptr)
m_roots.insert(c);
else
p->m_catlist.insert(c);
return c;
}
UserCategory *UserCategory::createRoot(const std::string &n, const std::string &domain)
{
return newCategory(n, nullptr, domain);
}
UserCategory *UserCategory::find(const std::string &s)
{
if (m_allcats.count(s) == 0)
return nullptr;
DPRINTF(LOG_LEVEL_INFO, "UserCategory::find(%s): exists\n", s.c_str());
return m_allcats.find(s)->second;
}
bool UserCategory::deleteCategory(const std::string &n)
{
UserCategory *c = find(n);
if (c == nullptr)
return false;
return deleteCategory(c);
}
bool UserCategory::deleteCategory(UserCategory *c)
{
if (!find(c->name()))
return false;
m_allcats.erase(c->name());
if (c->parent())
c->parent()->m_catlist.erase(c);
else
m_roots.erase(c);
delete c;
return true;
}