OGRE  1.9
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-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 "OgreString.h"
34 #include "OgreCommon.h"
36 #include "OgreHeaderPrefix.h"
37 
38 namespace Ogre {
39 
47  enum ParameterType
49  {
64  };
65 
68  {
69  public:
73  ParameterDef(const String& newName, const String& newDescription, ParameterType newType)
74  : name(newName), description(newDescription), paramType(newType) {}
75  };
77 
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  };
88 
91  {
92  friend class StringInterface;
93  protected:
96 
99 
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:
134  void addParameter(const ParameterDef& paramDef, ParamCommand* paramCmd)
135  {
136  mParamDefs.push_back(paramDef);
137  mParamCommands[paramDef.name] = paramCmd;
138  }
144  const ParameterList& getParameters(void) const
145  {
146  return mParamDefs;
147  }
148 
149 
150 
151  };
153 
164  {
165  private:
166  OGRE_STATIC_MUTEX( msDictionaryMutex );
167 
170 
174 
175  protected:
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 
210  virtual ~StringInterface() {}
211 
220  {
221  return mParamDict;
222  }
223 
225  {
226  return mParamDict;
227  }
228 
234  const ParameterList& getParameters(void) const;
235 
250  virtual bool setParameter(const String& name, const String& value);
260  virtual void setParameterList(const NameValuePairList& paramList);
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  }
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 
326  static void cleanupDictionary () ;
327 
328  };
329 
334 }
335 
336 #include "OgreHeaderSuffix.h"
337 
338 #endif
339 
#define _OgreExport
Definition: OgrePlatform.h:260
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:550
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
bool createParamDictionary(const String &className)
Internal method for creating a parameter dictionary for the class, if it does not already exist...
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
String mParamDictName
Class name for this instance to be used as a lookup (must be initialised by subclasses) ...
map< String, ParamDictionary >::type ParamDictionaryMap
std::map< K, V, P, A > type
virtual String getParameter(const String &name) const
Generic parameter retrieval method.
ParameterType
List of parameter types available.
vector< ParameterDef >::type ParameterList
#define OGRE_STATIC_MUTEX(name)
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
static ParamDictionaryMap msDictionary
Dictionary of parameters.
virtual String doGet(const void *target) const =0
virtual void copyParametersTo(StringInterface *dest) const
Method for copying this object's parameters to another object.
ParamDictionary * mParamDict
Class to hold a dictionary of parameters for a single class.
#define OGRE_LOCK_MUTEX(name)
Abstract class which is command object which gets/sets parameters.