OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OgreStringInterface.h
Go to the documentation of this file.
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-2011 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 
36 namespace Ogre {
37 
45  enum ParameterType
47  {
62  };
63 
66  {
67  public:
71  ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
72  : name(newName), description(newDescription), paramType(newType) {}
73  };
75 
78  {
79  public:
80  virtual String doGet(const void* target) const = 0;
81  virtual void doSet(void* target, const String& val) = 0;
82 
83  virtual ~ParamCommand() { }
84  };
86 
89  {
90  friend class StringInterface;
91  protected:
94 
97 
100  {
101  ParamCommandMap::iterator i = mParamCommands.find(name);
102  if (i != mParamCommands.end())
103  {
104  return i->second;
105  }
106  else
107  {
108  return 0;
109  }
110  }
111 
112  const ParamCommand* getParamCommand(const String& name) const
113  {
114  ParamCommandMap::const_iterator i = mParamCommands.find(name);
115  if (i != mParamCommands.end())
116  {
117  return i->second;
118  }
119  else
120  {
121  return 0;
122  }
123  }
124  public:
132  void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
133  {
134  mParamDefs.push_back(paramDef);
135  mParamCommands[paramDef.name] = paramCmd;
136  }
142  const ParameterList& getParameters(void) const
143  {
144  return mParamDefs;
145  }
146 
147 
148 
149  };
151 
162  {
163  private:
164  OGRE_STATIC_MUTEX( msDictionaryMutex )
165 
166 
167  static ParamDictionaryMap msDictionary;
168 
170  String mParamDictName;
171  ParamDictionary* mParamDict;
172 
173  protected:
184  bool createParamDictionary(const String& className)
185  {
186  OGRE_LOCK_MUTEX( msDictionaryMutex )
187 
188  ParamDictionaryMap::iterator it = msDictionary.find(className);
189 
190  if ( it == msDictionary.end() )
191  {
192  mParamDict = &msDictionary.insert( std::make_pair( className, ParamDictionary() ) ).first->second;
193  mParamDictName = className;
194  return true;
195  }
196  else
197  {
198  mParamDict = &it->second;
199  mParamDictName = className;
200  return false;
201  }
202  }
203 
204  public:
205  StringInterface() : mParamDict(NULL) { }
206 
208  virtual ~StringInterface() {}
209 
218  {
219  return mParamDict;
220  }
221 
223  {
224  return mParamDict;
225  }
226 
232  const ParameterList& getParameters(void) const;
233 
248  virtual bool setParameter(const String& name, const String& value);
258  virtual void setParameterList(const NameValuePairList& paramList);
270  virtual String getParameter(const String& name) const
271  {
272  // Get dictionary
273  const ParamDictionary* dict = getParamDictionary();
274 
275  if (dict)
276  {
277  // Look up command object
278  const ParamCommand* cmd = dict->getParamCommand(name);
279 
280  if (cmd)
281  {
282  return cmd->doGet(this);
283  }
284  }
285 
286  // Fallback
287  return "";
288  }
301  virtual void copyParametersTo(StringInterface* dest) const
302  {
303  // Get dictionary
304  const ParamDictionary* dict = getParamDictionary();
305 
306  if (dict)
307  {
308  // Iterate through own parameters
309  ParameterList::const_iterator i;
310 
311  for (i = dict->mParamDefs.begin();
312  i != dict->mParamDefs.end(); ++i)
313  {
314  dest->setParameter(i->name, getParameter(i->name));
315  }
316  }
317 
318 
319  }
320 
324  static void cleanupDictionary () ;
325 
326  };
327 
332 }
333 
334 #endif
335 
#define _OgreExport
Definition: OgrePlatform.h:203
void addParameter(const ParameterDef &paramDef, ParamCommand *paramCmd)
Method for adding a parameter definition for this class.
ParameterList mParamDefs
Definitions of parameters.
virtual bool setParameter(const String &name, const String &value)
Generic parameter setting method.
map< String, String >::type NameValuePairList
Name / value parameter pair (first = name, second = value)
Definition: OgreCommon.h:524
ParamCommand * getParamCommand(const String &name)
Retrieves the parameter command object for a named parameter.
ParamDictionary * getParamDictionary(void)
Retrieves the parameter dictionary for this class.
ParamCommandMap mParamCommands
Command objects to get/set.
const ParamCommand * getParamCommand(const String &name) const
#define OGRE_STATIC_MUTEX(name)
Class defining the common interface which classes can use to present a reflection-style, self-defining parameter set to callers.
Definition of a parameter supported by a StringInterface class, for introspection.
const ParamDictionary * getParamDictionary(void) const
virtual ~StringInterface()
Virtual destructor, see Effective C++.
map< String, ParamCommand * >::type ParamCommandMap
map< String, ParamDictionary >::type ParamDictionaryMap
std::map< K, V, P, A > type
virtual String getParameter(const String &name) const
Generic parameter retrieval method.
#define OGRE_LOCK_MUTEX(name)
ParameterType
List of parameter types available.
vector< ParameterDef >::type ParameterList
ParameterDef(const String &newName, const String &newDescription, ParameterType newType)
const ParameterList & getParameters(void) const
Retrieves a list of parameters valid for this object.
_StringBase String
virtual String doGet(const void *target) const =0
virtual void copyParametersTo(StringInterface *dest) const
Method for copying this object's parameters to another object.
Class to hold a dictionary of parameters for a single class.
Abstract class which is command object which gets/sets parameters.