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