Changeset 1747 for code/trunk/src/core/ConfigValueContainer.h
- Timestamp:
- Sep 9, 2008, 4:25:52 AM (17 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/core3 (added) merged: 1573-1574,1583-1586,1591-1594,1596-1597,1603,1606-1607,1610-1611,1655,1658,1676-1679,1681-1685,1687,1716-1723,1725-1729,1736
- Property svn:mergeinfo changed
-
code/trunk/src/core/ConfigValueContainer.h
r1505 r1747 50 50 51 51 #include "util/Math.h" 52 #include "util/MultiType Math.h"52 #include "util/MultiType.h" 53 53 #include "ConfigFileManager.h" 54 54 55 55 namespace orxonox 56 56 { 57 class ConfigValueCallbackBase 58 { 59 public: 60 virtual void call(void* object) = 0; 61 inline virtual ~ConfigValueCallbackBase() {} 62 }; 63 64 template <class T> 65 class ConfigValueCallback: public ConfigValueCallbackBase 66 { 67 public: 68 inline ConfigValueCallback(void (T::*function) (void)) : function_(function) {} 69 inline virtual ~ConfigValueCallback() {} 70 inline virtual void call(void* object) 71 { (((T*)object)->*this->function_)(); } 72 73 private: 74 void (T::*function_) (void); 75 }; 76 77 57 78 //! The ConfigValuecontainer contains all needed informations about a configurable variable. 58 79 /** … … 74 95 { 75 96 public: 76 ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const MultiTypeMath& defvalue); 77 ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<MultiTypeMath>& defvalue); 78 79 /** @brief Returns the configured value. @param value This is only needed to determine the right type. @return The value */ 80 template <typename T> 81 inline ConfigValueContainer& getValue(T* value) 82 { this->value_.getValue(value); return *this; } 83 template <typename T> 84 inline ConfigValueContainer& getValue(std::vector<T>* value) 85 { 86 value->clear(); 87 for (unsigned int i = 0; i < this->valueVector_.size(); i++) 88 value->push_back(this->valueVector_[i]); 97 /** 98 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. 99 @param type The type of the corresponding config-file 100 @param identifier The identifier of the class the variable belongs to 101 @param varname The name of the variable 102 @param defvalue The default-value 103 @param value Only needed do determine the right type. 104 */ 105 template <class D, class V> 106 ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const D& defvalue, const V& value) 107 { 108 this->init(type, identifier, varname); 109 this->initValue((V)defvalue); 110 } 111 112 /** 113 @brief Constructor: Converts the default-value to a string, checks the config-file for a changed value, sets the intern value variable. 114 @param type The type of the corresponding config-file 115 @param identifier The identifier of the class the variable belongs to 116 @param varname The name of the variable 117 @param defvalue The default-value 118 */ 119 template <class V> 120 ConfigValueContainer(ConfigFileType type, Identifier* identifier, const std::string& varname, const std::vector<V>& defvalue) 121 { 122 this->init(type, identifier, varname); 123 124 this->value_ = V(); 125 for (unsigned int i = 0; i < defvalue.size(); i++) 126 this->valueVector_.push_back(MultiType(defvalue[i])); 127 128 this->initVector(); 129 } 130 131 ~ConfigValueContainer(); 132 133 /** 134 @brief Returns the configured value. 135 @param value A pointer to the variable to store the value. 136 @param object The object calling this function 137 @return The ConfigValueContainer 138 */ 139 template <typename T, class C> 140 ConfigValueContainer& getValue(T* value, C* object) 141 { 142 if ((this->callback_ && object) || this->bContainerIsNew_) 143 { 144 if (this->bContainerIsNew_) 145 this->bContainerIsNew_ = false; 146 147 T temp = *value; 148 this->value_.getValue(value); 149 if ((*value) != temp) 150 { 151 if (this->callback_ && object) 152 this->callback_->call(object); 153 else 154 this->bDoInitialCallback_ = true; 155 } 156 } 157 else 158 { 159 this->value_.getValue(value); 160 } 89 161 return *this; 90 162 } 91 163 92 template <typename T> 93 inline void setVectorType(const std::vector<T>& value) 94 { 95 this->value_ = T(); 96 this->update(); 97 } 98 164 /** 165 @brief Returns the configured vector. 166 @param value A pointer to the vector to store the values. 167 @param object The object calling this function 168 @return The ConfigValueContainer 169 */ 170 template <typename T, class C> 171 ConfigValueContainer& getValue(std::vector<T>* value, C* object) 172 { 173 if ((this->callback_ && object) || this->bContainerIsNew_) 174 { 175 if (this->bContainerIsNew_) 176 this->bContainerIsNew_ = false; 177 178 std::vector<T> temp = *value; 179 value->clear(); 180 for (unsigned int i = 0; i < this->valueVector_.size(); ++i) 181 value->push_back(this->valueVector_[i]); 182 183 if (value->size() != temp.size()) 184 { 185 if (this->callback_ && object) 186 this->callback_->call(object); 187 else 188 this->bDoInitialCallback_ = true; 189 } 190 else 191 { 192 for (unsigned int i = 0; i < value->size(); ++i) 193 { 194 if ((*value)[i] != temp[i]) 195 { 196 if (this->callback_ && object) 197 this->callback_->call(object); 198 else 199 this->bDoInitialCallback_ = true; 200 break; 201 } 202 } 203 } 204 } 205 else 206 { 207 value->clear(); 208 for (unsigned int i = 0; i < this->valueVector_.size(); ++i) 209 value->push_back(this->valueVector_[i]); 210 } 211 return *this; 212 } 213 214 /** @brief Returns the name of this container. */ 99 215 inline const std::string& getName() const 100 216 { return this->varname_; } 217 /** @brief Returns true if this config-value is a vector */ 101 218 inline bool isVector() const 102 219 { return this->bIsVector_; } 220 /** @brief Returns the vectors size (or zero if it's not a vector). */ 103 221 inline unsigned int getVectorSize() const 104 222 { return this->valueVector_.size(); } 105 223 106 voiddescription(const std::string& description);224 ConfigValueContainer& description(const std::string& description); 107 225 const std::string& getDescription() const; 108 226 109 bool set(const MultiTypeMath& input); 110 bool tset(const MultiTypeMath& input); 111 112 bool set(unsigned int index, const MultiTypeMath& input); 113 bool tset(unsigned int index, const MultiTypeMath& input); 114 bool add(const MultiTypeMath& input); 227 /** 228 @brief Adds a callback function, that gets called after getValue() if the newly assigned value differs from the old value of the variable. 229 @param object The object to call the function 230 @param function The callback function 231 */ 232 template <class T> 233 inline ConfigValueContainer& callback(T* object, void (T::*function) (void)) 234 { 235 if (!this->callback_) 236 { 237 this->callback_ = new ConfigValueCallback<T>(function); 238 239 if (this->bDoInitialCallback_) 240 { 241 this->bDoInitialCallback_ = false; 242 this->callback_->call(object); 243 } 244 } 245 246 return (*this); 247 } 248 249 bool set(const MultiType& input); 250 bool tset(const MultiType& input); 251 252 bool set(unsigned int index, const MultiType& input); 253 bool tset(unsigned int index, const MultiType& input); 254 bool add(const MultiType& input); 115 255 bool remove(unsigned int index); 116 256 … … 120 260 /** @brief Converts the config-value to a string. @return The string */ 121 261 inline std::string toString() const 122 { return this->value_ .toString(); }262 { return this->value_; } 123 263 /** @brief Returns the typename of the assigned config-value. @return The typename */ 124 264 inline std::string getTypename() const … … 126 266 127 267 private: 128 bool callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiTypeMath&), const std::string& input); 268 void init(ConfigFileType type, Identifier* identifier, const std::string& varname); 269 void initValue(const MultiType& defvalue); 270 void initVector(); 271 bool callFunctionWithIndex(bool (ConfigValueContainer::* function) (unsigned int, const MultiType&), const std::string& input); 129 272 130 273 bool bIsVector_; //!< True if the container contains a std::vector … … 137 280 std::vector<std::string> defvalueStringVector_; //!< A vector, containg the strings of the default-values in case we're storing a vector 138 281 139 MultiType Mathvalue_; //!< The value140 std::vector<MultiType Math>valueVector_; //!< A vector, containg the values in case we're storing a vector282 MultiType value_; //!< The value 283 std::vector<MultiType> valueVector_; //!< A vector, containg the values in case we're storing a vector 141 284 142 285 bool bAddedDescription_; //!< True if a description was added 143 286 LanguageEntryLabel description_; //!< The description 287 ConfigValueCallbackBase* callback_; //!< A callback function to call after getValue if the value changed 288 289 bool bContainerIsNew_; //!< True if it's the first time getValue() gets called 290 bool bDoInitialCallback_; //!< True if the callback should be called as soon as it gets created 144 291 }; 145 292 }
Note: See TracChangeset
for help on using the changeset viewer.