1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * |
---|
4 | * |
---|
5 | * License notice: |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License |
---|
9 | * as published by the Free Software Foundation; either version 2 |
---|
10 | * of the License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
20 | * |
---|
21 | * Author: |
---|
22 | * Fabian 'x3n' Landau |
---|
23 | * Co-authors: |
---|
24 | * ... |
---|
25 | * |
---|
26 | */ |
---|
27 | |
---|
28 | #ifndef _XMLPort_H__ |
---|
29 | #define _XMLPort_H__ |
---|
30 | |
---|
31 | #include "util/XMLIncludes.h" |
---|
32 | #include "util/MultiTypeMath.h" |
---|
33 | #include "util/TinyXML/ticpp.h" |
---|
34 | #include "util/SubString.h" |
---|
35 | #include "Functor.h" |
---|
36 | #include "Debug.h" |
---|
37 | |
---|
38 | #include "CorePrereqs.h" |
---|
39 | |
---|
40 | |
---|
41 | #define XMLPortParam(classname, paramname, loadfunction, savefunction, xmlelement, loading) \ |
---|
42 | orxonox::XMLPortClassParamContainer<classname>* xmlcontainer##loadfunction##savefunction = (orxonox::XMLPortClassParamContainer<classname>*)(this->getIdentifier()->getXMLPortParamContainer(paramname)); \ |
---|
43 | if (!xmlcontainer##loadfunction##savefunction) \ |
---|
44 | { \ |
---|
45 | xmlcontainer##loadfunction##savefunction = new orxonox::XMLPortClassParamContainer<classname>(this->getIdentifier()->getName(), std::string(paramname), createFunctor(&classname::loadfunction), createFunctor(&classname::savefunction)); \ |
---|
46 | this->getIdentifier()->addXMLPortParamContainer(paramname, xmlcontainer##loadfunction##savefunction); \ |
---|
47 | } \ |
---|
48 | xmlcontainer##loadfunction##savefunction->port(this, xmlelement, loading) |
---|
49 | |
---|
50 | #define XMLPortParamLoadOnly(classname, paramname, loadfunction, xmlelement, loading) \ |
---|
51 | orxonox::XMLPortClassParamContainer<classname>* xmlcontainer##loadfunction##savefunction = (orxonox::XMLPortClassParamContainer<classname>*)(this->getIdentifier()->getXMLPortParamContainer(paramname)); \ |
---|
52 | if (!xmlcontainer##loadfunction##savefunction) \ |
---|
53 | { \ |
---|
54 | xmlcontainer##loadfunction##savefunction = new orxonox::XMLPortClassParamContainer<classname>(this->getIdentifier()->getName(), std::string(paramname), createFunctor(&classname::loadfunction), 0); \ |
---|
55 | this->getIdentifier()->addXMLPortParamContainer(paramname, xmlcontainer##loadfunction##savefunction); \ |
---|
56 | } \ |
---|
57 | xmlcontainer##loadfunction##savefunction->port(this, xmlelement, loading) |
---|
58 | |
---|
59 | namespace orxonox |
---|
60 | { |
---|
61 | class _CoreExport XMLPortParamContainer |
---|
62 | { |
---|
63 | public: |
---|
64 | XMLPortParamContainer(); |
---|
65 | |
---|
66 | inline const std::string& getName() const |
---|
67 | { return this->paramname_; } |
---|
68 | |
---|
69 | XMLPortParamContainer& description(const std::string description); |
---|
70 | const std::string& getDescription(); |
---|
71 | |
---|
72 | XMLPortParamContainer& defaultValues(const MultiTypeMath& param1 = MT_null, const MultiTypeMath& param2 = MT_null, const MultiTypeMath& param3 = MT_null, const MultiTypeMath& param4 = MT_null, const MultiTypeMath& param5 = MT_null) |
---|
73 | { |
---|
74 | this->defaultValues_[0] = param1; |
---|
75 | this->defaultValues_[1] = param2; |
---|
76 | this->defaultValues_[2] = param3; |
---|
77 | this->defaultValues_[3] = param4; |
---|
78 | this->defaultValues_[4] = param5; |
---|
79 | |
---|
80 | return (*this); |
---|
81 | } |
---|
82 | |
---|
83 | protected: |
---|
84 | std::string classname_; |
---|
85 | std::string paramname_; |
---|
86 | MultiTypeMath defaultValues_[5]; |
---|
87 | |
---|
88 | private: |
---|
89 | LanguageEntryLabel description_; |
---|
90 | bool bAddedDescription_; |
---|
91 | bool bAddedDefaultValues_; |
---|
92 | }; |
---|
93 | |
---|
94 | template <class T> |
---|
95 | class XMLPortClassParamContainer : public XMLPortParamContainer |
---|
96 | { |
---|
97 | public: |
---|
98 | XMLPortClassParamContainer(const std::string classname, const std::string paramname, FunctorMember<T>* loadfunction, FunctorMember<T>* savefunction) |
---|
99 | { |
---|
100 | this->classname_ = classname; |
---|
101 | this->paramname_ = paramname; |
---|
102 | this->loadfunction_ = loadfunction; |
---|
103 | this->savefunction_ = savefunction; |
---|
104 | } |
---|
105 | |
---|
106 | XMLPortParamContainer& port(T* object, Element& xmlelement, bool loading) |
---|
107 | { |
---|
108 | if (loading) |
---|
109 | { |
---|
110 | std::cout << "3_1: load param " << this->paramname_ << std::endl; |
---|
111 | std::string attribute = xmlelement.GetAttribute(this->paramname_); |
---|
112 | std::cout << "3_2: attribute " << attribute << std::endl; |
---|
113 | if (attribute.size() > 0) |
---|
114 | { |
---|
115 | SubString tokens(attribute, ",", SubString::WhiteSpaces, false, '\\', '"', '(', ')', '\0'); |
---|
116 | std::cout << "3_3: tokens: " << tokens.size() << " params: " << this->loadfunction_->getParamCount() << std::endl; |
---|
117 | if ((unsigned int)tokens.size() >= (unsigned int)this->loadfunction_->getParamCount()) |
---|
118 | { |
---|
119 | if (this->loadfunction_->getParamCount() == 1) |
---|
120 | { |
---|
121 | std::cout << "3_4 start: load with whole attribute as input" << std::endl; |
---|
122 | (*this->loadfunction_)(object, MultiTypeMath(attribute)); |
---|
123 | std::cout << "3_5 end" << std::endl; |
---|
124 | } |
---|
125 | else |
---|
126 | { |
---|
127 | std::cout << "3_4: load with tokens as input" << std::endl; |
---|
128 | MultiTypeMath param1, param2, param3, param4, param5; |
---|
129 | if (tokens.size() >= 1) param1 = tokens[0]; |
---|
130 | if (tokens.size() >= 2) param1 = tokens[1]; |
---|
131 | if (tokens.size() >= 3) param1 = tokens[2]; |
---|
132 | if (tokens.size() >= 4) param1 = tokens[3]; |
---|
133 | if (tokens.size() >= 5) param1 = tokens[4]; |
---|
134 | |
---|
135 | (*this->loadfunction_)(object, param1, param2, param3, param4, param5); |
---|
136 | } |
---|
137 | } |
---|
138 | else |
---|
139 | { |
---|
140 | COUT(2) << " Warning: Parameter \"" << this->paramname_ << "\" in \"" << this->classname_ << "\" (objectname: " << object->getName() << ") is incomplete and couln't be loaded." << std::endl; |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | else |
---|
145 | { |
---|
146 | if (this->savefunction_) |
---|
147 | { |
---|
148 | // xmlelement.SetAttribute(this->paramname_, "..."); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | return (*this); |
---|
153 | } |
---|
154 | |
---|
155 | private: |
---|
156 | FunctorMember<T>* loadfunction_; |
---|
157 | FunctorMember<T>* savefunction_; |
---|
158 | }; |
---|
159 | } |
---|
160 | |
---|
161 | #endif /* _XMLPort_H__ */ |
---|