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