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