/src/ogre/OgreMain/include/OgreStringInterface.h
Line | Count | Source |
1 | | /* |
2 | | ----------------------------------------------------------------------------- |
3 | | This source file is part of OGRE |
4 | | (Object-oriented Graphics Rendering Engine) |
5 | | For the latest info, see http://www.ogre3d.org/ |
6 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | |
29 | | #ifndef __StringInterface_H__ |
30 | | #define __StringInterface_H__ |
31 | | |
32 | | #include "OgrePrerequisites.h" |
33 | | #include "OgreCommon.h" |
34 | | #include "OgreHeaderPrefix.h" |
35 | | #include "OgreStringConverter.h" |
36 | | |
37 | | namespace Ogre { |
38 | | |
39 | | /** \addtogroup Core |
40 | | * @{ |
41 | | */ |
42 | | /** \addtogroup General |
43 | | * @{ |
44 | | */ |
45 | | |
46 | | /// @deprecated do not use |
47 | | enum ParameterType |
48 | | { |
49 | | PT_BOOL, |
50 | | PT_REAL, |
51 | | PT_INT, |
52 | | PT_UNSIGNED_INT, |
53 | | PT_SHORT, |
54 | | PT_UNSIGNED_SHORT, |
55 | | PT_LONG, |
56 | | PT_UNSIGNED_LONG, |
57 | | PT_STRING, |
58 | | PT_VECTOR3, |
59 | | PT_MATRIX3, |
60 | | PT_MATRIX4, |
61 | | PT_QUATERNION, |
62 | | PT_COLOURVALUE |
63 | | }; |
64 | | |
65 | | /// @deprecated directly pass parameter name |
66 | | class _OgreExport ParameterDef |
67 | | { |
68 | | public: |
69 | | String name; |
70 | | ParameterDef(const String& newName, const String& = "", ParameterType = PT_INT) |
71 | 0 | : name(newName) {} |
72 | | }; |
73 | | typedef std::vector<String> ParameterList; |
74 | | |
75 | | /** Abstract class which is command object which gets/sets parameters.*/ |
76 | | class _OgreExport ParamCommand |
77 | | { |
78 | | public: |
79 | | virtual String doGet(const void* target) const = 0; |
80 | | virtual void doSet(void* target, const String& val) = 0; |
81 | | |
82 | 0 | virtual ~ParamCommand() { } |
83 | | }; |
84 | | typedef std::map<String, ParamCommand* > ParamCommandMap; |
85 | | |
86 | | #ifndef SWIG |
87 | | /** Generic ParamCommand implementation |
88 | | stores pointers to the class getter and setter functions */ |
89 | | template <typename _Class, typename Param, Param (_Class::*getter)() const, void (_Class::*setter)(Param)> |
90 | | class SimpleParamCommand : public ParamCommand { |
91 | | public: |
92 | 0 | String doGet(const void* target) const override { |
93 | 0 | return StringConverter::toString((static_cast<const _Class*>(target)->*getter)()); |
94 | 0 | } |
95 | | |
96 | 0 | void doSet(void* target, const String& val) override { |
97 | 0 | typename std::decay<Param>::type tmp; |
98 | 0 | StringConverter::parse(val, tmp); |
99 | 0 | (static_cast<_Class*>(target)->*setter)(tmp); |
100 | 0 | } |
101 | | }; |
102 | | |
103 | | /// specialization for strings |
104 | | template <typename _Class, const String& (_Class::*getter)() const, void (_Class::*setter)(const String&)> |
105 | | class SimpleParamCommand<_Class, const String&, getter, setter> : public ParamCommand { |
106 | | public: |
107 | | String doGet(const void* target) const override { |
108 | | return (static_cast<const _Class*>(target)->*getter)(); |
109 | | } |
110 | | |
111 | | void doSet(void* target, const String& val) override { |
112 | | (static_cast<_Class*>(target)->*setter)(val); |
113 | | } |
114 | | }; |
115 | | #endif |
116 | | |
117 | | /** Class to hold a dictionary of parameters for a single class. */ |
118 | | class _OgreExport ParamDictionary |
119 | | { |
120 | | friend class StringInterface; |
121 | | /// Definitions of parameters |
122 | | ParameterList mParamDefs; |
123 | | |
124 | | /// Command objects to get/set |
125 | | ParamCommandMap mParamCommands; |
126 | | |
127 | | /** Retrieves the parameter command object for a named parameter. */ |
128 | | ParamCommand* getParamCommand(const String& name); |
129 | | const ParamCommand* getParamCommand(const String& name) const; |
130 | | public: |
131 | | ParamDictionary(); |
132 | | ~ParamDictionary(); |
133 | | /** Method for adding a parameter definition for this class. |
134 | | @param name The name of the parameter |
135 | | @param paramCmd Pointer to a ParamCommand subclass to handle the getting / setting of this parameter. |
136 | | NB this class will not destroy this on shutdown, please ensure you do |
137 | | |
138 | | */ |
139 | | void addParameter(const String& name, ParamCommand* paramCmd); |
140 | | |
141 | | /// @deprecated do not use |
142 | | void addParameter(const ParameterDef& def, ParamCommand* paramCmd) |
143 | 0 | { |
144 | 0 | addParameter(def.name, paramCmd); |
145 | 0 | } |
146 | | /** Retrieves a list of parameters valid for this object. |
147 | | @return |
148 | | A reference to a static list of ParameterDef objects. |
149 | | |
150 | | */ |
151 | | const ParameterList& getParameters(void) const |
152 | 0 | { |
153 | 0 | return mParamDefs; |
154 | 0 | } |
155 | | }; |
156 | | typedef std::map<String, ParamDictionary> ParamDictionaryMap; |
157 | | |
158 | | /** Class defining the common interface which classes can use to |
159 | | present a reflection-style, self-defining parameter set to callers. |
160 | | |
161 | | This class also holds a static map of class name to parameter dictionaries |
162 | | for each subclass to use. See ParamDictionary for details. |
163 | | |
164 | | In order to use this class, each subclass must call createParamDictionary in their constructors |
165 | | which will create a parameter dictionary for the class if it does not exist yet. |
166 | | */ |
167 | | class _OgreExport StringInterface |
168 | | { |
169 | | private: |
170 | | /// Class name for this instance to be used as a lookup (must be initialised by subclasses) |
171 | | String mParamDictName; |
172 | | ParamDictionary* mParamDict; |
173 | | |
174 | | protected: |
175 | | /** Internal method for creating a parameter dictionary for the class, if it does not already exist. |
176 | | |
177 | | This method will check to see if a parameter dictionary exist for this class yet, |
178 | | and if not will create one. NB you must supply the name of the class (RTTI is not |
179 | | used or performance). |
180 | | @param |
181 | | className the name of the class using the dictionary |
182 | | @return |
183 | | true if a new dictionary was created, false if it was already there |
184 | | */ |
185 | | bool createParamDictionary(const String& className); |
186 | | |
187 | | public: |
188 | 0 | StringInterface() : mParamDict(NULL) { } |
189 | | |
190 | | /** Virtual destructor, see Effective C++ */ |
191 | 0 | virtual ~StringInterface() {} |
192 | | |
193 | | /** Retrieves the parameter dictionary for this class. |
194 | | |
195 | | Only valid to call this after createParamDictionary. |
196 | | @return |
197 | | Pointer to ParamDictionary shared by all instances of this class |
198 | | which you can add parameters to, retrieve parameters etc. |
199 | | */ |
200 | | ParamDictionary* getParamDictionary(void) |
201 | 0 | { |
202 | 0 | return mParamDict; |
203 | 0 | } |
204 | | |
205 | | const ParamDictionary* getParamDictionary(void) const |
206 | 0 | { |
207 | 0 | return mParamDict; |
208 | 0 | } |
209 | | |
210 | | /** Retrieves a list of parameters valid for this object. |
211 | | @return |
212 | | A reference to a static list of ParameterDef objects. |
213 | | |
214 | | */ |
215 | | const ParameterList& getParameters(void) const; |
216 | | |
217 | | /** Generic parameter setting method. |
218 | | |
219 | | Call this method with the name of a parameter and a string version of the value |
220 | | to set. The implementor will convert the string to a native type internally. |
221 | | If in doubt, check the parameter definition in the list returned from |
222 | | StringInterface::getParameters. |
223 | | @param |
224 | | name The name of the parameter to set |
225 | | @param |
226 | | value String value. Must be in the right format for the type specified in the parameter definition. |
227 | | See the StringConverter class for more information. |
228 | | @return |
229 | | true if set was successful, false otherwise (NB no exceptions thrown - tolerant method) |
230 | | */ |
231 | | bool setParameter(const String& name, const String& value); |
232 | | /** Generic multiple parameter setting method. |
233 | | |
234 | | Call this method with a list of name / value pairs |
235 | | to set. The implementor will convert the string to a native type internally. |
236 | | If in doubt, check the parameter definition in the list returned from |
237 | | StringInterface::getParameters. |
238 | | @param |
239 | | paramList Name/value pair list |
240 | | */ |
241 | | void setParameterList(const NameValuePairList& paramList); |
242 | | /** Generic parameter retrieval method. |
243 | | |
244 | | Call this method with the name of a parameter to retrieve a string-format value of |
245 | | the parameter in question. If in doubt, check the parameter definition in the |
246 | | list returned from getParameters for the type of this parameter. If you |
247 | | like you can use StringConverter to convert this string back into a native type. |
248 | | @param |
249 | | name The name of the parameter to get |
250 | | @return |
251 | | String value of parameter, blank if not found |
252 | | */ |
253 | | String getParameter(const String& name) const; |
254 | | /** Method for copying this object's parameters to another object. |
255 | | |
256 | | This method takes the values of all the object's parameters and tries to set the |
257 | | same values on the destination object. This provides a completely type independent |
258 | | way to copy parameters to other objects. Note that because of the String manipulation |
259 | | involved, this should not be regarded as an efficient process and should be saved for |
260 | | times outside of the rendering loop. |
261 | | @par |
262 | | Any unrecognised parameters will be ignored as with setParameter method. |
263 | | @param dest Pointer to object to have it's parameters set the same as this object. |
264 | | |
265 | | */ |
266 | | void copyParametersTo(StringInterface* dest) const; |
267 | | /** Cleans up the static 'msDictionary' required to reset Ogre, |
268 | | otherwise the containers are left with invalid pointers, which will lead to a crash |
269 | | as soon as one of the ResourceManager implementers (e.g. MaterialManager) initializes.*/ |
270 | | static void cleanupDictionary () ; |
271 | | |
272 | | }; |
273 | | |
274 | | /** @} */ |
275 | | /** @} */ |
276 | | |
277 | | |
278 | | } |
279 | | |
280 | | #include "OgreHeaderSuffix.h" |
281 | | |
282 | | #endif |
283 | | |