[4250] | 1 | /* |
---|
| 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 | |
---|
| 16 | /*! |
---|
| 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 | |
---|
[4239] | 24 | #include "factory.h" |
---|
| 25 | #include "debug.h" |
---|
[4241] | 26 | #include "substring.h" |
---|
[4233] | 27 | |
---|
[4251] | 28 | // Forward Declaration // |
---|
| 29 | template<class T> class tList; |
---|
| 30 | |
---|
[4249] | 31 | /** |
---|
| 32 | useable FunctionParameters are: |
---|
| 33 | l_INT: int |
---|
| 34 | l_LONG: long |
---|
| 35 | l_SHORT: short |
---|
| 36 | l_FLOAT: float |
---|
| 37 | l_STRING: const char* |
---|
| 38 | */ |
---|
[4233] | 39 | |
---|
[4249] | 40 | #define l_INT_TYPE int |
---|
| 41 | #define l_INT_FUNC atoi |
---|
[4251] | 42 | #define l_INT_NAME "int" |
---|
[4233] | 43 | |
---|
[4249] | 44 | #define l_LONG_TYPE long |
---|
| 45 | #define l_LONG_FUNC atol |
---|
[4251] | 46 | #define l_LONG_NAME "long" |
---|
[4233] | 47 | |
---|
[4249] | 48 | #define l_SHORT_TYPE short |
---|
| 49 | #define l_SHORT_FUNC atoi |
---|
[4251] | 50 | #define l_SHORT_NAME "short" |
---|
[4242] | 51 | |
---|
[4249] | 52 | #define l_FLOAT_TYPE float |
---|
| 53 | #define l_FLOAT_FUNC atof |
---|
[4251] | 54 | #define l_FLOAT_NAME "float" |
---|
[4242] | 55 | |
---|
[4249] | 56 | #define l_STRING_TYPE const char* |
---|
[4251] | 57 | #define l_STRING_FUNC |
---|
| 58 | #define l_STRING_NAME "string" |
---|
[4242] | 59 | |
---|
[4241] | 60 | |
---|
[4249] | 61 | /** |
---|
| 62 | \brief a Macro to easily implement many different Constructors for the LoadParam-Class |
---|
| 63 | \param type1 The type of the first functionParameter |
---|
| 64 | */ |
---|
| 65 | #define LoadParam1(type1) \ |
---|
| 66 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE)) \ |
---|
[4254] | 67 | : BaseLoadParam(pt2Object, paramName, 1, type1##_NAME) \ |
---|
[4249] | 68 | { \ |
---|
| 69 | const char* loadString = grabParameter(root, paramName); \ |
---|
[4252] | 70 | if (loadString != NULL && root != NULL) \ |
---|
[4249] | 71 | (*pt2Object.*function)(type1##_FUNC(loadString)); \ |
---|
| 72 | else \ |
---|
[4251] | 73 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
[4249] | 74 | } |
---|
[4241] | 75 | |
---|
[4249] | 76 | |
---|
| 77 | // 2. TYPES |
---|
[4250] | 78 | #define LoadParam2(type1, type2) \ |
---|
[4249] | 79 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE)) \ |
---|
[4254] | 80 | : BaseLoadParam(pt2Object, paramName, 2, type1##_NAME, type2##_NAME) \ |
---|
[4249] | 81 | { \ |
---|
| 82 | const char* loadString = grabParameter(root, paramName); \ |
---|
[4252] | 83 | if (loadString != NULL && root != NULL) \ |
---|
[4249] | 84 | { \ |
---|
| 85 | SubString subLoads(loadString); \ |
---|
| 86 | if (subLoads.getCount() == 2) \ |
---|
| 87 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1))); \ |
---|
| 88 | else \ |
---|
| 89 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 90 | paramName, pt2Object->getClassName(), 2, subLoads.getCount()); \ |
---|
| 91 | } \ |
---|
| 92 | else \ |
---|
[4251] | 93 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
| 94 | } |
---|
[4241] | 95 | |
---|
[4243] | 96 | |
---|
[4249] | 97 | // 3. TYPES |
---|
[4250] | 98 | #define LoadParam3(type1, type2, type3) \ |
---|
[4251] | 99 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE))\ |
---|
[4254] | 100 | : BaseLoadParam(pt2Object, paramName, 3, type1##_NAME, type2##_NAME, type3##_NAME) \ |
---|
[4249] | 101 | { \ |
---|
| 102 | const char* loadString = grabParameter(root, paramName); \ |
---|
[4252] | 103 | if (loadString != NULL && root != NULL) \ |
---|
[4249] | 104 | { \ |
---|
| 105 | SubString subLoads(loadString); \ |
---|
| 106 | if (subLoads.getCount() == 3) \ |
---|
| 107 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2))); \ |
---|
| 108 | else \ |
---|
| 109 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 110 | paramName, pt2Object->getClassName(), 3, subLoads.getCount()); \ |
---|
| 111 | } \ |
---|
| 112 | else \ |
---|
[4251] | 113 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
| 114 | } |
---|
[4241] | 115 | |
---|
[4249] | 116 | |
---|
| 117 | // 4. TYPES |
---|
[4250] | 118 | #define LoadParam4(type1, type2, type3, type4) \ |
---|
[4249] | 119 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE)) \ |
---|
[4254] | 120 | : BaseLoadParam(pt2Object, paramName, 4, type1##_NAME, type2##_NAME, type3##_NAME, type2##_NAME, type4##_NAME) \ |
---|
[4249] | 121 | { \ |
---|
| 122 | const char* loadString = grabParameter(root, paramName); \ |
---|
[4252] | 123 | if (loadString != NULL && root != NULL) \ |
---|
[4249] | 124 | { \ |
---|
| 125 | SubString subLoads(loadString); \ |
---|
| 126 | if (subLoads.getCount() == 4) \ |
---|
| 127 | (*pt2Object.*function)(type1##_FUNC(subLoads.getString(0)), type2##_FUNC(subLoads.getString(1)), type3##_FUNC(subLoads.getString(2)), type4##_FUNC(subLoads.getString(3))); \ |
---|
| 128 | else \ |
---|
| 129 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 130 | paramName, pt2Object->getClassName(), 4, subLoads.getCount()); \ |
---|
| 131 | } \ |
---|
| 132 | else \ |
---|
[4251] | 133 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
| 134 | } |
---|
[4241] | 135 | |
---|
[4250] | 136 | |
---|
| 137 | // 5. TYPES |
---|
| 138 | #define LoadParam5(type1, type2, type3, type4, type5) \ |
---|
| 139 | LoadParam(const TiXmlElement* root, const char* paramName, T* pt2Object, void(T::*function)(type1##_TYPE, type2##_TYPE, type3##_TYPE, type4##_TYPE, type5##_TYPE)) \ |
---|
[4254] | 140 | : BaseLoadParam(pt2Object, paramName, 5, type1##_NAME, type2##_NAME, type3##_NAME, type2##_NAME, type4##_NAME, type5##_NAME) \ |
---|
[4250] | 141 | { \ |
---|
| 142 | const char* loadString = grabParameter(root, paramName); \ |
---|
[4252] | 143 | if (loadString != NULL && root != NULL) \ |
---|
[4250] | 144 | { \ |
---|
| 145 | SubString subLoads(loadString); \ |
---|
| 146 | if (subLoads.getCount() == 5) \ |
---|
| 147 | (*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))); \ |
---|
| 148 | else \ |
---|
| 149 | PRINTF(2)("Not loaded Parameter %s of %s, because wrong count of arguments.\n -> Should have %d but have %d\n", \ |
---|
| 150 | paramName, pt2Object->getClassName(), 5, subLoads.getCount()); \ |
---|
| 151 | } \ |
---|
| 152 | else \ |
---|
[4251] | 153 | PRINTF(4)("Not loaded parameter %s of %s\n", paramName, pt2Object->getClassName()); \ |
---|
| 154 | } |
---|
[4250] | 155 | |
---|
| 156 | |
---|
[4256] | 157 | //! A class that handles the description of loadable parameters |
---|
[4250] | 158 | class LoadParamDescription |
---|
[4249] | 159 | { |
---|
[4254] | 160 | friend class BaseLoadParam; |
---|
| 161 | friend class LoadClassDescription; |
---|
| 162 | public: |
---|
| 163 | LoadParamDescription(const char* paramName); |
---|
| 164 | ~LoadParamDescription(void); |
---|
[4255] | 165 | |
---|
| 166 | void setDescription(const char* descriptionText); |
---|
| 167 | /** \returns the descriptionString */ |
---|
| 168 | const char* getDescription(void) { return this->description;}; |
---|
| 169 | |
---|
| 170 | void print(void) const; |
---|
[4250] | 171 | private: |
---|
[4256] | 172 | char* paramName; //!< The name of the parameter |
---|
| 173 | int paramCount; //!< The count of parameters |
---|
| 174 | char** types; //!< What kind of parameters does this function take ?? |
---|
| 175 | char* description; //!< A longer description about this function |
---|
[4250] | 176 | }; |
---|
| 177 | |
---|
[4251] | 178 | class LoadClassDescription |
---|
| 179 | { |
---|
[4254] | 180 | friend class BaseLoadParam; |
---|
[4251] | 181 | public: |
---|
[4254] | 182 | LoadClassDescription(const char* className); |
---|
| 183 | ~LoadClassDescription(void); |
---|
[4251] | 184 | |
---|
[4254] | 185 | static LoadClassDescription* addClass(const char* className); |
---|
| 186 | LoadParamDescription* addParam(const char* paramName); |
---|
[4251] | 187 | |
---|
[4260] | 188 | static void printAll(const char* fileName = NULL); |
---|
[4255] | 189 | |
---|
[4256] | 190 | static bool parametersDescription; //!< if parameter-description should be enabled. |
---|
| 191 | static tList<LoadClassDescription>* classList; //!< a list, that holds all the loadable classes. (after one instance has been loaded) |
---|
[4251] | 192 | private: |
---|
[4256] | 193 | char* className; //!< name of the class |
---|
| 194 | tList<LoadParamDescription>* paramList; //!< List of parameters this class knows. |
---|
[4251] | 195 | }; |
---|
| 196 | |
---|
[4250] | 197 | // abstract Base class |
---|
| 198 | class BaseLoadParam |
---|
| 199 | { |
---|
[4255] | 200 | public: |
---|
[4260] | 201 | BaseLoadParam* describe(const char* descriptionText); |
---|
[4255] | 202 | |
---|
[4251] | 203 | protected: |
---|
[4254] | 204 | BaseLoadParam(BaseObject* object, const char* paramName, int paramCount, ...); |
---|
[4251] | 205 | |
---|
| 206 | protected: |
---|
[4256] | 207 | LoadClassDescription* classDesc; //!< The LoadClassDescription of this LoadParameter |
---|
| 208 | LoadParamDescription* paramDesc; //!< The LoadParameterDescription of this LoadParameter |
---|
[4249] | 209 | }; |
---|
[4243] | 210 | |
---|
[4241] | 211 | |
---|
[4256] | 212 | //! derived template class, so all the Classes can load something. |
---|
[4250] | 213 | template<class T> class LoadParam : public BaseLoadParam |
---|
[4249] | 214 | { |
---|
| 215 | public: |
---|
| 216 | LoadParam1(l_STRING); |
---|
| 217 | LoadParam2(l_STRING, l_STRING); |
---|
| 218 | LoadParam3(l_STRING, l_STRING, l_STRING); |
---|
| 219 | LoadParam4(l_STRING, l_STRING, l_STRING, l_STRING); |
---|
[4243] | 220 | |
---|
[4249] | 221 | LoadParam1(l_INT); |
---|
| 222 | LoadParam2(l_INT, l_INT); |
---|
| 223 | LoadParam3(l_INT, l_INT, l_INT); |
---|
| 224 | LoadParam4(l_INT, l_INT, l_INT, l_INT); |
---|
[4241] | 225 | |
---|
[4249] | 226 | LoadParam1(l_FLOAT); |
---|
| 227 | LoadParam2(l_FLOAT, l_FLOAT); |
---|
| 228 | LoadParam3(l_FLOAT, l_FLOAT, l_FLOAT); |
---|
| 229 | LoadParam4(l_FLOAT, l_FLOAT, l_FLOAT, l_FLOAT); |
---|
[4233] | 230 | }; |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | #endif /* _LOAD_PARAM_H */ |
---|