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-2013 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 "OgreString.h" |
---|
34 | #include "OgreCommon.h" |
---|
35 | #include "Threading/OgreThreadHeaders.h" |
---|
36 | #include "OgreHeaderPrefix.h" |
---|
37 | |
---|
38 | namespace Ogre { |
---|
39 | |
---|
40 | /** \addtogroup Core |
---|
41 | * @{ |
---|
42 | */ |
---|
43 | /** \addtogroup General |
---|
44 | * @{ |
---|
45 | */ |
---|
46 | |
---|
47 | /// List of parameter types available |
---|
48 | enum ParameterType |
---|
49 | { |
---|
50 | PT_BOOL, |
---|
51 | PT_REAL, |
---|
52 | PT_INT, |
---|
53 | PT_UNSIGNED_INT, |
---|
54 | PT_SHORT, |
---|
55 | PT_UNSIGNED_SHORT, |
---|
56 | PT_LONG, |
---|
57 | PT_UNSIGNED_LONG, |
---|
58 | PT_STRING, |
---|
59 | PT_VECTOR3, |
---|
60 | PT_MATRIX3, |
---|
61 | PT_MATRIX4, |
---|
62 | PT_QUATERNION, |
---|
63 | PT_COLOURVALUE |
---|
64 | }; |
---|
65 | |
---|
66 | /// Definition of a parameter supported by a StringInterface class, for introspection |
---|
67 | class _OgreExport ParameterDef |
---|
68 | { |
---|
69 | public: |
---|
70 | String name; |
---|
71 | String description; |
---|
72 | ParameterType paramType; |
---|
73 | ParameterDef(const String& newName, const String& newDescription, ParameterType newType) |
---|
74 | : name(newName), description(newDescription), paramType(newType) {} |
---|
75 | }; |
---|
76 | typedef vector<ParameterDef>::type ParameterList; |
---|
77 | |
---|
78 | /** Abstract class which is command object which gets/sets parameters.*/ |
---|
79 | class _OgreExport ParamCommand |
---|
80 | { |
---|
81 | public: |
---|
82 | virtual String doGet(const void* target) const = 0; |
---|
83 | virtual void doSet(void* target, const String& val) = 0; |
---|
84 | |
---|
85 | virtual ~ParamCommand() { } |
---|
86 | }; |
---|
87 | typedef map<String, ParamCommand* >::type ParamCommandMap; |
---|
88 | |
---|
89 | /** Class to hold a dictionary of parameters for a single class. */ |
---|
90 | class _OgreExport ParamDictionary |
---|
91 | { |
---|
92 | friend class StringInterface; |
---|
93 | protected: |
---|
94 | /// Definitions of parameters |
---|
95 | ParameterList mParamDefs; |
---|
96 | |
---|
97 | /// Command objects to get/set |
---|
98 | ParamCommandMap mParamCommands; |
---|
99 | |
---|
100 | /** Retrieves the parameter command object for a named parameter. */ |
---|
101 | ParamCommand* getParamCommand(const String& name) |
---|
102 | { |
---|
103 | ParamCommandMap::iterator i = mParamCommands.find(name); |
---|
104 | if (i != mParamCommands.end()) |
---|
105 | { |
---|
106 | return i->second; |
---|
107 | } |
---|
108 | else |
---|
109 | { |
---|
110 | return 0; |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | const ParamCommand* getParamCommand(const String& name) const |
---|
115 | { |
---|
116 | ParamCommandMap::const_iterator i = mParamCommands.find(name); |
---|
117 | if (i != mParamCommands.end()) |
---|
118 | { |
---|
119 | return i->second; |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | return 0; |
---|
124 | } |
---|
125 | } |
---|
126 | public: |
---|
127 | ParamDictionary() {} |
---|
128 | /** Method for adding a parameter definition for this class. |
---|
129 | @param paramDef A ParameterDef object defining the parameter |
---|
130 | @param paramCmd Pointer to a ParamCommand subclass to handle the getting / setting of this parameter. |
---|
131 | NB this class will not destroy this on shutdown, please ensure you do |
---|
132 | |
---|
133 | */ |
---|
134 | void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd) |
---|
135 | { |
---|
136 | mParamDefs.push_back(paramDef); |
---|
137 | mParamCommands[paramDef.name] = paramCmd; |
---|
138 | } |
---|
139 | /** Retrieves a list of parameters valid for this object. |
---|
140 | @return |
---|
141 | A reference to a static list of ParameterDef objects. |
---|
142 | |
---|
143 | */ |
---|
144 | const ParameterList& getParameters(void) const |
---|
145 | { |
---|
146 | return mParamDefs; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | }; |
---|
152 | typedef map<String, ParamDictionary>::type ParamDictionaryMap; |
---|
153 | |
---|
154 | /** Class defining the common interface which classes can use to |
---|
155 | present a reflection-style, self-defining parameter set to callers. |
---|
156 | @remarks |
---|
157 | This class also holds a static map of class name to parameter dictionaries |
---|
158 | for each subclass to use. See ParamDictionary for details. |
---|
159 | @remarks |
---|
160 | In order to use this class, each subclass must call createParamDictionary in their constructors |
---|
161 | which will create a parameter dictionary for the class if it does not exist yet. |
---|
162 | */ |
---|
163 | class _OgreExport StringInterface |
---|
164 | { |
---|
165 | private: |
---|
166 | OGRE_STATIC_MUTEX( msDictionaryMutex ); |
---|
167 | |
---|
168 | /// Dictionary of parameters |
---|
169 | static ParamDictionaryMap msDictionary; |
---|
170 | |
---|
171 | /// Class name for this instance to be used as a lookup (must be initialised by subclasses) |
---|
172 | String mParamDictName; |
---|
173 | ParamDictionary* mParamDict; |
---|
174 | |
---|
175 | protected: |
---|
176 | /** Internal method for creating a parameter dictionary for the class, if it does not already exist. |
---|
177 | @remarks |
---|
178 | This method will check to see if a parameter dictionary exist for this class yet, |
---|
179 | and if not will create one. NB you must supply the name of the class (RTTI is not |
---|
180 | used or performance). |
---|
181 | @param |
---|
182 | className the name of the class using the dictionary |
---|
183 | @return |
---|
184 | true if a new dictionary was created, false if it was already there |
---|
185 | */ |
---|
186 | bool createParamDictionary(const String& className) |
---|
187 | { |
---|
188 | OGRE_LOCK_MUTEX( msDictionaryMutex ); |
---|
189 | |
---|
190 | ParamDictionaryMap::iterator it = msDictionary.find(className); |
---|
191 | |
---|
192 | if ( it == msDictionary.end() ) |
---|
193 | { |
---|
194 | mParamDict = &msDictionary.insert( std::make_pair( className, ParamDictionary() ) ).first->second; |
---|
195 | mParamDictName = className; |
---|
196 | return true; |
---|
197 | } |
---|
198 | else |
---|
199 | { |
---|
200 | mParamDict = &it->second; |
---|
201 | mParamDictName = className; |
---|
202 | return false; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | public: |
---|
207 | StringInterface() : mParamDict(NULL) { } |
---|
208 | |
---|
209 | /** Virtual destructor, see Effective C++ */ |
---|
210 | virtual ~StringInterface() {} |
---|
211 | |
---|
212 | /** Retrieves the parameter dictionary for this class. |
---|
213 | @remarks |
---|
214 | Only valid to call this after createParamDictionary. |
---|
215 | @return |
---|
216 | Pointer to ParamDictionary shared by all instances of this class |
---|
217 | which you can add parameters to, retrieve parameters etc. |
---|
218 | */ |
---|
219 | ParamDictionary* getParamDictionary(void) |
---|
220 | { |
---|
221 | return mParamDict; |
---|
222 | } |
---|
223 | |
---|
224 | const ParamDictionary* getParamDictionary(void) const |
---|
225 | { |
---|
226 | return mParamDict; |
---|
227 | } |
---|
228 | |
---|
229 | /** Retrieves a list of parameters valid for this object. |
---|
230 | @return |
---|
231 | A reference to a static list of ParameterDef objects. |
---|
232 | |
---|
233 | */ |
---|
234 | const ParameterList& getParameters(void) const; |
---|
235 | |
---|
236 | /** Generic parameter setting method. |
---|
237 | @remarks |
---|
238 | Call this method with the name of a parameter and a string version of the value |
---|
239 | to set. The implementor will convert the string to a native type internally. |
---|
240 | If in doubt, check the parameter definition in the list returned from |
---|
241 | StringInterface::getParameters. |
---|
242 | @param |
---|
243 | name The name of the parameter to set |
---|
244 | @param |
---|
245 | value String value. Must be in the right format for the type specified in the parameter definition. |
---|
246 | See the StringConverter class for more information. |
---|
247 | @return |
---|
248 | true if set was successful, false otherwise (NB no exceptions thrown - tolerant method) |
---|
249 | */ |
---|
250 | virtual bool setParameter(const String& name, const String& value); |
---|
251 | /** Generic multiple parameter setting method. |
---|
252 | @remarks |
---|
253 | Call this method with a list of name / value pairs |
---|
254 | to set. The implementor will convert the string to a native type internally. |
---|
255 | If in doubt, check the parameter definition in the list returned from |
---|
256 | StringInterface::getParameters. |
---|
257 | @param |
---|
258 | paramList Name/value pair list |
---|
259 | */ |
---|
260 | virtual void setParameterList(const NameValuePairList& paramList); |
---|
261 | /** Generic parameter retrieval method. |
---|
262 | @remarks |
---|
263 | Call this method with the name of a parameter to retrieve a string-format value of |
---|
264 | the parameter in question. If in doubt, check the parameter definition in the |
---|
265 | list returned from getParameters for the type of this parameter. If you |
---|
266 | like you can use StringConverter to convert this string back into a native type. |
---|
267 | @param |
---|
268 | name The name of the parameter to get |
---|
269 | @return |
---|
270 | String value of parameter, blank if not found |
---|
271 | */ |
---|
272 | virtual String getParameter(const String& name) const |
---|
273 | { |
---|
274 | // Get dictionary |
---|
275 | const ParamDictionary* dict = getParamDictionary(); |
---|
276 | |
---|
277 | if (dict) |
---|
278 | { |
---|
279 | // Look up command object |
---|
280 | const ParamCommand* cmd = dict->getParamCommand(name); |
---|
281 | |
---|
282 | if (cmd) |
---|
283 | { |
---|
284 | return cmd->doGet(this); |
---|
285 | } |
---|
286 | } |
---|
287 | |
---|
288 | // Fallback |
---|
289 | return ""; |
---|
290 | } |
---|
291 | /** Method for copying this object's parameters to another object. |
---|
292 | @remarks |
---|
293 | This method takes the values of all the object's parameters and tries to set the |
---|
294 | same values on the destination object. This provides a completely type independent |
---|
295 | way to copy parameters to other objects. Note that because of the String manipulation |
---|
296 | involved, this should not be regarded as an efficient process and should be saved for |
---|
297 | times outside of the rendering loop. |
---|
298 | @par |
---|
299 | Any unrecognised parameters will be ignored as with setParameter method. |
---|
300 | @param dest Pointer to object to have it's parameters set the same as this object. |
---|
301 | |
---|
302 | */ |
---|
303 | virtual void copyParametersTo(StringInterface* dest) const |
---|
304 | { |
---|
305 | // Get dictionary |
---|
306 | const ParamDictionary* dict = getParamDictionary(); |
---|
307 | |
---|
308 | if (dict) |
---|
309 | { |
---|
310 | // Iterate through own parameters |
---|
311 | ParameterList::const_iterator i; |
---|
312 | |
---|
313 | for (i = dict->mParamDefs.begin(); |
---|
314 | i != dict->mParamDefs.end(); ++i) |
---|
315 | { |
---|
316 | dest->setParameter(i->name, getParameter(i->name)); |
---|
317 | } |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | } |
---|
322 | |
---|
323 | /** Cleans up the static 'msDictionary' required to reset Ogre, |
---|
324 | otherwise the containers are left with invalid pointers, which will lead to a crash |
---|
325 | as soon as one of the ResourceManager implementers (e.g. MaterialManager) initializes.*/ |
---|
326 | static void cleanupDictionary () ; |
---|
327 | |
---|
328 | }; |
---|
329 | |
---|
330 | /** @} */ |
---|
331 | /** @} */ |
---|
332 | |
---|
333 | |
---|
334 | } |
---|
335 | |
---|
336 | #include "OgreHeaderSuffix.h" |
---|
337 | |
---|
338 | #endif |
---|
339 | |
---|