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