Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/ConfigValueContainer.h @ 7297

Last change on this file since 7297 was 7297, checked in by landauf, 14 years ago

fixed lots of Doxygen warnings

Note: Doxygen prints a warning if only a part of the parameters of a function are documented.

Added documentation for missing parameters (as good as I could), removed documentation of obsolete parameters and fixed names of renamed parameters.
Some parameters are tagged with "FIXME", please replace this with an appropriate documentation if you know what it does.

  • Property svn:eol-style set to native
File size: 12.9 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/**
30    @file
31    @brief Definition of the ConfigValueContainer class.
32
33    The ConfigValueContainer class contains all needed information about a configurable variable:
34     - the name of the variable
35     - the name of the class the variable belongs to
36     - the default value
37     - the user-specified value
38     - a pointer to the entry in the config-file
39
40    This is needed to assign the configured values to all newly created objects.
41*/
42
43#ifndef _ConfigValueContainer_H__
44#define _ConfigValueContainer_H__
45
46#include "CorePrereqs.h"
47
48#include <string>
49#include <vector>
50
51#include "util/MultiType.h"
52#include "Identifier.h"
53
54namespace orxonox
55{
56    class ConfigValueCallbackBase
57    {
58        public:
59            virtual void call(void* object) = 0;
60            inline virtual ~ConfigValueCallbackBase() {}
61    };
62
63    template <class T>
64    class ConfigValueCallback: public ConfigValueCallbackBase
65    {
66        public:
67            inline ConfigValueCallback(void (T::*function) (void)) : function_(function) {}
68            inline virtual ~ConfigValueCallback() {}
69            inline virtual void call(void* object)
70            {
71                if (!Identifier::isCreatingHierarchy())
72                    (static_cast<T*>(object)->*this->function_)();
73            }
74
75        private:
76            void (T::*function_) (void);
77    };
78
79
80    //! The ConfigValuecontainer contains all needed information about a configurable variable.
81    /**
82        The ConfigValueContainer class contains all needed information about a configurable variable:
83         - the name of the variable
84         - the name of the class the variable belongs to
85         - the default value
86         - the user-specified value
87         - a pointer to the entry in the config-file
88
89        This is needed to assign the configured values to all newly created objects.
90
91        The container searches for the entry in the config file.
92        If there is an entry, it parses the specified value and assigns it to the variable of the right type.
93        If there is no entry, it adds the entry with the default-value to the section of the variables class.
94        If there is no section, the section and the entry are added to the end of the config-file.
95    */
96    class _CoreExport ConfigValueContainer
97    {
98        public:
99            /**
100                @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
101                @param type The type of the corresponding config-file
102                @param identifier The identifier of the class the variable belongs to
103                @param sectionname Name of the section the configValue should be put in.
104                @param varname The name of the variable
105                @param defvalue The default-value
106                @param value Only needed do determine the right type.
107            */
108            template <class D, class V>
109            ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const D& defvalue, const V& value)
110            {
111                this->init(type, identifier, sectionname, varname);
112                this->initValue(static_cast<V>(defvalue));
113            }
114
115            /**
116                @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable.
117                @param type The type of the corresponding config-file
118                @param identifier The identifier of the class the variable belongs to
119                @param sectionname Name of the section the configValue should be put in.
120                @param varname The name of the variable
121                @param defvalue The default-value
122                @param value Only needed do determine the right type.
123            */
124            template <class D, class V>
125            ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value)
126            {
127                this->init(type, identifier, sectionname, varname);
128
129                this->value_ = V();
130                for (unsigned int i = 0; i < defvalue.size(); i++)
131                    this->valueVector_.push_back(MultiType(defvalue[i]));
132
133                this->initVector();
134            }
135
136            ~ConfigValueContainer();
137
138            /**
139                @brief Returns the configured value.
140                @param value A pointer to the variable to store the value.
141                @param object The object calling this function
142                @return The ConfigValueContainer
143            */
144            template <typename T, class C>
145            ConfigValueContainer& getValue(T* value, C* object)
146            {
147                if ((this->callback_ && object) || this->bContainerIsNew_)
148                {
149                    T temp = *value;
150                    this->value_.getValue(value);
151                    if (this->bContainerIsNew_ || (*value) != temp)
152                    {
153                        this->bContainerIsNew_ = false;
154                        if (this->callback_ && object)
155                            this->callback_->call(object);
156                        else
157                            this->bDoInitialCallback_ = true;
158                    }
159                }
160                else
161                {
162                    this->value_.getValue(value);
163                }
164                return *this;
165            }
166
167            /**
168                @brief Returns the configured vector.
169                @param value A pointer to the vector to store the values.
170                @param object The object calling this function
171                @return The ConfigValueContainer
172            */
173            template <typename T, class C>
174            ConfigValueContainer& getValue(std::vector<T>* value, C* object)
175            {
176                if ((this->callback_ && object) || this->bContainerIsNew_)
177                {
178                    if (this->bContainerIsNew_)
179                        this->bContainerIsNew_ = false;
180
181                    std::vector<T> temp = *value;
182                    value->clear();
183                    for (unsigned int i = 0; i < this->valueVector_.size(); ++i)
184                        value->push_back(this->valueVector_[i]);
185
186                    if (value->size() != temp.size())
187                    {
188                        if (this->callback_ && object)
189                            this->callback_->call(object);
190                        else
191                            this->bDoInitialCallback_ = true;
192                    }
193                    else
194                    {
195                        for (unsigned int i = 0; i < value->size(); ++i)
196                        {
197                            if ((*value)[i] != temp[i])
198                            {
199                                if (this->callback_ && object)
200                                    this->callback_->call(object);
201                                else
202                                    this->bDoInitialCallback_ = true;
203                                break;
204                            }
205                        }
206                    }
207                }
208                else
209                {
210                    value->clear();
211                    for (unsigned int i = 0; i < this->valueVector_.size(); ++i)
212                        value->push_back(this->valueVector_[i]);
213                }
214                return *this;
215            }
216
217            /** @brief Returns the name of this container. */
218            inline const std::string& getName() const
219                { return this->varname_; }
220            /** @brief Returns the name of the section this config value is in. */
221            inline const std::string& getSectionName() const
222                { return this->sectionname_; }
223            /** @brief Returns the associated identifier (can be NULL). */
224            inline Identifier* getIdentifier() const
225                { return this->identifier_; }
226            /** @brief Returns true if this config-value is a vector */
227            inline bool isVector() const
228                { return this->bIsVector_; }
229            /** @brief Returns the vectors size (or zero if it's not a vector). */
230            inline unsigned int getVectorSize() const
231                { return this->valueVector_.size(); }
232
233            ConfigValueContainer& description(const std::string& description);
234            const std::string& getDescription() const;
235
236            /**
237                @brief Adds a callback function, that gets called after getValue() if the newly assigned value differs from the old value of the variable.
238                @param object The object to call the function
239                @param function The callback function
240            */
241            template <class T>
242            inline ConfigValueContainer& callback(T* object, void (T::*function) (void))
243            {
244                if (!this->callback_)
245                {
246                    this->callback_ = new ConfigValueCallback<T>(function);
247
248                    if (this->bDoInitialCallback_)
249                    {
250                        this->bDoInitialCallback_ = false;
251                        this->callback_->call(object);
252                    }
253                }
254
255                return (*this);
256            }
257
258            bool set(const MultiType& input);
259            bool tset(const MultiType& input);
260
261            bool set(unsigned int index, const MultiType& input);
262            bool tset(unsigned int index, const MultiType& input);
263            bool add(const MultiType& input);
264            bool remove(unsigned int index);
265
266            bool reset();
267            void update();
268
269            /** @brief Converts the config-value to a string. @return The string */
270            inline std::string toString() const
271                { return this->value_; }
272            /** @brief Returns the typename of the assigned config-value. @return The typename */
273            inline std::string getTypename() const
274                { return this->value_.getTypename(); }
275
276        private:
277            void init(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname);
278            void initValue(const MultiType& defvalue);
279            void initVector();
280            bool callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiType&), const std::string& input);
281
282            bool                       bIsVector_;                  //!< True if the container contains a std::vector
283
284            ConfigFileType::Value      type_;                       //!< The type of the corresponding config-file
285            Identifier*                identifier_;                 //!< The identifier of the class
286            std::string                sectionname_;                //!< The name of the class the variable belongs to
287            std::string                varname_;                    //!< The name of the variable
288            std::string                defvalueString_;             //!< The string of the default-value
289            std::vector<std::string>   defvalueStringVector_;       //!< A vector, containg the strings of the default-values in case we're storing a vector
290
291            MultiType                  value_;                      //!< The value
292            std::vector<MultiType>     valueVector_;                //!< A vector, containg the values in case we're storing a vector
293
294            bool                       bAddedDescription_;          //!< True if a description was added
295            LanguageEntryLabel         description_;                //!< The description
296            ConfigValueCallbackBase*   callback_;                   //!< A callback function to call after getValue if the value changed
297
298            bool                       bContainerIsNew_;            //!< True if it's the first time getValue() gets called
299            bool                       bDoInitialCallback_;         //!< True if the callback should be called as soon as it gets created
300    };
301}
302
303#endif /* _ConfigValueContainer_H__ */
Note: See TracBrowser for help on using the repository browser.