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