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