00001 #ifndef CONFIGURATION_H_
00002 #define CONFIGURATION_H_
00003
00004 #include <string>
00005 #include <vector>
00006 #include <stdlib.h>
00007 #include <threemxl/platform/io/logging/Log2.h>
00008
00009 #include "OptionVars.h"
00010
00011 #define CONFIGURATION_ARRAY_DELIMITER ';'
00012
00013
00014 class CConfigSection;
00015 class IConfig;
00016 class IConfigProperty;
00017
00018 namespace mu
00019 {
00020 class Parser;
00021 }
00022
00023
00024
00025
00026
00027 class IConfig
00028 {
00029 protected:
00030 std::vector<IConfig*> mPendingInterfaces;
00031
00032 IConfig* registerPendingInterface(IConfig* configInterface)
00033 {
00034 mPendingInterfaces.push_back(configInterface);
00035 return configInterface;
00036 }
00037
00038 public:
00039 virtual ~IConfig()
00040 {
00041
00042
00043 while (mPendingInterfaces.size() > 0)
00044 {
00045 delete mPendingInterfaces.back();
00046 mPendingInterfaces.pop_back();
00047 }
00048 }
00049 };
00050
00051 class IConfigSection: public IConfig
00052 {
00053 friend class CConfigSection;
00054 public:
00055 virtual ~IConfigSection() {}
00056 protected:
00057
00058 virtual std::string name() const {return "";}
00059 virtual bool hasSection(const std::string& section) const {return false;}
00060
00061 virtual IConfigSection* parent() {return NULL;}
00062 virtual IConfigSection* section(const std::string& section) {return NULL;}
00063 virtual IConfigSection* firstSection() {return NULL;}
00064 virtual IConfigSection* nextSection() {return NULL;}
00065 virtual IConfigSection* nextSimilarSection() {return NULL;}
00066
00067 virtual IConfigProperty* get(const std::string& property) {return NULL;}
00068 virtual IConfigProperty* firstProperty() {return NULL;}
00069 virtual bool has(const std::string& property) const {return false;}
00070 };
00071
00072 class IConfigProperty: public IConfig
00073 {
00074 friend class CConfigSection;
00075 friend class CConfigProperty;
00076 public:
00077 virtual ~IConfigProperty() {}
00078 protected:
00079 virtual std::string name() const {return "";}
00080
00081 virtual std::string toString() const=0;
00082
00083 bool toBool() const;
00084 long toInt() const;
00085 unsigned long toUInt() const;
00086 double toFloat() const;
00087 virtual bool isVerbose() const {return false;}
00088
00089 virtual void set(const std::string& value) {}
00090
00091
00092 virtual IConfigProperty* nextProperty() {return NULL;}
00093 };
00094
00095
00096 class IConfigPropertyString: public IConfigProperty
00097 {
00098 protected:
00099 std::string mData;
00100
00101 public:
00102 IConfigPropertyString(const std::string& data):
00103 mData(data)
00104 {}
00105 virtual std::string toString() const {return mData;}
00106 };
00107
00108 class CConfigProperty;
00109
00110 class CConfigPropertyArray: protected std::vector<IConfigPropertyString>
00111 {
00112 public:
00113 void setData(const std::string& data, const char delimiter=CONFIGURATION_ARRAY_DELIMITER);
00114
00115 using std::vector<IConfigPropertyString>::size;
00116 CConfigProperty operator[](size_type __n);
00117 };
00118
00119 class CConfigProperty
00120 {
00121 protected:
00122 IConfigProperty *mIConfigProperty;
00123
00124 public:
00125 CConfigProperty(IConfigProperty* configPropertyInterface);
00126 virtual ~CConfigProperty();
00127
00128 std::string name() const;
00129 std::string value() const;
00130 void set(const std::string& value);
00131 CConfigProperty nextProperty() const;
00132 bool isNull() const;
00133
00134
00135 virtual bool toBool() const {if (!isNull()) return mIConfigProperty->toBool(); else return false;}
00136 virtual long toInt() const {if (!isNull()) return mIConfigProperty->toInt(); else return 0;}
00137 virtual unsigned long toUInt() const {if (!isNull()) return mIConfigProperty->toUInt(); else return 0;}
00138 virtual double toFloat() const {if (!isNull()) return mIConfigProperty->toFloat(); else return 0;}
00139
00140 virtual bool isVerbose() const {if (!isNull()) return mIConfigProperty->isVerbose(); else return false;}
00141
00142 virtual void toArray(CConfigPropertyArray* array) const {array->setData(mIConfigProperty->toString());}
00143
00144 operator bool() const
00145 {
00146 return isNull();
00147 }
00148 };
00149
00150 class CConfigSection
00151 {
00152 protected:
00153 IConfigSection *mIConfigSection;
00154
00155 public:
00156 CConfigSection(IConfigSection* configSectionInterface);
00157 ~CConfigSection();
00158
00164 std::string name() const;
00165
00172 bool hasSection(const std::string& section) const;
00173
00177 CConfigSection parent() const;
00178
00185 CConfigSection section(const std::string& section) const;
00186
00192 CConfigSection firstSection() const;
00193
00200 CConfigSection nextSection() const;
00201
00208 CConfigSection nextSimilarSection() const;
00209
00217 bool has(const std::string& property) const;
00218
00219
00220 CConfigProperty get(const std::string& property) const;
00221
00222 CConfigProperty firstProperty() const;
00223
00234 bool get(const std::string& property, std::string *value) const;
00235 bool get(const std::string& property, bool *value) const;
00236 bool get(const std::string& property, char *value) const;
00237 bool get(const std::string& property, unsigned char *value) const;
00238 bool get(const std::string& property, short *value) const;
00239 bool get(const std::string& property, unsigned short *value) const;
00240 bool get(const std::string& property, int *value) const;
00241 bool get(const std::string& property, unsigned int *value) const;
00242 bool get(const std::string& property, long *value) const;
00243 bool get(const std::string& property, unsigned long *value) const;
00244 bool get(const std::string& property, long long *value) const;
00245 bool get(const std::string& property, unsigned long long *value) const;
00246 bool get(const std::string& property, float *value) const;
00247 bool get(const std::string& property, double *value) const;
00248
00249
00250 bool get(const std::string& property, COptionBool *value) const;
00251 bool get(const std::string& property, COptionInt *value) const;
00252 bool get(const std::string& property, COptionDouble *value) const;
00253 bool get(const std::string& property, COptionChar *value) const;
00254 bool get(const std::string& property, COptionByte *value) const;
00255 bool get(const std::string& property, COptionWord *value) const;
00256
00257
00258 bool getArray(const std::string& property, CConfigPropertyArray* array) const;
00259 bool getArray(const std::string& property, double* array, unsigned int maxNumElements) const;
00260
00269 bool get(const std::string& property, std::string *value, std::string preset) const;
00270 bool get(const std::string& property, bool *value, bool preset) const;
00271 bool get(const std::string& property, char *value, char preset) const;
00272 bool get(const std::string& property, unsigned char *value, unsigned char preset) const;
00273 bool get(const std::string& property, short *value, short preset) const;
00274 bool get(const std::string& property, unsigned short *value, unsigned short preset) const;
00275 bool get(const std::string& property, int *value, int preset) const;
00276 bool get(const std::string& property, unsigned int *value, unsigned int preset) const;
00277 bool get(const std::string& property, long *value, long preset) const;
00278 bool get(const std::string& property, unsigned long *value, unsigned long preset) const;
00279 bool get(const std::string& property, long long *value, long long preset) const;
00280 bool get(const std::string& property, unsigned long long *value, unsigned long long preset) const;
00281 bool get(const std::string& property, float *value, float preset) const;
00282 bool get(const std::string& property, double *value, double preset) const;
00283
00291
00292
00293 bool isNull() const;
00294
00295 operator bool() const
00296 {
00297 return isNull();
00298 }
00299 };
00300
00301
00302 class CConfigConstant
00303 {
00304 public:
00305 std::string mKey;
00306 std::string mValue;
00307
00308 CConfigConstant() {}
00309 CConfigConstant(const std::string& key, const std::string& value): mKey(key), mValue(value) {}
00310
00311 };
00312
00313 typedef std::vector<CConfigConstant> CConfigConstants;
00314
00315
00321 #define CONST_ConfRootSectionName "configuration"
00322 #define CONST_ConfConstantsSectionName "constants"
00323 #define CONST_ConfStringsSectionName "strings"
00324
00325 class CConfiguration
00326 {
00327 protected:
00328 CLog2 mLog;
00329
00330
00331
00332 std::string replaceConstants(const std::string& expr, mu::Parser *parser);
00333
00334
00335
00336
00337 int resolveExpressionsInSection(const CConfigSection& section, mu::Parser *parser);
00338
00339
00340
00341
00342 std::vector<std::string> mRegisteredStrings;
00343 std::vector<std::string> mNodesExcludedFromParsing;
00344 bool isRegisteredString(const std::string& str);
00345 bool shouldParseNode(const std::string& nodeName);
00346
00347
00348
00349
00350 std::map<std::string, std::string> mStringConstants;
00351
00352
00353
00354
00355 std::string replaceStringConstants(const std::string& expr);
00356
00357 public:
00361 CConfiguration(): mLog("config") {}
00362
00366 virtual ~CConfiguration() {}
00367
00373 virtual bool loadFile(const std::string& filename)=0;
00374
00379 virtual bool saveFile(const std::string& filename = "")=0;
00380
00384 virtual bool reload()=0;
00385
00389 virtual std::string errorStr()=0;
00390
00394 virtual void clear()=0;
00395
00399 virtual CConfigSection root()=0;
00400
00404 int resolveExpressions();
00405 };
00406
00407 #endif