Changeset 1006 for code/branches/core2/src/orxonox/core
- Timestamp:
- Apr 10, 2008, 1:24:29 AM (17 years ago)
- Location:
- code/branches/core2/src/orxonox/core
- Files:
-
- 1 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/core2/src/orxonox/core/CMakeLists.txt
r966 r1006 6 6 IdentifierDistributor.cc 7 7 MetaObjectList.cc 8 ConfigFileManager.cc 8 9 ConfigValueContainer.cc 9 10 Error.cc -
code/branches/core2/src/orxonox/core/ConfigFileManager.h
r989 r1006 29 29 #define _ConfigFileManager_H__ 30 30 31 #include <iostream> 32 #include <string> 31 33 #include <list> 34 #include <map> 35 36 #include "util/Math.h" 32 37 33 38 #include "CorePrereqs.h" 39 40 #define DEFAULT_CONFIG_FILE "default.ini" 34 41 35 42 namespace orxonox 36 43 { 37 class ConfigFileEntry 38 { 39 public: 40 ConfigFileEntry(const std::string& name, const std::string& value = ""); 41 42 void setValue(const std::string& value); 43 const std::string& getValue() const; 44 const std::string& getName() const; 45 46 private: 44 enum _CoreExport ConfigFileType 45 { 46 CFT_Settings, 47 CFT_Keybindings 48 }; 49 50 51 void reloadConfig(); 52 void saveConfig(); 53 void cleanConfig(); 54 void loadSettings(const std::string& filename); 55 void loadKeybindings(const std::string& filename); 56 57 58 ///////////////////// 59 // ConfigFileEntry // 60 ///////////////////// 61 class _CoreExport ConfigFileEntry 62 { 63 public: 64 virtual void setValue(const std::string& value) = 0; 65 virtual const std::string& getValue() const = 0; 66 virtual const std::string& getName() const = 0; 67 virtual unsigned int getIndex() const { return 0; } 68 virtual std::string getFileEntry() const = 0; 69 }; 70 71 72 ////////////////////////// 73 // ConfigFileEntryValue // 74 ////////////////////////// 75 class _CoreExport ConfigFileEntryValue : public ConfigFileEntry 76 { 77 public: 78 inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", const std::string& additionalComment = "") : name_(name), value_(value), additionalComment_(additionalComment), bString_(false) {} 79 inline virtual ~ConfigFileEntryValue() {} 80 81 inline virtual const std::string& getName() const 82 { return this->name_; } 83 84 inline virtual void setValue(const std::string& value) 85 { this->value_ = value; } 86 inline virtual const std::string& getValue() const 87 { return this->value_; } 88 89 inline bool isString() const 90 { return this->bString_; } 91 inline void setString(bool bString) 92 { this->bString_ = bString; } 93 94 virtual std::string getFileEntry() const; 95 96 protected: 47 97 std::string name_; 48 98 std::string value_; 49 }; 50 51 class ConfigFileSection 52 { 53 public: 54 void addEntry(const std::string& name); 55 ConfigFileEntry* getEntry(const std::string& name) const; 56 57 void setValue(const std::string& name, const std::string& value); 58 const std::string& getValue(const std::string& name) const; 59 60 private: 99 std::string additionalComment_; 100 bool bString_; 101 }; 102 103 104 /////////////////////////////// 105 // ConfigFileEntryArrayValue // 106 /////////////////////////////// 107 class _CoreExport ConfigFileEntryArrayValue : public ConfigFileEntryValue 108 { 109 public: 110 inline ConfigFileEntryArrayValue(const std::string& name, unsigned int index, const std::string& value = "", const std::string& additionalComment = "") : ConfigFileEntryValue(name, value, additionalComment), index_(index) {} 111 inline virtual ~ConfigFileEntryArrayValue() {} 112 113 inline virtual unsigned int getIndex() const 114 { return this->index_; } 115 116 virtual std::string getFileEntry() const; 117 118 private: 119 unsigned int index_; 120 }; 121 122 123 //////////////////////////// 124 // ConfigFileEntryComment // 125 //////////////////////////// 126 class _CoreExport ConfigFileEntryComment : public ConfigFileEntry 127 { 128 public: 129 inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {} 130 inline virtual ~ConfigFileEntryComment() {} 131 132 inline virtual const std::string& getName() const 133 { return this->comment_; } 134 135 inline virtual void setValue(const std::string& value) 136 {} 137 inline virtual const std::string& getValue() const 138 { return this->comment_; } 139 140 inline virtual std::string getFileEntry() const 141 { return this->comment_; } 142 143 private: 144 std::string comment_; 145 }; 146 147 148 /////////////////////// 149 // ConfigFileSection // 150 /////////////////////// 151 class _CoreExport ConfigFileSection 152 { 153 friend class ConfigFile; 154 155 public: 156 inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "") : name_(name), additionalComment_(additionalComment), bUpdated_(false) {} 157 ~ConfigFileSection(); 158 159 inline const std::string& getName() const 160 { return this->name_; } 161 162 inline void setValue(const std::string& name, const std::string& value) 163 { this->getEntry(name)->setValue(value); } 164 inline const std::string& getValue(const std::string& name) 165 { return this->getEntry(name)->getValue(); } 166 167 inline void setValue(const std::string& name, unsigned int index, const std::string& value) 168 { this->getEntry(name, index)->setValue(value); } 169 inline const std::string& getValue(const std::string& name, unsigned int index) 170 { return this->getEntry(name, index)->getValue(); } 171 172 std::string getFileEntry() const; 173 174 private: 175 std::list<ConfigFileEntry*>& getEntries() 176 { return this->entries_; } 177 std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const 178 { return this->entries_.begin(); } 179 std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const 180 { return this->entries_.end(); } 181 182 std::list<ConfigFileEntry*>::iterator getEntryIterator(const std::string& name, unsigned int index); 183 184 inline ConfigFileEntry* getEntry(const std::string& name) 185 { return (*this->getEntryIterator(name, 0)); } 186 inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index) 187 { return (*this->getEntryIterator(name, index)); } 188 61 189 std::string name_; 62 std::list<ConfigFileEntry> entries_; 63 }; 64 65 class ConfigFile 66 { 67 public: 68 enum Type 69 { 70 Settings, 71 Keybindings 72 }; 73 74 public: 190 std::string additionalComment_; 191 std::list<ConfigFileEntry*> entries_; 192 bool bUpdated_; 193 }; 194 195 196 //////////////// 197 // ConfigFile // 198 //////////////// 199 class _CoreExport ConfigFile 200 { 201 public: 202 inline ConfigFile(const std::string& filename) : filename_(filename), bUpdated_(false) {} 203 ~ConfigFile(); 204 205 void load(); 206 void save() const; 207 void clean(); 208 209 inline void setValue(const std::string& section, const std::string& name, const std::string& value) 210 { this->getSection(section)->setValue(name, value); this->save(); } 211 inline const std::string& getValue(const std::string& section, const std::string& name) 212 { return this->getSection(section)->getValue(name); this->saveIfUpdated(); } 213 214 inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value) 215 { this->getSection(section)->setValue(name, index, value); this->save(); } 216 inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index) 217 { return this->getSection(section)->getValue(name, index); this->saveIfUpdated(); } 218 219 private: 220 ConfigFileSection* getSection(const std::string& section); 221 void saveIfUpdated(); 222 223 std::string filename_; 224 std::list<ConfigFileSection*> sections_; 225 bool bUpdated_; 226 }; 227 228 229 /////////////////////// 230 // ConfigFileManager // 231 /////////////////////// 232 class _CoreExport ConfigFileManager 233 { 234 public: 235 static ConfigFileManager* getInstance(); 236 237 void setFile(ConfigFileType type, const std::string& filename); 238 75 239 void load(); 76 240 void save(); 77 78 void addSection(const std::string& name); 79 ConfigFileSection* getSection(const std::string& name) const;80 81 void setValue(const std::string& section, const std::string& name, const std::string& value);82 const std::string& getValue(const std::string& section, const std::string& name) const; 83 84 private:85 std::string name_;86 Type type_;87 std::list<ConfigFileSection> sections_; 88 };89 90 class ConfigFileManager91 {92 public: 93 static ConfigFileManater* getInstance();94 95 void load(const std::string& filename); 96 void save(const std::string& filename) const;97 98 void setFile(const std::string& filename);99 ConfigFile& getFile(const std::string& filename) const;100 101 void setValue(const std::string& filename, const std::string& section, const std::string& name, const std::string& value);102 const std::string& getValue(const std::string& filename, const std::string& section, const std::string& name) const; 103 104 private: 105 std:: list<ConfigFile> configFiles_;241 void clean(); 242 243 void load(ConfigFileType type); 244 void save(ConfigFileType type); 245 void clean(ConfigFileType type); 246 247 inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, const std::string& value) 248 { this->getFile(type)->setValue(section, name, value); } 249 inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name) 250 { return this->getFile(type)->getValue(section, name); } 251 252 inline void setValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index, const std::string& value) 253 { this->getFile(type)->setValue(section, name, index, value); } 254 inline const std::string& getValue(ConfigFileType type, const std::string& section, const std::string& name, unsigned int index) 255 { return this->getFile(type)->getValue(section, name, index); } 256 257 void updateConfigValues() const; 258 void updateConfigValues(ConfigFileType type) const; 259 260 private: 261 ConfigFileManager(); 262 ConfigFileManager(const ConfigFileManager& other) {} 263 ~ConfigFileManager(); 264 265 ConfigFile* getFile(ConfigFileType type); 266 267 std::string getFilePath(const std::string& name) const; 268 269 std::map<ConfigFileType, ConfigFile*> configFiles_; 106 270 }; 107 271 }
Note: See TracChangeset
for help on using the changeset viewer.