configurationnode.h
1 /***************************************************************************
2  * Copyright (C) 2008 by Tomassino Ferrauto *
3  * t_ferrauto@yahoo.it *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
21 #ifndef CONFIGURATION_NODE_H
22 #define CONFIGURATION_NODE_H
23 
24 #include "configurationconfig.h"
25 #include <QMap>
26 #include <QString>
27 #include <QStringList>
28 
29 namespace farsa {
30 
31 class ParameterSettable;
32 
37 enum ObjectCreationStatus {
38  ObjectNotCreated,
39  CreatingObject,
40  ObjectCreatedNotConfigured,
42  ConfiguringObject,
44  CreatingAndConfiguringObject,
48  ObjectCreatedAndConfigured
50 };
51 
64 class FARSA_CONF_API ConfigurationNode
65 {
66 public:
71  {
76  object(NULL),
77  objectStatus(ObjectNotCreated)
78  {
79  }
80 
85  object(other.object),
86  objectStatus(other.objectStatus)
87  {
88  }
89 
93  ObjectAndStatus& operator=(const ObjectAndStatus& other)
94  {
95  if (this == &other) {
96  return *this;
97  }
98 
99  object = other.object;
100  objectStatus = other.objectStatus;
101 
102  return *this;
103  }
104 
109 
113  ObjectCreationStatus objectStatus;
114  };
115 
116 public:
128  ConfigurationNode(ConfigurationNode *parent, QString name = QString(), bool caseSensitive = false);
129 
134 
140  const ConfigurationNode* getParent() const
141  {
142  return m_parent;
143  }
144 
152  QList<const ConfigurationNode*> getAncestors() const;
153 
161  QStringList getAncestorsNames() const;
162 
168  QString getName() const
169  {
170  return m_name;
171  }
172 
179  QString getFullName() const;
180 
186  bool isNull() const
187  {
188  return m_name.isNull();
189  }
190 
196  bool isCaseSensitive() const
197  {
198  return m_caseSensitive;
199  }
200 
209  ConfigurationNode* addNode(QString name);
210 
220  ConfigurationNode* getNode(QString path);
221 
232  const ConfigurationNode* getNode(QString path) const;
233 
240  bool deleteNode(QString name);
241 
249  bool renameNode(QString oldName, QString newName);
250 
256  QList<ConfigurationNode*> getChildrenNodesList();
257 
263  QStringList getChildrenList() const;
264 
273  QStringList getFilteredChildrenList(QRegExp filter) const;
274 
288  bool setObjectForNode(QString path, ParameterSettable* object, ObjectCreationStatus status = ObjectCreatedAndConfigured);
289 
296  void resetObject();
297 
306  ObjectAndStatus getObjectForNode(QString path) const;
307 
316  bool addParameter(QString name);
317 
346  QString getValue(QString path, bool alsoMatchParents = false) const;
347 
378  ObjectAndStatus getObject(QString path, bool alsoMatchParents = false) const;
379 
388  bool setValue(QString path, QString value);
389 
399  bool setValue(QString path, ParameterSettable* object);
400 
408  bool deleteParameter(QString name);
409 
416  QStringList getParametersList() const;
417 
427  QStringList getFilteredParametersList(QRegExp filter) const;
428 
434  QStringList getObjectParametersList() const;
435 
443  QStringList getFilteredObjectParametersList(QRegExp filter) const;
444 
448  void clearAll();
449 
450 private:
454  ConfigurationNode *const m_parent;
455 
460  const bool m_caseSensitive;
461 
465  const QString m_name;
466 
472  mutable ConfigurationNode* m_nullNode;
473 
477  mutable QString m_nullValue;
478 
482  ObjectAndStatus m_object;
483 
487  QMap<QString, ConfigurationNode*> m_children;
488 
492  QStringList m_childrenkeys;
493 
497  QMap<QString, QString> m_parameters;
498 
502  QStringList m_parameterskeys;
503 
507  QMap<QString, ObjectAndStatus> m_objectParameters;
508 
513 
517  ConfigurationNode& operator=(const ConfigurationNode &);
518 };
519 
520 } // end namespace farsa
521 
522 #endif