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