forked from Mudlet/Mudlet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.h
296 lines (262 loc) · 7.19 KB
/
Tree.h
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#ifndef MUDLET_TREE_H
#define MUDLET_TREE_H
/***************************************************************************
* Copyright (C) 2008-2012 by Heiko Koehn - KoehnHeiko@googlemail.com *
* Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "pre_guard.h"
#include <QString>
#include "post_guard.h"
#include <iostream>
#include <list>
template <class T>
class Tree
{
public:
explicit Tree();
explicit Tree(T* parent);
virtual ~Tree();
T* getParent() const { return mpParent; }
std::list<T*>* getChildrenList() const;
bool hasChildren() const { return (!mpMyChildrenList->empty()); }
int getChildCount() const { return mpMyChildrenList->size(); }
int getID() const { return mID; }
void setID(const int id) { mID = id; }
void addChild(T* newChild, int parentPostion = -1, int parentPosition = -1);
bool popChild(T* removeChild);
void setParent(T* parent);
void enableFamily();
void disableFamily();
bool isActive() const;
bool activate();
void deactivate();
bool setIsActive(bool);
bool shouldBeActive() const;
void setShouldBeActive(bool b);
bool isTemporary() const;
void setTemporary(bool state);
// Returns true if all the ancesters of this node are active. If there are no ancestors it also returns true.
bool ancestorsActive() const;
QString& getError();
void setError(QString);
bool state() const;
QString getPackageName() const { return mPackageName; }
void setPackageName(const QString& n) { mPackageName = n; }
void setModuleName(const QString& n) { mModuleName = n; }
QString getModuleName() const { return mModuleName; }
bool isFolder() { return mFolder; }
void setIsFolder(bool b) { mFolder = b; }
T* mpParent;
std::list<T*>* mpMyChildrenList;
int mID;
QString mPackageName;
QString mModuleName;
protected:
virtual bool canBeActivated() const;
bool mOK_init;
bool mOK_code;
private:
bool mActive;
bool mUserActiveState;
QString mErrorMessage;
bool mTemporary;
bool mFolder;
};
template <class T>
Tree<T>::Tree()
: mpParent( nullptr )
, mpMyChildrenList( new std::list<T *> )
, mID( 0 )
, mOK_init( true )
, mOK_code( true )
, mActive( false )
, mUserActiveState( false )
, mTemporary( false )
, mFolder( false )
{
}
template <class T>
Tree<T>::Tree( T * pParent )
: mpParent( pParent )
, mpMyChildrenList( new std::list<T *> )
, mID( 0 )
, mOK_init( true )
, mOK_code( true )
, mActive( false )
, mUserActiveState( false )
, mTemporary( false )
, mFolder( false )
{
if (pParent) {
pParent->addChild((T*)(this));
} else {
mpParent = nullptr;
}
}
template <class T>
Tree<T>::~Tree()
{
while (!mpMyChildrenList->empty()) {
auto it = mpMyChildrenList->begin();
T* pChild = *it;
delete pChild;
}
delete mpMyChildrenList;
if (mpParent) {
mpParent->popChild((T*)this); // tell parent about my death
if (std::uncaught_exception()) {
std::cout << "ERROR: Hook destructed during stack rewind because of an uncaught exception." << std::endl;
}
}
}
template <class T>
void Tree<T>::setTemporary(const bool state) {
mTemporary = state;
}
template <class T>
bool Tree<T>::isTemporary() const {
return mTemporary;
}
template <class T>
bool Tree<T>::ancestorsActive() const
{
Tree<T>* node(mpParent);
while (node) {
if (!node->isActive()) {
return false;
}
node = node->mpParent;
}
return true;
}
template <class T>
bool Tree<T>::shouldBeActive() const
{
return mUserActiveState;
}
template <class T>
void Tree<T>::setShouldBeActive(bool b)
{
mUserActiveState = b;
}
template <class T>
bool Tree<T>::setIsActive(bool b)
{
setShouldBeActive(b);
if (b) {
return activate();
} else {
mActive = false;
return false;
}
}
template <class T>
inline bool Tree<T>::state() const
{
return (mOK_init && mOK_code);
}
template <class T>
inline bool Tree<T>::canBeActivated() const
{
return (shouldBeActive() && state());
}
template <class T>
bool Tree<T>::activate()
{
if (canBeActivated()) {
mActive = true;
return true;
}
mActive = false;
return false;
}
template <class T>
void Tree<T>::deactivate()
{
mActive = false;
}
template <class T>
bool Tree<T>::isActive() const
{
return (mActive && canBeActivated());
}
template <class T>
void Tree<T>::enableFamily()
{
activate();
for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
(*it)->enableFamily();
}
}
template <class T>
void Tree<T>::disableFamily()
{
deactivate();
for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
(*it)->disableFamily();
}
}
template <class T>
void Tree<T>::addChild(T* newChild, int parentPosition, int childPosition)
{
if ((parentPosition == -1) || (childPosition >= static_cast<int>(mpMyChildrenList->size()))) {
mpMyChildrenList->push_back(newChild);
} else {
// insert item at proper position
int cnt = 0;
for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
if (cnt >= childPosition) {
mpMyChildrenList->insert(it, newChild);
break;
}
cnt++;
}
}
}
template <class T>
void Tree<T>::setParent(T* pParent)
{
mpParent = pParent;
}
template <class T>
bool Tree<T>::popChild(T* pChild)
{
for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
if (*it == pChild) {
mpMyChildrenList->remove(pChild);
return true;
}
}
return false;
}
template <class T>
std::list<T*>* Tree<T>::getChildrenList() const
{
return mpMyChildrenList;
}
template <class T>
QString& Tree<T>::getError()
{
return mErrorMessage;
}
template <class T>
void Tree<T>::setError(QString error)
{
mErrorMessage = error;
}
#endif // MUDLET_TREE_H