configuration/include/configurationnode.h

00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Tomassino Ferrauto                              *
00003  *   t_ferrauto@yahoo.it                                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00021 #ifndef CONFIGURATION_NODE_H
00022 #define CONFIGURATION_NODE_H
00023 
00024 #include "configurationconfig.h"
00025 #include <QMap>
00026 #include <QString>
00027 #include <QStringList>
00028 
00029 namespace farsa {
00030 
00031 class ParameterSettable;
00032 
00037 enum ObjectCreationStatus {
00038     ObjectNotCreated, 
00039     CreatingObject, 
00040     ObjectCreatedNotConfigured, 
00042     ConfiguringObject, 
00044     CreatingAndConfiguringObject, 
00048     ObjectCreatedAndConfigured 
00050 };
00051 
00064 class FARSA_CONF_API ConfigurationNode
00065 {
00066 public:
00070     struct ObjectAndStatus
00071     {
00075         ObjectAndStatus() :
00076             object(NULL),
00077             objectStatus(ObjectNotCreated)
00078         {
00079         }
00080 
00084         ObjectAndStatus(const ObjectAndStatus& other) :
00085             object(other.object),
00086             objectStatus(other.objectStatus)
00087         {
00088         }
00089 
00093         ObjectAndStatus& operator=(const ObjectAndStatus& other)
00094         {
00095             if (this == &other) {
00096                 return *this;
00097             }
00098 
00099             object = other.object;
00100             objectStatus = other.objectStatus;
00101 
00102             return *this;
00103         }
00104 
00108         ParameterSettable *object;
00109 
00113         ObjectCreationStatus objectStatus;
00114     };
00115 
00116 public:
00128     ConfigurationNode(ConfigurationNode *parent, QString name = QString(), bool caseSensitive = false);
00129 
00133     ~ConfigurationNode();
00134 
00140     const ConfigurationNode* getParent() const
00141     {
00142         return m_parent;
00143     }
00144 
00152     QList<const ConfigurationNode*> getAncestors() const;
00153 
00161     QStringList getAncestorsNames() const;
00162 
00168     QString getName() const
00169     {
00170         return m_name;
00171     }
00172 
00179     QString getFullName() const;
00180 
00186     bool isNull() const
00187     {
00188         return m_name.isNull();
00189     }
00190 
00196     bool isCaseSensitive() const
00197     {
00198         return m_caseSensitive;
00199     }
00200 
00209     ConfigurationNode* addNode(QString name);
00210 
00220     ConfigurationNode* getNode(QString path);
00221 
00232     const ConfigurationNode* getNode(QString path) const;
00233 
00240     bool deleteNode(QString name);
00241 
00249     bool renameNode(QString oldName, QString newName);
00250 
00256     QList<ConfigurationNode*> getChildrenNodesList();
00257 
00263     QStringList getChildrenList() const;
00264 
00273     QStringList getFilteredChildrenList(QRegExp filter) const;
00274 
00288     bool setObjectForNode(QString path, ParameterSettable* object, ObjectCreationStatus status = ObjectCreatedAndConfigured);
00289 
00296     void resetObject();
00297 
00306     ObjectAndStatus getObjectForNode(QString path) const;
00307 
00316     bool addParameter(QString name);
00317 
00346     QString getValue(QString path, bool alsoMatchParents = false) const;
00347 
00378     ObjectAndStatus getObject(QString path, bool alsoMatchParents = false) const;
00379 
00388     bool setValue(QString path, QString value);
00389 
00399     bool setValue(QString path, ParameterSettable* object);
00400 
00408     bool deleteParameter(QString name);
00409 
00416     QStringList getParametersList() const;
00417 
00427     QStringList getFilteredParametersList(QRegExp filter) const;
00428 
00434     QStringList getObjectParametersList() const;
00435 
00443     QStringList getFilteredObjectParametersList(QRegExp filter) const;
00444 
00448     void clearAll();
00449 
00450 private:
00454     ConfigurationNode *const m_parent;
00455 
00460     const bool m_caseSensitive;
00461 
00465     const QString m_name;
00466 
00472     mutable ConfigurationNode* m_nullNode;
00473 
00477     mutable QString m_nullValue;
00478 
00482     ObjectAndStatus m_object;
00483 
00487     QMap<QString, ConfigurationNode*> m_children;
00488     
00492     QStringList m_childrenkeys;
00493 
00497     QMap<QString, QString> m_parameters;
00498 
00502     QStringList m_parameterskeys;
00503 
00507     QMap<QString, ObjectAndStatus> m_objectParameters;
00508 
00512     ConfigurationNode(const ConfigurationNode &);
00513 
00517     ConfigurationNode& operator=(const ConfigurationNode &);
00518 };
00519 
00520 } // end namespace farsa
00521 
00522 #endif