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