Orxonox  0.0.5 Codename: Arcturus
ConfigValueContainer.h
Go to the documentation of this file.
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 
44 #ifndef _ConfigValueContainer_H__
45 #define _ConfigValueContainer_H__
46 
47 #include "core/CorePrereqs.h"
48 
49 #include <string>
50 #include <vector>
51 
52 #include "util/MultiType.h"
53 #include "core/class/Identifier.h"
54 
55 namespace orxonox
56 {
58  {
59  public:
60  virtual void call(void* object) = 0;
61  virtual inline ~ConfigValueCallbackBase() {}
62  };
63 
64  template <class T>
66  {
67  public:
68  inline ConfigValueCallback(void (T::*function) (void)) : function_(function) {}
69  virtual inline ~ConfigValueCallback() = default;
70  virtual inline void call(void* object) override
71  {
72  if (!IdentifierManager::getInstance().isCreatingHierarchy())
73  (static_cast<T*>(object)->*this->function_)();
74  }
75 
76  private:
77  void (T::*function_) (void);
78  };
79 
80 
99  {
100  public:
110  template <class D, class V>
111  ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const D& defvalue, const V& value)
112  {
113  this->init(type, identifier, sectionname, varname);
114  this->initValue(static_cast<V>(defvalue));
115  }
116 
126  template <class D, class V>
127  ConfigValueContainer(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname, const std::vector<D>& defvalue, const std::vector<V>& value)
128  {
129  this->init(type, identifier, sectionname, varname);
130 
131  this->value_ = V();
132  for (const D& defvalueElement : defvalue)
133  this->valueVector_.emplace_back(defvalueElement);
134 
135  this->initVector();
136  }
137 
139 
146  template <typename T, class C>
147  ConfigValueContainer& getValue(T* value, C* object)
148  {
149  if ((this->callback_ && object) || this->bContainerIsNew_)
150  {
151  T temp = *value;
152  this->value_.getValue(value);
153  if (this->bContainerIsNew_ || (*value) != temp)
154  {
155  this->bContainerIsNew_ = false;
156  if (this->callback_ && object)
157  this->callback_->call(object);
158  else
159  this->bDoInitialCallback_ = true;
160  }
161  }
162  else
163  {
164  this->value_.getValue(value);
165  }
166  return *this;
167  }
168 
175  template <typename T, class C>
176  ConfigValueContainer& getValue(std::vector<T>* value, C* object)
177  {
178  if ((this->callback_ && object) || this->bContainerIsNew_)
179  {
180  if (this->bContainerIsNew_)
181  this->bContainerIsNew_ = false;
182 
183  std::vector<T> temp = *value;
184  value->clear();
185  for (const MultiType& vectorEntry : this->valueVector_)
186  value->push_back(vectorEntry);
187 
188  if (value->size() != temp.size())
189  {
190  if (this->callback_ && object)
191  this->callback_->call(object);
192  else
193  this->bDoInitialCallback_ = true;
194  }
195  else
196  {
197  for (unsigned int i = 0; i < value->size(); ++i)
198  {
199  if ((*value)[i] != temp[i])
200  {
201  if (this->callback_ && object)
202  this->callback_->call(object);
203  else
204  this->bDoInitialCallback_ = true;
205  break;
206  }
207  }
208  }
209  }
210  else
211  {
212  value->clear();
213  for (const MultiType& vectorEntry : this->valueVector_)
214  value->push_back(vectorEntry);
215  }
216  return *this;
217  }
218 
220  inline const std::string& getName() const
221  { return this->varname_; }
223  inline const std::string& getSectionName() const
224  { return this->sectionname_; }
226  inline Identifier* getIdentifier() const
227  { return this->identifier_; }
229  inline bool isVector() const
230  { return this->bIsVector_; }
232  inline unsigned int getVectorSize() const
233  { return this->valueVector_.size(); }
234 
235  ConfigValueContainer& description(const std::string& description);
236  const std::string& getDescription() const;
237 
243  template <class T>
244  inline ConfigValueContainer& callback(T* object, void (T::*function) (void))
245  {
246  if (!this->callback_)
247  {
248  this->callback_ = new ConfigValueCallback<T>(function);
249 
250  if (this->bDoInitialCallback_)
251  {
252  this->bDoInitialCallback_ = false;
253  this->callback_->call(object);
254  }
255  }
256 
257  return (*this);
258  }
259 
260  bool set(const MultiType& input);
261  bool tset(const MultiType& input);
262 
263  bool set(unsigned int index, const MultiType& input);
264  bool tset(unsigned int index, const MultiType& input);
265  bool add(const MultiType& input);
266  bool remove(unsigned int index);
267 
268  bool reset();
269  void update();
270 
272  inline std::string toString() const
273  { return this->value_; }
275  inline std::string getTypename() const
276  { return this->value_.getTypename(); }
277 
278  private:
279  void init(ConfigFileType::Value type, Identifier* identifier, const std::string& sectionname, const std::string& varname);
280  void initValue(const MultiType& defvalue);
281  void initVector();
282  bool callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiType&), const std::string& input);
283 
284  bool bIsVector_;
285 
291  std::vector<std::string> defvalueStringVector_;
292 
294  std::vector<MultiType> valueVector_;
295 
299 
302  };
303 }
304 
305 #endif /* _ConfigValueContainer_H__ */
ConfigValueContainer & callback(T *object, void(T::*function)(void))
Adds a callback function, that gets called after getValue() if the newly assigned value differs from ...
Definition: ConfigValueContainer.h:244
The ConfigValuecontainer contains all needed information about a configurable variable.
Definition: ConfigValueContainer.h:98
std::string toString() const
Converts the config-value to a string.
Definition: ConfigValueContainer.h:272
virtual ~ConfigValueCallbackBase()
Definition: ConfigValueContainer.h:61
ConfigValueContainer(ConfigFileType::Value type, Identifier *identifier, const std::string &sectionname, const std::string &varname, const D &defvalue, const V &value)
Constructor: Converts the default-value to a string, checks the config-file for a changed value...
Definition: ConfigValueContainer.h:111
ConfigFileType::Value type_
The type of the corresponding config-file.
Definition: ConfigValueContainer.h:286
unsigned int getVectorSize() const
Returns the vectors size (or zero if it&#39;s not a vector).
Definition: ConfigValueContainer.h:232
Definition: InputPrereqs.h:90
MultiType value_
The value.
Definition: ConfigValueContainer.h:293
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
bool bAddedDescription_
True if a description was added.
Definition: ConfigValueContainer.h:296
std::vector< MultiType > valueVector_
A vector, containg the values in case we&#39;re storing a vector.
Definition: ConfigValueContainer.h:294
ConfigValueContainer & getValue(std::vector< T > *value, C *object)
Returns the configured vector.
Definition: ConfigValueContainer.h:176
std::vector< std::string > defvalueStringVector_
A vector, containg the strings of the default-values in case we&#39;re storing a vector.
Definition: ConfigValueContainer.h:291
std::string getTypename() const
Returns the typename of the assigned config-value.
Definition: ConfigValueContainer.h:275
virtual void call(void *object) override
Definition: ConfigValueContainer.h:70
Identifier * getIdentifier() const
Returns the associated identifier (can be nullptr).
Definition: ConfigValueContainer.h:226
ConfigValueContainer(ConfigFileType::Value type, Identifier *identifier, const std::string &sectionname, const std::string &varname, const std::vector< D > &defvalue, const std::vector< V > &value)
Constructor: Converts the default-value to a string, checks the config-file for a changed value...
Definition: ConfigValueContainer.h:127
Definition: InputPrereqs.h:104
Identifier * identifier_
The identifier of the class.
Definition: ConfigValueContainer.h:287
Value
Definition: CorePrereqs.h:113
std::string defvalueString_
The string of the default-value.
Definition: ConfigValueContainer.h:290
Declaration of Identifier, definition of ClassIdentifier<T>; used to identify the class of an object...
typedef void(ENET_CALLBACK *ENetPacketFreeCallback)(struct _ENetPacket *)
bool bIsVector_
True if the container contains a std::vector.
Definition: ConfigValueContainer.h:284
LanguageEntryLabel description_
The description.
Definition: ConfigValueContainer.h:297
bool bContainerIsNew_
True if it&#39;s the first time getValue() gets called.
Definition: ConfigValueContainer.h:300
ConfigValueCallback(void(T::*function)(void))
Definition: ConfigValueContainer.h:68
const std::string & getSectionName() const
Returns the name of the section this config value is in.
Definition: ConfigValueContainer.h:223
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Declaration of the MultiType and some helper constructs.
#define _CoreExport
Definition: CorePrereqs.h:61
Definition: ConfigValueContainer.h:57
Definition: ConfigValueContainer.h:65
The MultiType can hold a value of many possible types and convert them to other types.
Definition: MultiType.h:130
The Identifier is used to identify the class of an object and to store information about the class...
Definition: Identifier.h:109
Definition: InputPrereqs.h:105
Definition: InputPrereqs.h:78
bool isVector() const
Returns true if this config-value is a vector.
Definition: ConfigValueContainer.h:229
std::string sectionname_
The name of the class the variable belongs to.
Definition: ConfigValueContainer.h:288
ConfigValueContainer & getValue(T *value, C *object)
Returns the configured value.
Definition: ConfigValueContainer.h:147
virtual void call(void *object)=0
bool bDoInitialCallback_
True if the callback should be called as soon as it gets created.
Definition: ConfigValueContainer.h:301
std::string LanguageEntryLabel
Definition: CorePrereqs.h:141
static IdentifierManager & getInstance()
Returns a reference to the singleton instance.
Definition: Singleton.h:118
ConfigValueCallbackBase * callback_
A callback function to call after getValue if the value changed.
Definition: ConfigValueContainer.h:298
std::string varname_
The name of the variable.
Definition: ConfigValueContainer.h:289
const std::string & getName() const
Returns the name of this container.
Definition: ConfigValueContainer.h:220