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