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 "base_object.h" |
---|
25 | |
---|
26 | #include "executor/executor_substring.h" |
---|
27 | #include "executor/executor_member.h" |
---|
28 | #include "executor/functor_member.h" |
---|
29 | |
---|
30 | #include "parser/tinyxml/tinyxml.h" |
---|
31 | |
---|
32 | // Forward Declaration // |
---|
33 | class LoadClassDescription; |
---|
34 | class LoadParamDescription; |
---|
35 | class TiXmlElement; |
---|
36 | |
---|
37 | /** |
---|
38 | * Loads a Parameter from ROOT named PARAMETER_NAME |
---|
39 | * onto OBJECT of CLASS, trough FUNCTION |
---|
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 | */ |
---|
46 | #define LoadParam(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
47 | CLoadParam<CLASS>(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS, CLASS>(&CLASS::FUNCTION), false) |
---|
48 | |
---|
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 | */ |
---|
57 | #define LoadParam_CYCLE(ROOT, PARAMETER_NAME, OBJECT, CLASS, FUNCTION) \ |
---|
58 | CLoadParam<CLASS>(ROOT, PARAMETER_NAME, OBJECT, createExecutor<CLASS, CLASS>(&CLASS::FUNCTION), true) |
---|
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 | |
---|
83 | |
---|
84 | /************************** |
---|
85 | **** REAL DECLARATIONS **** |
---|
86 | **************************/ |
---|
87 | //!< A BaseClass for all LoadParam's. |
---|
88 | class LoadParamBase |
---|
89 | { |
---|
90 | protected: |
---|
91 | LoadParamBase(const TiXmlElement* root, const std::string& paramName, bool inLoadCycle = false); |
---|
92 | |
---|
93 | protected: |
---|
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); |
---|
96 | |
---|
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); |
---|
100 | |
---|
101 | protected: |
---|
102 | const std::string paramName; //!< The Name of the Parameter this LoadParams applies to. |
---|
103 | bool inLoadCycle; //!< If the Parameter is in a LoadCycle. |
---|
104 | |
---|
105 | const TiXmlElement* loadElem; //!< The Element to load. |
---|
106 | }; |
---|
107 | |
---|
108 | |
---|
109 | //! The Loading Class of the LoadParam, that acctually executes the loading process. |
---|
110 | template <class OperateClass> class CLoadParam : public LoadParamBase |
---|
111 | { |
---|
112 | public: |
---|
113 | /** |
---|
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. |
---|
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 | } |
---|
139 | this->setDescriptionValues(OperateClass::staticClassID(), executor->getParamCount(), executor->getDefaultValues(), executor->hasRetVal()); |
---|
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 | */ |
---|
148 | CLoadParam& defaultValues(const MultiType& value0 = MT_NULL, const MultiType& value1 = MT_NULL, |
---|
149 | const MultiType& value2 = MT_NULL, const MultiType& value3 = MT_NULL, |
---|
150 | const MultiType& value4 = MT_NULL) |
---|
151 | { this->executor->defaultValues(value0, value1, value2, value3, value4); return *this; }; |
---|
152 | //! Describes a LoadParam |
---|
153 | CLoadParam& describe(const std::string& descriptionText) { LoadParamBase::describe(OperateClass::staticClassID(), descriptionText); return *this; }; |
---|
154 | // CLoadParam& attribute(const std::string& attributeName, const Executor<SubString>& executor); |
---|
155 | |
---|
156 | private: |
---|
157 | Executor<const SubString, OperateClass>* executor; //!< The Executor, that actually executes the Loading process. |
---|
158 | OperateClass* object; //!< The Object this LoadParam operates on. |
---|
159 | }; |
---|
160 | |
---|
161 | #endif /* _LOAD_PARAM_H */ |
---|