| 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 "util/executor/executor_substring.h" | 
|---|
| 25 | #include "util/executor/executor_member.h" | 
|---|
| 26 | #include "util/executor/functor_member.h" | 
|---|
| 27 |  | 
|---|
| 28 | #include "parser/tinyxml/tinyxml.h" | 
|---|
| 29 |  | 
|---|
| 30 | // Forward Declaration // | 
|---|
| 31 | class LoadClassDescription; | 
|---|
| 32 | class LoadParamDescription; | 
|---|
| 33 | class TiXmlElement; | 
|---|
| 34 |  | 
|---|
| 35 | /** | 
|---|
| 36 | * Loads a Parameter from ROOT named PARAMETER_NAME | 
|---|
| 37 | * onto OBJECT of CLASS, trough FUNCTION | 
|---|
| 38 | * @param ROOT the TiXmlElement to load the Parameter from | 
|---|
| 39 | * @param PARAMETER_NAME the Name of the Parameter to load | 
|---|
| 40 | * @param OBJECT The BaseObject to load the new setting to. | 
|---|
| 41 | * @param CLASS What Class the BaseObejct is of (this is for identifying the Functuon) | 
|---|
| 42 | * @param FUNCTION The function of Class to Load (if you want to call &CLASS::FUNCTION write FUNCTION here). | 
|---|
| 43 | */ | 
|---|
| 44 | #define LoadParam(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ | 
|---|
| 45 | CLoadParam<CLASS>(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS, CLASS>(&CLASS::FUNCTION), false) | 
|---|
| 46 |  | 
|---|
| 47 | /** | 
|---|
| 48 | * @brief Does essentially the same as LoadParam, but within a Cycle in an ordered fashion. | 
|---|
| 49 | * | 
|---|
| 50 | * This Function looks in each Element, if the PARAMETER_NAME matches, and loads onto OBJECT | 
|---|
| 51 | * of CLASS the ROOT through FUNCTION | 
|---|
| 52 | * | 
|---|
| 53 | * @see LoadParam(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) | 
|---|
| 54 | */ | 
|---|
| 55 | #define LoadParam_CYCLE(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ | 
|---|
| 56 | CLoadParam<CLASS>(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS, CLASS>(&CLASS::FUNCTION), true) | 
|---|
| 57 |  | 
|---|
| 58 | /** | 
|---|
| 59 | * this Starts a Cycle in the Loading Process | 
|---|
| 60 | * be aware, that in the cycle the first parameter of load_param should because | 
|---|
| 61 | * called element, and that you must say true at the Fith parameter, or it will fail | 
|---|
| 62 | * also you will have to close the Cycle again with LOAD_PARAM_END_CYCLE | 
|---|
| 63 | * | 
|---|
| 64 | * @param ROOT The root XLM-element to search element under. | 
|---|
| 65 | * @param ELEMENT the element to search | 
|---|
| 66 | */ | 
|---|
| 67 | #define LOAD_PARAM_START_CYCLE(ROOT, ELEMENT) \ | 
|---|
| 68 | const TiXmlElement* ELEMENT; \ | 
|---|
| 69 | ELEMENT= ROOT->FirstChildElement(); \ | 
|---|
| 70 | while( ELEMENT != NULL) \ | 
|---|
| 71 | { | 
|---|
| 72 | /** | 
|---|
| 73 | * closes a LoadParam Loop | 
|---|
| 74 | * @see LOAD_PARAM_START_CYCLE | 
|---|
| 75 | * @param ELEMENT the Element to step through. | 
|---|
| 76 | */ | 
|---|
| 77 | #define LOAD_PARAM_END_CYCLE(ELEMENT) \ | 
|---|
| 78 | ELEMENT = ELEMENT->NextSiblingElement(); \ | 
|---|
| 79 | } | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | /************************** | 
|---|
| 83 | **** REAL DECLARATIONS **** | 
|---|
| 84 | **************************/ | 
|---|
| 85 | //!< A BaseClass for all LoadParam's. | 
|---|
| 86 | class LoadParamBase | 
|---|
| 87 | { | 
|---|
| 88 | protected: | 
|---|
| 89 | LoadParamBase(const TiXmlElement* root, const std::string& paramName, bool inLoadCycle = false); | 
|---|
| 90 |  | 
|---|
| 91 | protected: | 
|---|
| 92 | void describe(const ClassID& classID, const std::string& descriptionText); | 
|---|
| 93 | void setDescriptionValues(const ClassID& classID, unsigned int paramCount, const MultiType* const defaultValues, bool retVal = false); | 
|---|
| 94 |  | 
|---|
| 95 | public: | 
|---|
| 96 | static std::string grabParameter(const TiXmlElement* root, const std::string& parameterName); | 
|---|
| 97 | static const TiXmlElement* grabParameterElement(const TiXmlElement* root, const std::string& parameterName); | 
|---|
| 98 |  | 
|---|
| 99 | protected: | 
|---|
| 100 | const std::string        paramName;            //!< The Name of the Parameter this LoadParams applies to. | 
|---|
| 101 | bool                     inLoadCycle;          //!< If the Parameter is in a LoadCycle. | 
|---|
| 102 |  | 
|---|
| 103 | const TiXmlElement*      loadElem;             //!< The Element to load. | 
|---|
| 104 | }; | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | //! The Loading Class of the LoadParam, that acctually executes the loading process. | 
|---|
| 108 | template <class OperateClass> class CLoadParam : public LoadParamBase | 
|---|
| 109 | { | 
|---|
| 110 | public: | 
|---|
| 111 | /** | 
|---|
| 112 | * @brief generates a LoadParam based on: | 
|---|
| 113 | * @param root the Root Element to load onto the object. @param paramName the Parameter name that is loaded. | 
|---|
| 114 | * @param object the Object to apply the changes on. @param executor the Functional Object, that actually executes the function Call. | 
|---|
| 115 | * @param inLoadCycle If we are inside of a loading cycle. (Loading will be different here) | 
|---|
| 116 | */ | 
|---|
| 117 | CLoadParam(const TiXmlElement* root, const std::string& paramName, OperateClass* object, Executor<const SubString, OperateClass>* executor, bool inLoadCycle = false) | 
|---|
| 118 | : LoadParamBase(root, paramName, inLoadCycle) | 
|---|
| 119 | { | 
|---|
| 120 | assert (executor != NULL); | 
|---|
| 121 | this->object = object; | 
|---|
| 122 | this->executor = executor; | 
|---|
| 123 | } | 
|---|
| 124 | virtual ~CLoadParam() | 
|---|
| 125 | { | 
|---|
| 126 | std::string loadString; | 
|---|
| 127 | if (this->loadElem != NULL &&  this->loadElem->ToText()) | 
|---|
| 128 | { | 
|---|
| 129 | loadString = this->loadElem->Value(); | 
|---|
| 130 | if (!loadString.empty()) | 
|---|
| 131 | { | 
|---|
| 132 | /*      PRINTF(4)("Loading value '%s' with Parameters '%s' onto: %s::%s\n", | 
|---|
| 133 | this->paramName.c_str(), loadString.c_str(), this->object->getClassCName(), this->object->getCName());*/ | 
|---|
| 134 | (*this->executor)(this->object, SubString(loadString, ",", SubString::WhiteSpaces, false, '\\')); | 
|---|
| 135 | } | 
|---|
| 136 | } | 
|---|
| 137 | this->setDescriptionValues(OperateClass::staticClassID(), executor->getParamCount(), executor->getDefaultValues(), executor->hasRetVal()); | 
|---|
| 138 | delete this->executor; | 
|---|
| 139 | } | 
|---|
| 140 | /** | 
|---|
| 141 | * @brief set the default values of the executor | 
|---|
| 142 | * @param value0 the first default value   @param value1 the second default value | 
|---|
| 143 | * @param value2 the third default value   @param value3 the fourth default value | 
|---|
| 144 | * @param value4 the fifth default value | 
|---|
| 145 | */ | 
|---|
| 146 | CLoadParam& defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, | 
|---|
| 147 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, | 
|---|
| 148 | const MultiType& value4 = MT_NULL) | 
|---|
| 149 | { this->executor->defaultValues(value0, value1, value2, value3, value4); return *this;  }; | 
|---|
| 150 | //! Describes a LoadParam | 
|---|
| 151 | CLoadParam& describe(const std::string& descriptionText) { LoadParamBase::describe(OperateClass::staticClassID(), descriptionText); return *this; }; | 
|---|
| 152 | //     CLoadParam& attribute(const std::string& attributeName, const Executor<SubString>& executor); | 
|---|
| 153 |  | 
|---|
| 154 | private: | 
|---|
| 155 | Executor<const SubString, OperateClass>*         executor;            //!< The Executor, that actually executes the Loading process. | 
|---|
| 156 | OperateClass*                                    object;              //!< The Object this LoadParam operates on. | 
|---|
| 157 | }; | 
|---|
| 158 |  | 
|---|
| 159 | #endif /* _LOAD_PARAM_H */ | 
|---|