[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 | |
---|
[4597] | 24 | #include "base_object.h" |
---|
[5129] | 25 | |
---|
[4239] | 26 | #include "factory.h" |
---|
| 27 | #include "debug.h" |
---|
[4241] | 28 | #include "substring.h" |
---|
[4598] | 29 | #include "tinyxml.h" |
---|
[4233] | 30 | |
---|
[4251] | 31 | // Forward Declaration // |
---|
| 32 | template<class T> class tList; |
---|
| 33 | |
---|
[4495] | 34 | //! macro that makes it even more easy to load a Parameter |
---|
[4249] | 35 | /** |
---|
[4834] | 36 | * @param className the name of the class to load |
---|
| 37 | * @param parameterName the name of the parameter to load as written in the XML-file |
---|
| 38 | * @param function the function to call |
---|
| 39 | */ |
---|
[4495] | 40 | #define LOAD_PARAM(className, parameterName, paramFunction) \ |
---|
| 41 | LoadParam<className>(root, #parameterName, this, &className::paramFunction) |
---|
| 42 | |
---|
| 43 | /** |
---|
[4834] | 44 | * this Starts a Cycle in the Loading Process |
---|
| 45 | * be aware, that in the cycle the first parameter of load_param should because |
---|
| 46 | * called element, and that you must say true at the Fith parameter, or it will fail |
---|
| 47 | * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE |
---|
| 48 | */ |
---|
| 49 | #define LOAD_PARAM_START_CYCLE const TiXmlElement* element; \ |
---|
| 50 | element = root->FirstChildElement(); \ |
---|
| 51 | while( element != NULL) \ |
---|
| 52 | { |
---|
| 53 | /** |
---|
| 54 | * closes a LoadParam Loop |
---|
| 55 | * @see LOAD_PARAM_START_CYCLE |
---|
| 56 | */ |
---|
| 57 | #define LOAD_PARAM_END_CYCLE element = element->NextSiblingElement(); \ |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
[4623] | 61 | /***************************************** |
---|
| 62 | **** MACROS DEFINITIONS OF LOADABLES ***** |
---|
| 63 | *****************************************/ |
---|
[4487] | 64 | // 1. TYPE |
---|
[4249] | 65 | /** |
---|
[4836] | 66 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 1 argument |
---|
| 67 | * @param type1 The type of the first functionParameter |
---|
[4249] | 68 | */ |
---|
| 69 | #define LoadParam1(type1) \ |
---|
[4623] | 70 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), \ |
---|
| 71 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT) \ |
---|
| 72 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, type1##_NAME, default1) \ |
---|
[4249] | 73 | { \ |
---|
[4252] | 74 | if (loadString != NULL && root != NULL) \ |
---|
[4624] | 75 | (*pt2Object.*function)(type1##_FUNC(loadString, default1)); \ |
---|
[4249] | 76 | else \ |
---|
[4592] | 77 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[4249] | 78 | } |
---|
[4241] | 79 | |
---|
[4249] | 80 | // 2. TYPES |
---|
[4487] | 81 | /** |
---|
[4836] | 82 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 2 arguments |
---|
| 83 | * @param type1 The type of the first functionParameter |
---|
| 84 | * @param type2 The type of the second functionParameter |
---|
[4487] | 85 | */ |
---|
[4250] | 86 | #define LoadParam2(type1, type2) \ |
---|
[4623] | 87 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE), \ |
---|
| 88 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT) \ |
---|
| 89 | : BaseLoadParam(root, pt2Object, paramName, 2, multi, NULL, type1##_NAME, default1, type2##_NAME, default2) \ |
---|
[4249] | 90 | { \ |
---|
[4252] | 91 | if (loadString != NULL && root != NULL) \ |
---|
[4592] | 92 | { \ |
---|
| 93 | SubString subLoads(loadString); \ |
---|
| 94 | if (subLoads.getCount() == 2) \ |
---|
[4624] | 95 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2)); \ |
---|
[4592] | 96 | else \ |
---|
| 97 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 98 | paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \ |
---|
| 99 | } \ |
---|
[4249] | 100 | else \ |
---|
[4592] | 101 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[4251] | 102 | } |
---|
[4241] | 103 | |
---|
[4243] | 104 | |
---|
[4249] | 105 | // 3. TYPES |
---|
[4487] | 106 | /** |
---|
[4836] | 107 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 3 arguments |
---|
| 108 | * @param type1 The type of the first functionParameter |
---|
| 109 | * @param type2 The type of the second functionParameter |
---|
| 110 | * @param type3 The type of the third functionParameter |
---|
[4487] | 111 | */ |
---|
[4250] | 112 | #define LoadParam3(type1, type2, type3) \ |
---|
[4623] | 113 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE), \ |
---|
| 114 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT)\ |
---|
| 115 | : BaseLoadParam(root, pt2Object, paramName, 3, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3) \ |
---|
[4249] | 116 | { \ |
---|
[4252] | 117 | if (loadString != NULL && root != NULL) \ |
---|
[4592] | 118 | { \ |
---|
| 119 | SubString subLoads(loadString); \ |
---|
| 120 | if (subLoads.getCount() == 3) \ |
---|
[4624] | 121 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0), default1), type2##_FUNC(subLoads.getString(1), default2), type3##_FUNC(subLoads.getString(2), default3)); \ |
---|
[4592] | 122 | else \ |
---|
| 123 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 124 | paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \ |
---|
| 125 | } \ |
---|
[4249] | 126 | else \ |
---|
[4592] | 127 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[4251] | 128 | } |
---|
[4241] | 129 | |
---|
[4249] | 130 | |
---|
| 131 | // 4. TYPES |
---|
[4487] | 132 | /** |
---|
[4836] | 133 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 4 arguments |
---|
| 134 | * @param type1 The type of the first functionParameter |
---|
| 135 | * @param type2 The type of the second functionParameter |
---|
| 136 | * @param type3 The type of the third functionParameter |
---|
| 137 | * @param type4 The type of the forth functionParameter |
---|
[4487] | 138 | */ |
---|
[4250] | 139 | #define LoadParam4(type1, type2, type3, type4) \ |
---|
[4623] | 140 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE), \ |
---|
| 141 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
| 142 | type4##_TYPE default4 = type4##_DEFAULT) \ |
---|
| 143 | : BaseLoadParam(root, pt2Object, paramName, 4, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \ |
---|
| 144 | type4##_NAME, default4) \ |
---|
[4249] | 145 | { \ |
---|
[4252] | 146 | if (loadString != NULL && root != NULL) \ |
---|
[4592] | 147 | { \ |
---|
| 148 | SubString subLoads(loadString); \ |
---|
| 149 | if (subLoads.getCount() == 4) \ |
---|
[4624] | 150 | (*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] | 151 | else \ |
---|
| 152 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 153 | paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \ |
---|
| 154 | } \ |
---|
[4249] | 155 | else \ |
---|
[4592] | 156 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[4251] | 157 | } |
---|
[4241] | 158 | |
---|
[4250] | 159 | |
---|
| 160 | // 5. TYPES |
---|
[4487] | 161 | /** |
---|
[4836] | 162 | * a Macro to easily implement many different Constructors for the LoadParam-Class with 5 arguments |
---|
| 163 | * @param type1 The type of the first functionParameter |
---|
| 164 | * @param type2 The type of the second functionParameter |
---|
| 165 | * @param type3 The type of the third functionParameter |
---|
| 166 | * @param type4 The type of the forth functionParameter |
---|
| 167 | * @param type5 The type of the fifth functionParameter |
---|
[4487] | 168 | */ |
---|
[4250] | 169 | #define LoadParam5(type1, type2, type3, type4, type5) \ |
---|
[4623] | 170 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, \ |
---|
| 171 | void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE), \ |
---|
| 172 | bool multi = false, type1##_TYPE default1 = type1##_DEFAULT, type2##_TYPE default2 = type2##_DEFAULT, type3##_TYPE default3 = type3##_DEFAULT, \ |
---|
[4725] | 173 | type4##_TYPE default4 = type4##_DEFAULT, type5##_TYPE default5 = type5##_DEFAULT ) \ |
---|
[4623] | 174 | : BaseLoadParam(root, pt2Object, paramName, 5, multi, NULL, type1##_NAME, default1, type2##_NAME, default2, type3##_NAME, default3, \ |
---|
| 175 | type4##_NAME, default4, type5##_NAME, default5) \ |
---|
[4250] | 176 | { \ |
---|
[4252] | 177 | if (loadString != NULL && root != NULL) \ |
---|
[4592] | 178 | { \ |
---|
| 179 | SubString subLoads(loadString); \ |
---|
| 180 | if (subLoads.getCount() == 5) \ |
---|
[4624] | 181 | (*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] | 182 | else \ |
---|
| 183 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 184 | paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \ |
---|
| 185 | } \ |
---|
[4250] | 186 | else \ |
---|
[4592] | 187 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[4251] | 188 | } |
---|
[4250] | 189 | |
---|
[4598] | 190 | // Pointer TYPE |
---|
| 191 | /** |
---|
[4836] | 192 | * a Macro to easily implement many different Constructors for the LoadParam-Class with one Pointer argument |
---|
| 193 | * @param type1 The type of the Pointer |
---|
[4598] | 194 | */ |
---|
| 195 | #define LoadParamPT(type1) \ |
---|
| 196 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE), type1##_TYPE pointerToParam, bool multi = false) \ |
---|
| 197 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, pointerToParam, type1##_NAME) \ |
---|
| 198 | { \ |
---|
| 199 | if (pointerToParam != NULL && root != NULL) \ |
---|
| 200 | (*pt2Object.*function)((type1##_TYPE) pointerToParam); \ |
---|
| 201 | else \ |
---|
| 202 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
| 203 | } |
---|
[4250] | 204 | |
---|
[4624] | 205 | |
---|
| 206 | /*********************** |
---|
| 207 | *** HELPER FUNCTIONS *** |
---|
| 208 | ***********************/ |
---|
[4860] | 209 | bool isBool(const char* BOOL, bool defaultValue); |
---|
[4734] | 210 | int isInt(const char* INT, int defaultValue); |
---|
| 211 | float isFloat(const char* FLOAT, float defaultValue); |
---|
| 212 | //const Vector& isVector(const char* VECTOR, const Vector& defaultValue); |
---|
| 213 | const char* isString(const char* STRING, const char* defaultValue); |
---|
| 214 | |
---|
[4624] | 215 | //TiXmlEmlemnt* isXmlElem(const) |
---|
| 216 | |
---|
| 217 | |
---|
| 218 | /************************ |
---|
| 219 | *** DESCRIPTION STUFF *** |
---|
| 220 | ************************/ |
---|
[4256] | 221 | //! A class that handles the description of loadable parameters |
---|
[4250] | 222 | class LoadParamDescription |
---|
[4249] | 223 | { |
---|
[4254] | 224 | friend class BaseLoadParam; |
---|
| 225 | friend class LoadClassDescription; |
---|
| 226 | public: |
---|
| 227 | LoadParamDescription(const char* paramName); |
---|
[4746] | 228 | ~LoadParamDescription(); |
---|
[4255] | 229 | |
---|
| 230 | void setDescription(const char* descriptionText); |
---|
[4836] | 231 | /** @returns the descriptionString */ |
---|
[4746] | 232 | const char* getDescription() { return this->description; }; |
---|
[4255] | 233 | |
---|
[4746] | 234 | void print() const; |
---|
[4250] | 235 | private: |
---|
[4623] | 236 | char* paramName; //!< The name of the parameter. |
---|
| 237 | int paramCount; //!< The count of parameters. |
---|
[4487] | 238 | char** types; //!< What kind of parameters does this function take ?? |
---|
[4623] | 239 | char* description; //!< A longer description about this function. |
---|
| 240 | char** defaultValues; //!< The 'Default Values'. |
---|
[4250] | 241 | }; |
---|
| 242 | |
---|
[4487] | 243 | //! A class for descriptions of a loadable module |
---|
[4251] | 244 | class LoadClassDescription |
---|
| 245 | { |
---|
[4254] | 246 | friend class BaseLoadParam; |
---|
[4251] | 247 | public: |
---|
[4254] | 248 | LoadClassDescription(const char* className); |
---|
[4746] | 249 | ~LoadClassDescription(); |
---|
[4251] | 250 | |
---|
[4254] | 251 | static LoadClassDescription* addClass(const char* className); |
---|
| 252 | LoadParamDescription* addParam(const char* paramName); |
---|
[4251] | 253 | |
---|
[4260] | 254 | static void printAll(const char* fileName = NULL); |
---|
[5113] | 255 | static tList<const char>* searchClassWithShort(const char* classNameBegin); |
---|
[5100] | 256 | // static const LoadParamDescription* getClass(const char* className); |
---|
[4255] | 257 | |
---|
[4251] | 258 | private: |
---|
[4496] | 259 | static bool parametersDescription; //!< if parameter-description should be enabled. |
---|
| 260 | static tList<LoadClassDescription>* classList; //!< a list, that holds all the loadable classes. (after one instance has been loaded) |
---|
| 261 | char* className; //!< name of the class |
---|
| 262 | tList<LoadParamDescription>* paramList; //!< List of parameters this class knows. |
---|
[4251] | 263 | }; |
---|
| 264 | |
---|
[4624] | 265 | |
---|
| 266 | /************************** |
---|
| 267 | **** REAL DECLARATIONS **** |
---|
| 268 | **************************/ |
---|
[4487] | 269 | //! abstract Base class for a Loadable parameter |
---|
[4597] | 270 | class BaseLoadParam : public BaseObject |
---|
[4250] | 271 | { |
---|
[4255] | 272 | public: |
---|
[4260] | 273 | BaseLoadParam* describe(const char* descriptionText); |
---|
[4255] | 274 | |
---|
[4251] | 275 | protected: |
---|
[4598] | 276 | BaseLoadParam(const TiXmlElement* root, BaseObject* object, const char* paramName, int paramCount, bool multi, const void* pointerToParam, ...); |
---|
[4251] | 277 | |
---|
| 278 | protected: |
---|
[4487] | 279 | LoadClassDescription* classDesc; //!< The LoadClassDescription of this LoadParameter |
---|
| 280 | LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter |
---|
| 281 | const char* loadString; //!< The string loaded by this LoadParam |
---|
[4598] | 282 | const void* pointerToParam; //!< A Pointer to a Parameter. |
---|
[4249] | 283 | }; |
---|
[4243] | 284 | |
---|
[4241] | 285 | |
---|
[4256] | 286 | //! derived template class, so all the Classes can load something. |
---|
[4250] | 287 | template<class T> class LoadParam : public BaseLoadParam |
---|
[4249] | 288 | { |
---|
| 289 | public: |
---|
[4501] | 290 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(), bool multi = false) |
---|
[4598] | 291 | : BaseLoadParam(root, pt2Object, paramName, 0, multi, NULL, "") |
---|
[4592] | 292 | { |
---|
[4501] | 293 | if (loadString != NULL && root != NULL) |
---|
[4592] | 294 | (*pt2Object.*function)(); |
---|
| 295 | else |
---|
| 296 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); |
---|
[4501] | 297 | } |
---|
| 298 | |
---|
| 299 | |
---|
[5133] | 300 | #define FUNCTOR_LIST(x) LoadParam ## x |
---|
| 301 | #include "functor_list.h" |
---|
| 302 | #undef FUNCTOR_LIST |
---|
[4243] | 303 | |
---|
[4734] | 304 | //! makes functions with one Vector loadable |
---|
| 305 | //LoadParam1(l_VECTOR); |
---|
| 306 | |
---|
[4726] | 307 | // loads a Ti-XML-element |
---|
[4599] | 308 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(const TiXmlElement*), bool multi = false) |
---|
[4625] | 309 | : BaseLoadParam(root, pt2Object, paramName, 1, multi, NULL, "XML") |
---|
[4599] | 310 | { |
---|
| 311 | if (root != NULL) |
---|
| 312 | { |
---|
| 313 | const TiXmlElement* elem = root->FirstChildElement(paramName); |
---|
| 314 | if (likely(elem != NULL)) |
---|
| 315 | (*pt2Object.*function)(elem); |
---|
| 316 | else |
---|
| 317 | PRINTF(2)("%s of %s is empty", paramName, pt2Object->getClassName()); |
---|
| 318 | } |
---|
| 319 | else |
---|
| 320 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | //LoadParamPT(l_XML_ELEM); |
---|
[4233] | 324 | }; |
---|
| 325 | |
---|
[4492] | 326 | // helper function |
---|
[4233] | 327 | |
---|
[4492] | 328 | const char* grabParameter(const TiXmlElement* root, const char* parameterName); |
---|
| 329 | |
---|
[4233] | 330 | #endif /* _LOAD_PARAM_H */ |
---|