[4592] | 1 | /* |
---|
[4250] | 2 | orxonox - the future of 3D-vertical-scrollers |
---|
[4233] | 3 | |
---|
[4250] | 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
| 14 | */ |
---|
| 15 | |
---|
[4592] | 16 | /*! |
---|
[5039] | 17 | * @file load_param.h |
---|
[5129] | 18 | * A Class and macro-functions, that makes our lives easy to load-in parameters |
---|
| 19 | */ |
---|
[4250] | 20 | |
---|
[4233] | 21 | #ifndef _LOAD_PARAM_H |
---|
| 22 | #define _LOAD_PARAM_H |
---|
| 23 | |
---|
[5332] | 24 | #include "functor_list.h" |
---|
[5129] | 25 | |
---|
[5549] | 26 | #include "debug.h" |
---|
| 27 | |
---|
[4239] | 28 | #include "factory.h" |
---|
[4241] | 29 | #include "substring.h" |
---|
[4598] | 30 | #include "tinyxml.h" |
---|
[5645] | 31 | #include "executor/executor.h" |
---|
[5549] | 32 | |
---|
[5141] | 33 | #include "helper_functions.h" |
---|
[4233] | 34 | |
---|
[4251] | 35 | // Forward Declaration // |
---|
| 36 | template<class T> class tList; |
---|
[5546] | 37 | class LoadClassDescription; |
---|
| 38 | class LoadParamDescription; |
---|
[5556] | 39 | class MultiType; |
---|
[4251] | 40 | |
---|
[5646] | 41 | |
---|
| 42 | /** |
---|
| 43 | * Loads a Parameter from ROOT named PARAMETER_NAME |
---|
| 44 | * onto OBJECT of CLASS, trough the FUNCTION |
---|
| 45 | * @param ROOT the TiXmlElement to load the Parameter from |
---|
| 46 | * @param PARAMETER_NAME the Name of the Parameter to load |
---|
| 47 | * @param OBJECT The BaseObject to load the new setting to. |
---|
| 48 | * @param CLASS What Class the BaseObejct is of (this is for identifying the Functuon) |
---|
| 49 | * @param FUNCTION The function of Class to Load (if you want to call &CLASS::FUNCTION write FUNCTION here). |
---|
| 50 | */ |
---|
| 51 | #define LoadParamNEW(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
| 52 | LoadParamBase(ROOT, PARAMETER_NAME, OBJECT, ExecutorObjective<CLASS>(&CLASS::FUNCTION)) |
---|
| 53 | |
---|
| 54 | |
---|
[5332] | 55 | /************************** |
---|
| 56 | **** REAL DECLARATIONS **** |
---|
| 57 | **************************/ |
---|
| 58 | //! abstract Base class for a Loadable parameter |
---|
[5545] | 59 | class LoadParamBase : public BaseObject |
---|
[5332] | 60 | { |
---|
[5645] | 61 | public: |
---|
| 62 | LoadParamBase(const TiXmlElement* root, const char* paramName, BaseObject* object, const Executor& exeutor); |
---|
| 63 | ~LoadParamBase(); |
---|
[5332] | 64 | |
---|
[5645] | 65 | LoadParamBase* describe(const char* descriptionText); |
---|
| 66 | LoadParamBase* defaultValues(unsigned int count, ...); |
---|
[5332] | 67 | |
---|
[5645] | 68 | protected: |
---|
| 69 | LoadParamBase(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...); |
---|
[5556] | 70 | |
---|
[5645] | 71 | protected: |
---|
| 72 | Executor* executor; |
---|
[5646] | 73 | BaseObject* object; |
---|
[5645] | 74 | |
---|
| 75 | LoadClassDescription* classDesc; //!< The LoadClassDescription of this LoadParameter |
---|
| 76 | LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter |
---|
| 77 | const char* loadString; //!< The string loaded by this LoadParam |
---|
| 78 | const void* pointerToParam; //!< A Pointer to a Parameter. |
---|
| 79 | |
---|
| 80 | MultiType* defaultValue; |
---|
[5332] | 81 | }; |
---|
| 82 | |
---|
| 83 | |
---|
[4495] | 84 | //! macro that makes it even more easy to load a Parameter |
---|
[4249] | 85 | /** |
---|
[4834] | 86 | * @param className the name of the class to load |
---|
| 87 | * @param parameterName the name of the parameter to load as written in the XML-file |
---|
| 88 | * @param function the function to call |
---|
| 89 | */ |
---|
[4495] | 90 | #define LOAD_PARAM(className, parameterName, paramFunction) \ |
---|
| 91 | LoadParam<className>(root, #parameterName, this, &className::paramFunction) |
---|
| 92 | |
---|
| 93 | /** |
---|
[4834] | 94 | * this Starts a Cycle in the Loading Process |
---|
| 95 | * be aware, that in the cycle the first parameter of load_param should because |
---|
| 96 | * called element, and that you must say true at the Fith parameter, or it will fail |
---|
| 97 | * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE |
---|
[5645] | 98 | * |
---|
[5644] | 99 | * @param ROOT The root XLM-element to search element under. |
---|
| 100 | * @param ELEMENT the element to search |
---|
[4834] | 101 | */ |
---|
[5644] | 102 | #define LOAD_PARAM_START_CYCLE(ROOT, ELEMENT) \ |
---|
| 103 | const TiXmlElement* ELEMENT; \ |
---|
| 104 | ELEMENT= ROOT->FirstChildElement(); \ |
---|
| 105 | while( ELEMENT != NULL) \ |
---|
| 106 | { |
---|
[4834] | 107 | /** |
---|
[5644] | 108 | * closes a LoadParam Loop |
---|
| 109 | * @see LOAD_PARAM_START_CYCLE |
---|
| 110 | * @param ELEMENT the Element to step through. |
---|
[4834] | 111 | */ |
---|
[5644] | 112 | #define LOAD_PARAM_END_CYCLE(ELEMENT) \ |
---|
| 113 | ELEMENT = ELEMENT->NextSiblingElement(); \ |
---|
| 114 | } |
---|
[4834] | 115 | |
---|
| 116 | |
---|
[4623] | 117 | /***************************************** |
---|
| 118 | **** MACROS DEFINITIONS OF LOADABLES ***** |
---|
| 119 | *****************************************/ |
---|
[5137] | 120 | // 0. TYPES |
---|
| 121 | /** |
---|
[5332] | 122 | * a Macro to easily implement many different Constructors for the LoadParam-Class with no argument |
---|
[5137] | 123 | */ |
---|
| 124 | #define LoadParam0() \ |
---|
| 125 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false) \ |
---|
[5556] | 126 | : LoadParamBase(root, pt2Object, paramName, 0, multi, NULL) \ |
---|
[5137] | 127 | { \ |
---|
| 128 | if (loadString != NULL && root != NULL) \ |
---|
| 129 | (*pt2Object.*function)(); \ |
---|
| 130 | else \ |
---|
| 131 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName());\ |
---|
| 132 | } |
---|
| 133 | |
---|
[4487] | 134 | // 1. TYPE |
---|
[4249] | 135 | /** |
---|
[5332] | 136 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument |
---|
[4836] | 137 | * @param type1 The type of the first functionParameter |
---|
[5332] | 138 | */ |
---|
[4249] | 139 | #define LoadParam1(type1) \ |
---|
[4623] | 140 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \ |
---|
| 141 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \ |
---|
[5545] | 142 | : LoadParamBase(root, pt2Object, paramName, 1, multi, NULL, type1##_PARAM, default1) \ |
---|
[5332] | 143 | { \ |
---|
[4252] | 144 | if (loadString != NULL && root != NULL) \ |
---|
[4624] | 145 | (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \ |
---|
[4249] | 146 | else \ |
---|
[4592] | 147 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[5332] | 148 | } |
---|
[4241] | 149 | |
---|
[4249] | 150 | // 2. TYPES |
---|
[4487] | 151 | /** |
---|
[5332] | 152 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments |
---|
[4836] | 153 | * @param type1 The type of the first functionParameter |
---|
| 154 | * @param type2 The type of the second functionParameter |
---|
[5499] | 155 | * |
---|
| 156 | * @TODO DEFAULT VALUES HACK |
---|
[5332] | 157 | */ |
---|
[4250] | 158 | #define LoadParam2(type1, type2) \ |
---|
[4623] | 159 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \ |
---|
| 160 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT) \ |
---|
[5545] | 161 | : LoadParamBase(root, pt2Object, paramName, 2, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2) \ |
---|
[5332] | 162 | { \ |
---|
[4252] | 163 | if (loadString != NULL && root != NULL) \ |
---|
[5332] | 164 | { \ |
---|
[4592] | 165 | SubString subLoads(loadString); \ |
---|
[5499] | 166 | if (subLoads.getCount() >= 1) \ |
---|
[4624] | 167 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \ |
---|
[4592] | 168 | else \ |
---|
| 169 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 170 | paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \ |
---|
[5332] | 171 | } \ |
---|
[4249] | 172 | else \ |
---|
[4592] | 173 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[5332] | 174 | } |
---|
[4241] | 175 | |
---|
[4243] | 176 | |
---|
[4249] | 177 | // 3. TYPES |
---|
[4487] | 178 | /** |
---|
[5332] | 179 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments |
---|
[4836] | 180 | * @param type1 The type of the first functionParameter |
---|
| 181 | * @param type2 The type of the second functionParameter |
---|
| 182 | * @param type3 The type of the third functionParameter |
---|
[5332] | 183 | */ |
---|
[4250] | 184 | #define LoadParam3(type1, type2, type3) \ |
---|
[4623] | 185 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \ |
---|
| 186 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\ |
---|
[5545] | 187 | : LoadParamBase(root, pt2Object, paramName, 3, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3) \ |
---|
[5332] | 188 | { \ |
---|
[4252] | 189 | if (loadString != NULL && root != NULL) \ |
---|
[5332] | 190 | { \ |
---|
[4592] | 191 | SubString subLoads(loadString); \ |
---|
| 192 | if (subLoads.getCount() == 3) \ |
---|
[4624] | 193 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \ |
---|
[4592] | 194 | else \ |
---|
| 195 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 196 | paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \ |
---|
[5332] | 197 | } \ |
---|
[4249] | 198 | else \ |
---|
[4592] | 199 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[5332] | 200 | } |
---|
[4241] | 201 | |
---|
[4249] | 202 | |
---|
| 203 | // 4. TYPES |
---|
[4487] | 204 | /** |
---|
[5332] | 205 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments |
---|
[4836] | 206 | * @param type1 The type of the first functionParameter |
---|
| 207 | * @param type2 The type of the second functionParameter |
---|
| 208 | * @param type3 The type of the third functionParameter |
---|
| 209 | * @param type4 The type of the forth functionParameter |
---|
[5332] | 210 | */ |
---|
[4250] | 211 | #define LoadParam4(type1, type2, type3, type4) \ |
---|
[4623] | 212 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \ |
---|
| 213 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
| 214 | type4##_TYPE default4 = type4##_DEFAULT) \ |
---|
[5545] | 215 | : LoadParamBase(root, pt2Object, paramName, 4, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \ |
---|
[5332] | 216 | type4##_PARAM, default4) \ |
---|
| 217 | { \ |
---|
[4252] | 218 | if (loadString != NULL && root != NULL) \ |
---|
[5332] | 219 | { \ |
---|
[4592] | 220 | SubString subLoads(loadString); \ |
---|
| 221 | if (subLoads.getCount() == 4) \ |
---|
[4624] | 222 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3), type4##_FUNC(subLoads.getString(3), default4)); \ |
---|
[4592] | 223 | else \ |
---|
| 224 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 225 | paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \ |
---|
[5332] | 226 | } \ |
---|
[4249] | 227 | else \ |
---|
[4592] | 228 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[5332] | 229 | } |
---|
[4241] | 230 | |
---|
[4250] | 231 | |
---|
| 232 | // 5. TYPES |
---|
[4487] | 233 | /** |
---|
[5332] | 234 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments |
---|
[4836] | 235 | * @param type1 The type of the first functionParameter |
---|
| 236 | * @param type2 The type of the second functionParameter |
---|
| 237 | * @param type3 The type of the third functionParameter |
---|
| 238 | * @param type4 The type of the forth functionParameter |
---|
| 239 | * @param type5 The type of the fifth functionParameter |
---|
[5332] | 240 | */ |
---|
[4250] | 241 | #define LoadParam5(type1, type2, type3, type4, type5) \ |
---|
[4623] | 242 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \ |
---|
| 243 | void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \ |
---|
| 244 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
[4725] | 245 | type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \ |
---|
[5545] | 246 | : LoadParamBase(root, pt2Object, paramName, 5, multi, NULL, type1##_PARAM, default1, type2##_PARAM, default2, type3##_PARAM, default3, \ |
---|
[5332] | 247 | type4##_PARAM, default4, type5##_PARAM, default5) \ |
---|
| 248 | { \ |
---|
[4252] | 249 | if (loadString != NULL && root != NULL) \ |
---|
[5332] | 250 | { \ |
---|
[4592] | 251 | SubString subLoads(loadString); \ |
---|
| 252 | if (subLoads.getCount() == 5) \ |
---|
[4624] | 253 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3), type4##_FUNC(subLoads.getString(3), default4), type5##_FUNC(subLoads.getString(4), default5)); \ |
---|
[4592] | 254 | else \ |
---|
| 255 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 256 | paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \ |
---|
[5332] | 257 | } \ |
---|
[4250] | 258 | else \ |
---|
[4592] | 259 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[5332] | 260 | } |
---|
[4250] | 261 | |
---|
[4598] | 262 | // Pointer TYPE |
---|
| 263 | /** |
---|
[5332] | 264 | * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument |
---|
[4836] | 265 | * @param type1 The type of the Pointer |
---|
[4598] | 266 | */ |
---|
| 267 | #define LoadParamPT(type1) \ |
---|
| 268 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \ |
---|
[5545] | 269 | : LoadParamBase(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_PARAM) \ |
---|
[4598] | 270 | { \ |
---|
| 271 | if (pointerToParam != NULL && root != NULL) \ |
---|
| 272 | (*pt2Object.*function)((type1##_TYPE) pointerToParam); \ |
---|
| 273 | else \ |
---|
| 274 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
| 275 | } |
---|
[4250] | 276 | |
---|
[4256] | 277 | //! derived template class, so all the Classes can load something. |
---|
[5545] | 278 | template<class T> class LoadParam : public LoadParamBase |
---|
[4249] | 279 | { |
---|
[5137] | 280 | public: |
---|
[4501] | 281 | |
---|
[5133] | 282 | #define FUNCTOR_LIST(x) LoadParam ## x |
---|
| 283 | #include "functor_list.h" |
---|
| 284 | #undef FUNCTOR_LIST |
---|
[4243] | 285 | |
---|
[4734] | 286 | //! makes functions with one Vector loadable |
---|
| 287 | //LoadParam1(l_VECTOR); |
---|
| 288 | |
---|
[4726] | 289 | // loads a Ti-XML-element |
---|
[4599] | 290 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false) |
---|
[5545] | 291 | : LoadParamBase(root, pt2Object, paramName, 1, multi, NULL, "XML") |
---|
[4599] | 292 | { |
---|
| 293 | if (root != NULL) |
---|
| 294 | { |
---|
| 295 | const TiXmlElement* elem = root->FirstChildElement(paramName); |
---|
[5279] | 296 | if (elem != NULL) |
---|
[4599] | 297 | (*pt2Object.*function)(elem); |
---|
| 298 | else |
---|
[5301] | 299 | PRINTF(4)("%s of %s is empty\n", paramName, pt2Object->getClassName()); |
---|
[4599] | 300 | } |
---|
| 301 | else |
---|
| 302 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | //LoadParamPT(l_XML_ELEM); |
---|
[4233] | 306 | }; |
---|
| 307 | |
---|
[4492] | 308 | // helper function |
---|
[4233] | 309 | |
---|
[4492] | 310 | const char* grabParameter(const TiXmlElement* root, const char* parameterName); |
---|
| 311 | |
---|
[4233] | 312 | #endif /* _LOAD_PARAM_H */ |
---|