00001
00011 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00012 #pragma once
00013 #endif
00014 #ifndef COMMON_CONFIG_H_
00015 #define COMMON_CONFIG_H_
00016
00017 #include <typeinfo>
00018
00019 namespace common
00020 {
00021
00022 class Stream;
00023
00028
00030 class Config_pimpl;
00031
00033 class Item
00034 {
00035 public:
00036 virtual ~Item() { }
00037 };
00038
00040 class Value : public Item
00041 {
00042 public:
00043 tstring Data;
00044
00045 Value() { }
00046 Value(const tstring &s) : Data(s) { }
00047 };
00048
00050 class List : public Item
00051 {
00052 public:
00053 STRING_VECTOR Data;
00054 };
00055
00057
00061 class Config : public Item
00062 {
00063 DECLARE_NO_COPY_CLASS(Config)
00064 friend class Config_pimpl;
00065
00066 private:
00067 scoped_ptr<Config_pimpl> pimpl;
00068
00069 public:
00072
00073 static const uint4 CM_MUST_EXIST = 0x02;
00075 static const uint4 CM_MUST_NOT_EXIST = 0x04;
00077 static const uint4 CM_CREATE_PATH = 0x08;
00079 static const uint4 CM_SAME_TYPE = 0x10;
00081
00082 Config();
00083 ~Config();
00084
00086 void LoadFromString(const tstring &S);
00088 void LoadFromStream(Stream *S);
00090 void LoadFromFile(const tstring &FileName);
00092 void SaveToString(tstring *S) const;
00094 void SaveToStream(Stream *S) const;
00096 void SaveToFile(const tstring &FileName) const;
00097
00099 Item * GetItem(const tstring &Path);
00101 Item * MustGetItem(const tstring &Path);
00103
00104 bool GetData(const tstring &Path, tstring *Data);
00106
00107 void MustGetData(const tstring &Path, tstring *Data);
00108
00110 bool ItemExists(const tstring &Path);
00112 void ItemMustExists(const tstring &Path);
00113
00115 bool SetItem(const tstring &Path, Item *a_Item, uint4 CreateMode);
00117 void MustSetItem(const tstring &Path, Item *a_Item, uint4 CreateMode);
00119 bool SetData(const tstring &Path, const tstring &Data, uint4 CreateMode);
00121 void MustSetData(const tstring &Path, const tstring &Data, uint4 CreateMode);
00122
00124 bool DeleteItem(const tstring &Path);
00125 void MustDeleteItem(const tstring &Path);
00126
00128 void GetElementNames(STRING_VECTOR *OutNames);
00129
00131
00133 template <typename T> T * GetItemEx(const tstring &Path);
00135
00136 template <typename T> T * MustGetItemEx(const tstring &Path);
00138
00139 template <typename T> bool GetDataEx(const tstring &Path, T *Data);
00141
00142 template <typename T> void MustGetDataEx(const tstring &Path, T *Data);
00144 template <typename T> bool ItemExistsEx(const tstring &Path);
00146 template <typename T> void ItemMustExistsEx(const tstring &Path);
00148
00149 template <typename T> bool SetDataEx(const tstring &Path, const T &Data, uint4 CreateMode);
00151
00152 template <typename T> void MustSetDataEx(const tstring &Path, const T &Data, uint4 CreateMode);
00153 };
00154
00155 template <typename T>
00156 inline T * Config::GetItemEx(const tstring &Path)
00157 {
00158 Item *i = GetItem(Path);
00159 if (i == 0)
00160 return 0;
00161 if (dynamic_cast<T*>(i) == 0)
00162 return 0;
00163 return i;
00164 }
00165
00166 template <typename T>
00167 inline T * Config::MustGetItemEx(const tstring &Path)
00168 {
00169 Item *i = MustGetItem(Path);
00170 T *R;
00171 if ((R = dynamic_cast<T*>(i)) == 0)
00172 throw Error(_T("Cannot read configuration element from path \"") + Path + _T("\". Element type other than expected."));
00173 return R;
00174 }
00175
00176 template <typename T>
00177 inline bool Config::GetDataEx(const tstring &Path, T *Data)
00178 {
00179 tstring s;
00180 if (!GetData(Path, &s))
00181 return false;
00182 if (!StrToSth<T>(Data, s))
00183 return false;
00184 return true;
00185 }
00186
00187 template <typename T>
00188 inline void Config::MustGetDataEx(const tstring &Path, T *Data)
00189 {
00190 tstring s;
00191 MustGetData(Path, &s);
00192 if (!StrToSth<T>(Data, s))
00193 throw Error(_T("Cannot read configuration data from path: \"") + Path + _T("\". Conversion error."));
00194 }
00195
00196 template <typename T>
00197 inline bool Config::ItemExistsEx(const tstring &Path)
00198 {
00199 Item *i = GetItem(Path);
00200 if (i == 0)
00201 return false;
00202 if (dynamic_cast<T*>(i) == 0)
00203 return false;
00204 return true;
00205 }
00206
00207 template <typename T>
00208 inline void Config::ItemMustExistsEx(const tstring &Path)
00209 {
00210 Item *i = GetItem(Path);
00211 if (i == 0)
00212 throw Error(_T("Configuration element with path \"") + Path + _T("\" does not exist."));
00213 if (dynamic_cast<T*>(i) == 0)
00214 throw Error(_T("Configuration element with path \"") + Path + _T("\" has invalid type."));
00215 }
00216
00217 template <typename T>
00218 inline bool Config::SetDataEx(const tstring &Path, const T &Data, uint4 CreateMode)
00219 {
00220 tstring s;
00221 SthToStr<T>(&s, Data);
00222 return SetData(Path, s, CreateMode);
00223 }
00224
00225 template <typename T>
00226 inline void Config::MustSetDataEx(const tstring &Path, const T &Data, uint4 CreateMode)
00227 {
00228 tstring s;
00229 SthToStr<T>(&s, Data);
00230 MustSetData(Path, s, CreateMode);
00231 }
00232
00234 extern scoped_ptr<Config> g_Config;
00235
00237
00238
00239 }
00240
00241 #endif