Mudlet  0
Mudclient
Tree.h
Go to the documentation of this file.
1 #ifndef MUDLET_TREE_H
2 #define MUDLET_TREE_H
3 
4 /***************************************************************************
5  * Copyright (C) 2008-2012 by Heiko Koehn - KoehnHeiko@googlemail.com *
6  * Copyright (C) 2014 by Ahmed Charles - acharles@outlook.com *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  ***************************************************************************/
23 
24 
25 #include "pre_guard.h"
26 #include <QString>
27 #include "post_guard.h"
28 
29 #include <iostream>
30 #include <list>
31 
32 
33 template <class T>
34 class Tree
35 {
36 public:
37  explicit Tree();
38  explicit Tree(T* parent);
39  virtual ~Tree();
40 
41  T* getParent() const { return mpParent; }
42  std::list<T*>* getChildrenList() const;
43  bool hasChildren() const { return (!mpMyChildrenList->empty()); }
44  int getChildCount() const { return mpMyChildrenList->size(); }
45  int getID() const { return mID; }
46  void setID(const int id) { mID = id; }
47  void addChild(T* newChild, int parentPostion = -1, int parentPosition = -1);
48  bool popChild(T* removeChild);
49  void setParent(T* parent);
50  void enableFamily();
51  void disableFamily();
52  bool isActive() const;
53  bool activate();
54  void deactivate();
55  bool setIsActive(bool);
56  bool shouldBeActive() const;
57  void setShouldBeActive(bool b);
58  bool isTemporary() const;
59  void setTemporary(bool state);
60  // Returns true if all the ancesters of this node are active. If there are no ancestors it also returns true.
61  bool ancestorsActive() const;
62  QString& getError();
63  void setError(QString);
64  bool state() const;
65  QString getPackageName() const { return mPackageName; }
66  void setPackageName(const QString& n) { mPackageName = n; }
67  void setModuleName(const QString& n) { mModuleName = n; }
68  QString getModuleName() const { return mModuleName; }
69  bool isFolder() { return mFolder; }
70  void setIsFolder(bool b) { mFolder = b; }
71 
73  std::list<T*>* mpMyChildrenList;
74  int mID;
77 
78 protected:
79  virtual bool canBeActivated() const;
80 
81  bool mOK_init;
82  bool mOK_code;
83 
84 private:
85  bool mActive;
88  bool mTemporary;
89  bool mFolder;
90 };
91 
92 template <class T>
94 : mpParent( nullptr )
95 , mpMyChildrenList( new std::list<T *> )
96 , mID( 0 )
97 , mOK_init( true )
98 , mOK_code( true )
99 , mActive( false )
101 , mTemporary( false )
102 , mFolder( false )
103 {
104 }
105 
106 template <class T>
107 Tree<T>::Tree( T * pParent )
108 : mpParent( pParent )
109 , mpMyChildrenList( new std::list<T *> )
110 , mID( 0 )
111 , mOK_init( true )
112 , mOK_code( true )
113 , mActive( false )
115 , mTemporary( false )
116 , mFolder( false )
117 {
118  if (pParent) {
119  pParent->addChild((T*)(this));
120  } else {
121  mpParent = nullptr;
122  }
123 }
124 
125 template <class T>
127 {
128  while (!mpMyChildrenList->empty()) {
129  auto it = mpMyChildrenList->begin();
130  T* pChild = *it;
131  delete pChild;
132  }
133  delete mpMyChildrenList;
134  if (mpParent) {
135  mpParent->popChild((T*)this); // tell parent about my death
136  if (std::uncaught_exception()) {
137  std::cout << "ERROR: Hook destructed during stack rewind because of an uncaught exception." << std::endl;
138  }
139  }
140 }
141 
142 template <class T>
143 void Tree<T>::setTemporary(const bool state) {
144  mTemporary = state;
145 }
146 
147 template <class T>
148 bool Tree<T>::isTemporary() const {
149  return mTemporary;
150 }
151 
152 template <class T>
154 {
155  Tree<T>* node(mpParent);
156  while (node) {
157  if (!node->isActive()) {
158  return false;
159  }
160  node = node->mpParent;
161  }
162  return true;
163 }
164 
165 template <class T>
167 {
168  return mUserActiveState;
169 }
170 
171 template <class T>
173 {
174  mUserActiveState = b;
175 }
176 
177 template <class T>
179 {
181  if (b) {
182  return activate();
183  } else {
184  mActive = false;
185  return false;
186  }
187 }
188 
189 template <class T>
190 inline bool Tree<T>::state() const
191 {
192  return (mOK_init && mOK_code);
193 }
194 
195 template <class T>
196 inline bool Tree<T>::canBeActivated() const
197 {
198  return (shouldBeActive() && state());
199 }
200 
201 template <class T>
203 {
204  if (canBeActivated()) {
205  mActive = true;
206  return true;
207  }
208  mActive = false;
209  return false;
210 }
211 
212 template <class T>
214 {
215  mActive = false;
216 }
217 
218 template <class T>
219 bool Tree<T>::isActive() const
220 {
221  return (mActive && canBeActivated());
222 }
223 
224 template <class T>
226 {
227  activate();
228  for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
229  (*it)->enableFamily();
230  }
231 }
232 
233 template <class T>
235 {
236  deactivate();
237  for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
238  (*it)->disableFamily();
239  }
240 }
241 
242 template <class T>
243 void Tree<T>::addChild(T* newChild, int parentPosition, int childPosition)
244 {
245  if ((parentPosition == -1) || (childPosition >= static_cast<int>(mpMyChildrenList->size()))) {
246  mpMyChildrenList->push_back(newChild);
247  } else {
248  // insert item at proper position
249  int cnt = 0;
250  for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
251  if (cnt >= childPosition) {
252  mpMyChildrenList->insert(it, newChild);
253  break;
254  }
255  cnt++;
256  }
257  }
258 }
259 
260 template <class T>
261 void Tree<T>::setParent(T* pParent)
262 {
263  mpParent = pParent;
264 }
265 
266 template <class T>
267 bool Tree<T>::popChild(T* pChild)
268 {
269  for (auto it = mpMyChildrenList->begin(); it != mpMyChildrenList->end(); it++) {
270  if (*it == pChild) {
271  mpMyChildrenList->remove(pChild);
272  return true;
273  }
274  }
275  return false;
276 }
277 
278 template <class T>
279 std::list<T*>* Tree<T>::getChildrenList() const
280 {
281  return mpMyChildrenList;
282 }
283 
284 template <class T>
286 {
287  return mErrorMessage;
288 }
289 
290 template <class T>
292 {
293  mErrorMessage = error;
294 }
295 
296 #endif // MUDLET_TREE_H
void setTemporary(bool state)
Definition: Tree.h:143
T * mpParent
Definition: Tree.h:72
void deactivate()
Definition: Tree.h:213
void setModuleName(const QString &n)
Definition: Tree.h:67
int mID
Definition: Tree.h:74
QString getPackageName() const
Definition: Tree.h:65
bool isFolder()
Definition: Tree.h:69
bool mTemporary
Definition: Tree.h:88
std::list< T * > * getChildrenList() const
Definition: Tree.h:279
bool mUserActiveState
Definition: Tree.h:86
virtual bool canBeActivated() const
Definition: Tree.h:196
bool activate()
Definition: Tree.h:202
void setIsFolder(bool b)
Definition: Tree.h:70
bool state() const
Definition: Tree.h:190
virtual ~Tree()
Definition: Tree.h:126
STL namespace.
bool setIsActive(bool)
Definition: Tree.h:178
bool mOK_init
Definition: Tree.h:81
Tree()
Definition: Tree.h:93
std::list< T * > * mpMyChildrenList
Definition: Tree.h:73
bool hasChildren() const
Definition: Tree.h:43
void disableFamily()
Definition: Tree.h:234
Definition: Tree.h:34
void setID(const int id)
Definition: Tree.h:46
bool popChild(T *removeChild)
Definition: Tree.h:267
QString mErrorMessage
Definition: Tree.h:87
bool shouldBeActive() const
Definition: Tree.h:166
int getChildCount() const
Definition: Tree.h:44
bool ancestorsActive() const
Definition: Tree.h:153
bool mOK_code
Definition: Tree.h:82
void addChild(T *newChild, int parentPostion=-1, int parentPosition=-1)
Definition: Tree.h:243
return false
Definition: ctelnet.cpp:465
int getID() const
Definition: Tree.h:45
void setPackageName(const QString &n)
Definition: Tree.h:66
T * getParent() const
Definition: Tree.h:41
bool mFolder
Definition: Tree.h:89
QString mPackageName
Definition: Tree.h:75
void setShouldBeActive(bool b)
Definition: Tree.h:172
bool mActive
Definition: Tree.h:85
void setError(QString)
Definition: Tree.h:291
void enableFamily()
Definition: Tree.h:225
bool isTemporary() const
Definition: Tree.h:148
QString getModuleName() const
Definition: Tree.h:68
QString mModuleName
Definition: Tree.h:76
void setParent(T *parent)
Definition: Tree.h:261
bool isActive() const
Definition: Tree.h:219
QString & getError()
Definition: Tree.h:285