Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestate/src/libraries/core/ConfigFileManager.h @ 6432

Last change on this file since 6432 was 6432, checked in by rgrieder, 14 years ago

Changed the way config values associated with general settings (ConfigFileType::Settings) are handled:

  • ConfigFileManager only handles config files listed in the ConfigFileType enum (normal enum again)
  • ConfigFileManager only takes care of ConfigFiles and returns a pointer to the right one, just two functions left. —> use like: ConfigFileManager::getInstance().getConfigFile(myType)→doSomething();
  • Moved all code (except for the argument completion functions) relating to ConfigFileType::Settings to a new class: SettingsConfigFile, which is a Singleton (it doesn't make sense to have multiple instances unless you start coding a lot more)
  • SettingsConfigFile handles config value containers according to their section and entry in the ini file, not according to class and variables names. (In most cases it will be class and variable names though)
  • SettingsConfigFile supports:
    • clear() (removes any file entries not associated to a config value container)
    • updateConfigValues() (does exactly that through the identifier)
    • config, tconfig and getConfig
    • commands listed above are exported to tolua, and tconfig, config and getConfig were given shortcuts in Lua (e.g. orxonox.config)
  • If you need to organise ConfigFiles yourself, just do it without the ConfigFileManager, like the KeyBinder does.
  • All getValue() functions have been split into getOrCreateValue() and getValue(), which is const
  • Removed obsolete config value management code in the Identifier (it still stores and destroys them and provides access to them)

All of that leads to one HUGE advantage:
"config OutputHandler softDebugLevelInGameConsole"
works now :D (any further implications are up to the reader…)
(it didn't work before because the actual config value container is in the InGameConsole singleton)

  • Property svn:eol-style set to native
File size: 15.0 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#ifndef _ConfigFileManager_H__
30#define _ConfigFileManager_H__
31
32#include "CorePrereqs.h"
33
34#include <list>
35#include <map>
36#include <set>
37#include <string>
38#include <boost/array.hpp>
39
40#include "util/Singleton.h"
41
42namespace orxonox // tolua_export
43{ // tolua_export
44
45    /////////////////////
46    // ConfigFileEntry //
47    /////////////////////
48    class _CoreExport ConfigFileEntry
49    {
50        public:
51            virtual ~ConfigFileEntry() {};
52            virtual void setValue(const std::string& value) = 0;
53            virtual const std::string& getValue() const = 0;
54            virtual const std::string& getName() const = 0;
55            virtual void setComment(const std::string& comment) = 0;
56            virtual unsigned int getIndex() const { return 0; }
57            virtual void setString(bool bString) = 0;
58            virtual const std::string& getFileEntry() const = 0;
59    };
60
61
62    //////////////////////////
63    // ConfigFileEntryValue //
64    //////////////////////////
65    class _CoreExport ConfigFileEntryValue : public ConfigFileEntry
66    {
67        public:
68            inline ConfigFileEntryValue(const std::string& name, const std::string& value = "", bool bString = false, const std::string& additionalComment = "")
69                : name_(name)
70                , value_(value)
71                , additionalComment_(additionalComment)
72                , bString_(bString)
73                { this->update(); }
74
75            inline virtual ~ConfigFileEntryValue() {}
76
77            inline virtual const std::string& getName() const
78                { return this->name_; }
79
80            inline virtual void setComment(const std::string& comment)
81                { this->additionalComment_ = comment; this->update(); }
82
83            inline virtual void setValue(const std::string& value)
84                { this->value_ = value; this->update(); }
85            inline virtual const std::string& getValue() const
86                { return this->value_; }
87
88            inline void virtual setString(bool bString)
89                { this->bString_ = bString; this->update(); }
90
91            inline virtual const std::string& getFileEntry() const
92                { return this->fileEntry_; }
93
94            inline virtual const std::string& getKeyString() const
95                { return this->name_; }
96
97        protected:
98            virtual void update();
99
100            const std::string name_;
101            std::string value_;
102            std::string additionalComment_;
103            std::string fileEntry_;
104            bool bString_;
105    };
106
107
108    ////////////////////////////////
109    // ConfigFileEntryVectorValue //
110    ////////////////////////////////
111    class _CoreExport ConfigFileEntryVectorValue : public ConfigFileEntryValue
112    {
113        public:
114            inline ConfigFileEntryVectorValue(const std::string& name, unsigned int index, const std::string& value = "", bool bString = false, const std::string& additionalComment = "")
115                : ConfigFileEntryValue(name, value, bString, additionalComment)
116                , index_(index)
117                { this->update(); /*No virtual calls in base class ctor*/ }
118
119            inline ~ConfigFileEntryVectorValue() {}
120
121            inline unsigned int getIndex() const
122                { return this->index_; }
123
124            inline const std::string& getKeyString() const
125                { return this->keyString_; }
126
127        private:
128            void update();
129
130            unsigned int index_;
131            std::string keyString_;
132    };
133
134
135    ////////////////////////////
136    // ConfigFileEntryComment //
137    ////////////////////////////
138    class _CoreExport ConfigFileEntryComment : public ConfigFileEntry
139    {
140        public:
141            inline ConfigFileEntryComment(const std::string& comment) : comment_(comment) {}
142            inline virtual ~ConfigFileEntryComment() {}
143
144            inline virtual const std::string& getName() const
145                { return this->comment_; }
146
147            inline virtual void setComment(const std::string& comment)
148                { this->comment_ = comment; }
149
150            inline virtual void setValue(const std::string& value)
151                {}
152            inline virtual const std::string& getValue() const
153                { return BLANKSTRING; }
154
155            inline void setString(bool bString)
156                {}
157
158            inline virtual const std::string& getFileEntry() const
159                { return this->comment_; }
160
161            inline virtual const std::string& getKeyString() const
162                { return BLANKSTRING; }
163
164        private:
165            std::string comment_;
166    };
167
168
169    ///////////////////////
170    // ConfigFileSection //
171    ///////////////////////
172    class _CoreExport ConfigFileSection
173    {
174        friend class ConfigFile;
175        friend class SettingsConfigFile;
176
177        public:
178            inline ConfigFileSection(const std::string& name, const std::string& additionalComment = "")
179                : name_(name)
180                , additionalComment_(additionalComment)
181                , bUpdated_(false)
182                {}
183            ~ConfigFileSection();
184
185            inline const std::string& getName() const
186                { return this->name_; }
187
188            inline void setComment(const std::string& comment)
189                { this->additionalComment_ = comment; }
190
191            inline void setValue(const std::string& name, const std::string& value, bool bString)
192                { this->getOrCreateEntry(name, value, bString)->setValue(value); }
193            inline const std::string& getValue(const std::string& name, const std::string& fallback, bool bString) const
194            {
195                ConfigFileEntry* entry = this->getEntry(name);
196                return (entry ? entry->getValue() : BLANKSTRING);
197            }
198            inline const std::string& getOrCreateValue(const std::string& name, const std::string& fallback, bool bString)
199                { return this->getOrCreateEntry(name, fallback, bString)->getValue(); }
200
201            inline void setValue(const std::string& name, unsigned int index, const std::string& value, bool bString)
202                { this->getOrCreateEntry(name, index, value, bString)->setValue(value); }
203            inline const std::string& getValue(const std::string& name, unsigned int index) const
204            {
205                ConfigFileEntry* entry = this->getEntry(name, index);
206                return (entry ? entry->getValue() : BLANKSTRING);
207            }
208            inline const std::string& getOrCreateValue(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
209                { return this->getOrCreateEntry(name, index, fallback, bString)->getValue(); }
210
211            void deleteVectorEntries(const std::string& name, unsigned int startindex = 0);
212            unsigned int getVectorSize(const std::string& name) const;
213
214            std::string getFileEntry() const;
215
216        private:
217            std::list<ConfigFileEntry*>& getEntries()
218                { return this->entries_; }
219            std::list<ConfigFileEntry*>::const_iterator getEntriesBegin() const
220                { return this->entries_.begin(); }
221            std::list<ConfigFileEntry*>::const_iterator getEntriesEnd() const
222                { return this->entries_.end(); }
223
224            std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name) const;
225            std::list<ConfigFileEntry*>::iterator       getOrCreateEntryIterator(const std::string& name, const std::string& fallback, bool bString);
226            std::list<ConfigFileEntry*>::const_iterator getEntryIterator(const std::string& name, unsigned int index) const;
227            std::list<ConfigFileEntry*>::iterator       getOrCreateEntryIterator(const std::string& name, unsigned int index, const std::string& fallback, bool bString);
228
229            inline ConfigFileEntry* getEntry(const std::string& name) const
230                { return (*this->getEntryIterator(name)); }
231            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, const std::string& fallback, bool bString)
232                { return (*this->getOrCreateEntryIterator(name, fallback, bString)); }
233            inline ConfigFileEntry* getEntry(const std::string& name, unsigned int index) const
234                { return (*this->getEntryIterator(name, index)); }
235            inline ConfigFileEntry* getOrCreateEntry(const std::string& name, unsigned int index, const std::string& fallback, bool bString)
236                { return (*this->getOrCreateEntryIterator(name, index, fallback, bString)); }
237
238            std::string name_;
239            std::string additionalComment_;
240            std::list<ConfigFileEntry*> entries_;
241            bool bUpdated_;
242    };
243
244
245    ////////////////
246    // ConfigFile //
247    ////////////////
248    class _CoreExport ConfigFile
249    {
250        public:
251            ConfigFile(const std::string& filename);
252            virtual ~ConfigFile();
253
254            virtual void load();
255            virtual void save() const;
256            virtual void saveAs(const std::string& filename) const;
257            virtual void clear();
258
259            inline const std::string& getFilename()
260                { return this->filename_; }
261
262            inline void setValue(const std::string& section, const std::string& name, const std::string& value, bool bString)
263            {
264                this->getOrCreateSection(section)->setValue(name, value, bString);
265                this->save();
266            }
267            inline const std::string& getValue(const std::string& section, const std::string& name, bool bString) const
268            {
269                ConfigFileSection* sectionPtr = this->getSection(section);
270                return (sectionPtr ? sectionPtr->getValue(name, bString) : BLANKSTRING);
271            }
272            const std::string& getOrCreateValue(const std::string& section, const std::string& name, const std::string& fallback, bool bString);
273
274            inline void setValue(const std::string& section, const std::string& name, unsigned int index, const std::string& value, bool bString)
275            {
276                this->getSection(section)->setValue(name, index, value, bString);
277                this->save();
278            }
279            inline const std::string& getValue(const std::string& section, const std::string& name, unsigned int index) const
280            {
281                ConfigFileSection* sectionPtr = this->getSection(section);
282                return (sectionPtr ? sectionPtr->getValue(name, index) : BLANKSTRING);
283            }
284            const std::string& getOrCreateValue(const std::string& section, const std::string& name, unsigned int index, const std::string& fallback, bool bString);
285
286            void deleteVectorEntries(const std::string& section, const std::string& name, unsigned int startindex = 0);
287            inline unsigned int getVectorSize(const std::string& section, const std::string& name) const
288            {
289                ConfigFileSection* sectionPtr = this->getSection(section);
290                return (sectionPtr ? sectionPtr->getVectorSize(name) : 0);
291            }
292
293        protected:
294            ConfigFileSection* getSection(const std::string& section) const;
295            ConfigFileSection* getOrCreateSection(const std::string& section);
296
297            std::list<ConfigFileSection*> sections_;
298
299        private:
300            void saveIfUpdated();
301            const std::string filename_;
302            bool bUpdated_;
303    };
304
305
306    ////////////////////////
307    // SettingsConfigFile //
308    ////////////////////////
309    class _CoreExport SettingsConfigFile // tolua_export
310        : public ConfigFile, public Singleton<SettingsConfigFile>
311    { // tolua_export
312        friend class Singleton<SettingsConfigFile>;
313
314        public:
315            typedef std::multimap<std::string, std::pair<std::string, ConfigValueContainer*> > ContainerMap;
316
317            SettingsConfigFile(const std::string& filename);
318            ~SettingsConfigFile();
319
320            void load(); // tolua_export
321            void setFilename(const std::string& filename); // tolua_export
322            void clean(bool bCleanComments = false); // tolua_export
323
324            bool config(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
325            bool tconfig(const std::string& section, const std::string& entry, const std::string& value); // tolua_export
326            std::string getConfig(const std::string& section, const std::string& entry); // tolua_export
327
328            void addConfigValueContainer(ConfigValueContainer* container);
329            void removeConfigValueContainer(ConfigValueContainer* container);
330
331            inline const std::set<std::string>& getSectionNames()
332                { return this->sectionNames_; }
333            inline ContainerMap::const_iterator getContainerLowerBound(const std::string section)
334                { return this->containers_.lower_bound(section); }
335            inline ContainerMap::const_iterator getContainerUpperBound(const std::string section)
336                { return this->containers_.upper_bound(section); }
337
338            static SettingsConfigFile& getInstance() { return Singleton<SettingsConfigFile>::getInstance(); } // tolua_export
339
340        private:
341            void updateConfigValues();
342            bool configImpl(const std::string& section, const std::string& entry, const std::string& value, bool (ConfigValueContainer::*function)(const MultiType&));
343
344            ContainerMap containers_;
345            std::set<std::string> sectionNames_;
346            static SettingsConfigFile* singletonPtr_s;
347    }; // tolua_export
348
349
350    ///////////////////////
351    // ConfigFileManager //
352    ///////////////////////
353    class _CoreExport ConfigFileManager : public Singleton<ConfigFileManager>
354    {
355        friend class Singleton<ConfigFileManager>;
356        public:
357            ConfigFileManager();
358            ~ConfigFileManager();
359
360            void setFilename(ConfigFileType::Value type, const std::string& filename);
361
362            inline ConfigFile* getConfigFile(ConfigFileType::Value type)
363            {
364                // Check array bounds
365                return configFiles_.at(type);
366            }
367
368        private:
369            ConfigFileManager(const ConfigFileManager&);
370
371            boost::array<ConfigFile*, 3> configFiles_;
372            static ConfigFileManager* singletonPtr_s;
373    };
374} // tolua_export
375
376#endif /* _ConfigFileManager_H__ */
Note: See TracBrowser for help on using the repository browser.