OGRE  1.8
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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-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 "OgreHeaderPrefix.h"
36 
37 namespace Ogre {
38 
46  enum ParameterType
48  {
63  };
64 
67  {
68  public:
72  ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
73  : name(newName), description(newDescription), paramType(newType) {}
74  };
76 
79  {
80  public:
81  virtual String doGet(const void* target) const = 0;
82  virtual void doSet(void* target, const String& val) = 0;
83 
84  virtual ~ParamCommand() { }
85  };
87 
90  {
91  friend class StringInterface;
92  protected:
95 
98 
101  {
102  ParamCommandMap::iterator i = mParamCommands.find(name);
103  if (i != mParamCommands.end())
104  {
105  return i->second;
106  }
107  else
108  {
109  return 0;
110  }
111  }
112 
113  const ParamCommand* getParamCommand(const String& name) const
114  {
115  ParamCommandMap::const_iterator i = mParamCommands.find(name);
116  if (i != mParamCommands.end())
117  {
118  return i->second;
119  }
120  else
121  {
122  return 0;
123  }
124  }
125  public:
133  void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
134  {
135  mParamDefs.push_back(paramDef);
136  mParamCommands[paramDef.name] = paramCmd;
137  }
143  const ParameterList& getParameters(void) const
144  {
145  return mParamDefs;
146  }
147 
148 
149 
150  };
152 
163  {
164  private:
165  OGRE_STATIC_MUTEX( msDictionaryMutex )
166 
167 
168  static ParamDictionaryMap msDictionary;
169 
171  String mParamDictName;
172  ParamDictionary* mParamDict;
173 
174  protected:
185  bool createParamDictionary(const String& className)
186  {
187  OGRE_LOCK_MUTEX( msDictionaryMutex )
188 
189  ParamDictionaryMap::iterator it = msDictionary.find(className);
190 
191  if ( it == msDictionary.end() )
192  {
193  mParamDict = &msDictionary.insert( std::make_pair( className, ParamDictionary() ) ).first->second;
194  mParamDictName = className;
195  return true;
196  }
197  else
198  {
199  mParamDict = &it->second;
200  mParamDictName = className;
201  return false;
202  }
203  }
204 
205  public:
206  StringInterface() : mParamDict(NULL) { }
207 
209  virtual ~StringInterface() {}
210 
219  {
220  return mParamDict;
221  }
222 
224  {
225  return mParamDict;
226  }
227 
233  const ParameterList& getParameters(void) const;
234 
249  virtual bool setParameter(const String& name, const String& value);
259  virtual void setParameterList(const NameValuePairList& paramList);
271  virtual String getParameter(const String& name) const
272  {
273  // Get dictionary
274  const ParamDictionary* dict = getParamDictionary();
275 
276  if (dict)
277  {
278  // Look up command object
279  const ParamCommand* cmd = dict->getParamCommand(name);
280 
281  if (cmd)
282  {
283  return cmd->doGet(this);
284  }
285  }
286 
287  // Fallback
288  return "";
289  }
302  virtual void copyParametersTo(StringInterface* dest) const
303  {
304  // Get dictionary
305  const ParamDictionary* dict = getParamDictionary();
306 
307  if (dict)
308  {
309  // Iterate through own parameters
310  ParameterList::const_iterator i;
311 
312  for (i = dict->mParamDefs.begin();
313  i != dict->mParamDefs.end(); ++i)
314  {
315  dest->setParameter(i->name, getParameter(i->name));
316  }
317  }
318 
319 
320  }
321 
325  static void cleanupDictionary () ;
326 
327  };
328 
333 }
334 
335 #include "OgreHeaderSuffix.h"
336 
337 #endif
338 
#define _OgreExport
Definition: OgrePlatform.h:233
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:553
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.