OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreProperty.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 #ifndef __OGRE_PROPERTY_H__
29 #define __OGRE_PROPERTY_H__
30 
32 #include "OgreAny.h"
33 #include "OgreIteratorWrappers.h"
34 #include "OgreString.h"
35 #include "OgreException.h"
36 #include "OgreQuaternion.h"
37 #include "OgreMatrix4.h"
38 #include <boost/bind.hpp>
39 #include <boost/function.hpp>
40 
95 namespace Ogre
96 {
104  enum PropertyType
106  {
109  PROP_INT = 2,
111  PROP_LONG = 4,
119  PROP_BOOL = 12,
123 
125  };
126 
133  {
134  public:
135 
141  PropertyDef(const String& name, const String& desc, PropertyType pType)
142  : mName(name), mDesc(desc), mType(pType) {}
143 
145  const String& getName() const { return mName; }
146 
148  const String& getDescription() const { return mDesc; }
149 
151  PropertyType getType() const { return mType; }
152 
154  static const String& getTypeName(PropertyType theType);
155 
156  static PropertyType getTypeForValue(const short& val) { return PROP_SHORT; }
157  static PropertyType getTypeForValue(const unsigned short& val) { return PROP_UNSIGNED_SHORT; }
158  static PropertyType getTypeForValue(const int& val) { return PROP_INT; }
159  static PropertyType getTypeForValue(const unsigned int& val) { return PROP_UNSIGNED_INT; }
160  static PropertyType getTypeForValue(const long& val) { return PROP_LONG; }
161  static PropertyType getTypeForValue(const unsigned long& val) { return PROP_UNSIGNED_LONG; }
162  static PropertyType getTypeForValue(const Real& val) { return PROP_REAL; }
163  static PropertyType getTypeForValue(const String& val) { return PROP_STRING; }
164  static PropertyType getTypeForValue(const Vector2& val) { return PROP_VECTOR2; }
165  static PropertyType getTypeForValue(const Vector3& val) { return PROP_VECTOR3; }
166  static PropertyType getTypeForValue(const Vector4& val) { return PROP_VECTOR4; }
167  static PropertyType getTypeForValue(const ColourValue& val) { return PROP_COLOUR; }
168  static PropertyType getTypeForValue(const bool& val) { return PROP_BOOL; }
170  static PropertyType getTypeForValue(const Matrix3& val) { return PROP_MATRIX3; }
171  static PropertyType getTypeForValue(const Matrix4& val) { return PROP_MATRIX4; }
172 
173  protected:
174  // no default construction
176 
180 
181  };
182 
185 
189  {
190  public:
192  PropertyBase(PropertyDef* def) : mDef(def) {}
193  virtual ~PropertyBase() {}
194 
196  const String& getName() const { return mDef->getName(); }
197 
199  const String& getDescription() const { return mDef->getDescription(); }
200 
202  PropertyType getType() const { return mDef->getType(); }
203 
205  virtual Ogre::Any getValue() const = 0;
206 
207  protected:
208  // disallow default construction
211 
212  };
213 
215  template <typename T>
216  class Property : public PropertyBase
217  {
218  public:
219  typedef T value_type;
220  typedef boost::function< T (void) > getter_func;
221  typedef boost::function< void (T) > setter_func;
222 
227  : PropertyBase(def)
228  , mGetter(getter)
229  , mSetter(setter)
230  {
231  }
232 
235  virtual void set(T val)
236  {
237  mSetter(val);
238  }
239 
240  virtual T get() const
241  {
242  return mGetter();
243  }
244 
246  {
247  return Ogre::Any(get());
248  }
249 
250  protected:
251  // disallow default construction
252  Property() {}
254 
257  };
258 
264  {
267  };
270 
271 
275  {
276  public:
277  PropertySet();
278  ~PropertySet();
279 
284  void addProperty(PropertyBase* prop);
285 
293  PropertyBase* getProperty(const String& name) const;
294 
296  bool hasProperty(const String& name) const;
297 
299  void removeProperty(const String& name);
300 
304  PropertyIterator getPropertyIterator();
305 
309  PropertyValueMap getValueMap() const;
310 
313  void setValueMap(const PropertyValueMap& values);
314 
317  template<typename T>
318  void getValue(const String& name, T& value) const
319  {
320  getPropertyImpl(name, value, PropertyDef::getTypeForValue(value));
321  }
322 
325  template<typename T>
326  void setValue(const String& name, const T* value)
327  {
328  setPropertyImpl(name, *value, PropertyDef::getTypeForValue(*value));
329  }
332  template<typename T>
333  void setValue(const String& name, T value)
334  {
335  setPropertyImpl(name, value, PropertyDef::getTypeForValue(value));
336  }
339  void setValue(const String& name, const char* pChar)
340  {
341  String v(pChar);
342  setPropertyImpl(name, v, PROP_STRING);
343  }
344 
345 
346  protected:
348 
350  template <typename T>
351  void setPropertyImpl(const String& name, const T& val, PropertyType typeCheck)
352  {
353  PropertyBase* baseProp = getProperty(name);
354  if (baseProp->getType() != typeCheck)
355  {
356  StringStream msg;
357  msg << "Property error: type passed in: '" << PropertyDef::getTypeName(typeCheck)
358  << "', type of property: '" << PropertyDef::getTypeName(baseProp->getType()) << "'";
359  OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, msg.str(), "PropertySet::setPropertyImpl");
360  }
361  static_cast<Property<T>*>(baseProp)->set(val);
362  }
363 
365  template <typename T>
366  void getPropertyImpl(const String& name, T& refVal, PropertyType typeCheck) const
367  {
368  PropertyBase* baseProp = getProperty(name);
369  if (baseProp->getType() != typeCheck)
370  {
371  StringStream msg;
372  msg << "Property error: type requested: '" << PropertyDef::getTypeName(typeCheck)
373  << "', type of property: '" << PropertyDef::getTypeName(baseProp->getType()) << "'";
374  OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, msg.str(), "PropertySet::getPropertyImpl");
375  }
376  refVal = static_cast<Property<T>*>(baseProp)->get();
377  }
378 
379  };
380 
384 }
385 
386 #endif
static PropertyType getTypeForValue(const Matrix3 &val)
Definition: OgreProperty.h:170
Ogre::Any getValue() const
Return the current value as an Any.
Definition: OgreProperty.h:245
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:79
static PropertyType getTypeForValue(const bool &val)
Definition: OgreProperty.h:168
static PropertyType getTypeForValue(const Vector4 &val)
Definition: OgreProperty.h:166
float Real
Software floating point type.
PropertyMap mPropertyMap
Definition: OgreProperty.h:347
Variant type that can hold Any other type.
Definition: OgreAny.h:54
void getValue(const String &name, T &value) const
Get a named property value.
Definition: OgreProperty.h:318
static PropertyType getTypeForValue(const short &val)
Definition: OgreProperty.h:156
Class representing colour.
PropertyType mType
Definition: OgreProperty.h:179
static PropertyType getTypeForValue(const Vector3 &val)
Definition: OgreProperty.h:165
void setValue(const String &name, const T *value)
Set a named property value (via pointer to avoid copy).
Definition: OgreProperty.h:326
PropertyType propType
Definition: OgreProperty.h:265
getter_func mGetter
Definition: OgreProperty.h:255
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:68
void setValue(const String &name, T value)
Set a named property value.
Definition: OgreProperty.h:333
void getPropertyImpl(const String &name, T &refVal, PropertyType typeCheck) const
Get a named property value, internal implementation (type match required)
Definition: OgreProperty.h:366
_StringStreamBase StringStream
static const String & getTypeName(PropertyType theType)
Get a string name of a property type.
void setPropertyImpl(const String &name, const T &val, PropertyType typeCheck)
Set a named property value, internal implementation (type match required)
Definition: OgreProperty.h:351
Implementation of a Quaternion, i.e.
Concrete IteratorWrapper for nonconst access to the underlying key-value container.
static PropertyType getTypeForValue(const Vector2 &val)
Definition: OgreProperty.h:164
static PropertyType getTypeForValue(const String &val)
Definition: OgreProperty.h:163
PropertyType getType() const
Get the type of the property.
Definition: OgreProperty.h:151
const String & getDescription() const
Get the description of the property.
Definition: OgreProperty.h:199
setter_func mSetter
Definition: OgreProperty.h:256
Ogre::MapIterator< PropertyMap > PropertyIterator
Definition: OgreProperty.h:302
static PropertyType getTypeForValue(const long &val)
Definition: OgreProperty.h:160
#define OGRE_EXCEPT(num, desc, src)
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
const String & getName() const
Get the name of the property.
Definition: OgreProperty.h:145
const String & getDescription() const
Get the description of the property.
Definition: OgreProperty.h:148
boost::function< T(void) > getter_func
Definition: OgreProperty.h:220
static PropertyType getTypeForValue(const unsigned int &val)
Definition: OgreProperty.h:159
PropertyDef * mDef
Definition: OgreProperty.h:210
static PropertyType getTypeForValue(const unsigned short &val)
Definition: OgreProperty.h:157
Standard 3-dimensional vector.
Definition: OgreVector3.h:50
Base interface for an instance of a property.
Definition: OgreProperty.h:188
std::map< K, V, P, A > type
static PropertyType getTypeForValue(const unsigned long &val)
Definition: OgreProperty.h:161
static PropertyType getTypeForValue(const int &val)
Definition: OgreProperty.h:158
static PropertyType getTypeForValue(const ColourValue &val)
Definition: OgreProperty.h:167
static PropertyType getTypeForValue(const Matrix4 &val)
Definition: OgreProperty.h:171
A simple structure designed just as a holder of property values between the instances of objects they...
Definition: OgreProperty.h:263
Property instance with passthrough calls to a given object.
Definition: OgreProperty.h:216
virtual ~PropertyBase()
Definition: OgreProperty.h:193
PropertyType
The type of a property.
Definition: OgreProperty.h:105
Defines a complete set of properties for a single object instance.
Definition: OgreProperty.h:274
Definition of a property of an object.
Definition: OgreProperty.h:132
static PropertyType getTypeForValue(const Real &val)
Definition: OgreProperty.h:162
boost::function< void(T) > setter_func
Definition: OgreProperty.h:221
_StringBase String
Definition: OgreCommon.h:53
#define _OgrePropertyExport
4-dimensional homogeneous vector.
Definition: OgreVector4.h:45
PropertyBase(PropertyDef *def)
Constructor.
Definition: OgreProperty.h:192
PropertyType getType() const
Get the type of the property.
Definition: OgreProperty.h:202
PropertyDef(const String &name, const String &desc, PropertyType pType)
Construct a property.
Definition: OgreProperty.h:141
map< String, PropertyValue >::type PropertyValueMap
Defines a transferable map of properties using wrapped value types (Ogre::Any)
Definition: OgreProperty.h:269
map< String, PropertyBase * >::type PropertyMap
Definition: OgreProperty.h:301
const String & getName() const
Get the name of the property.
Definition: OgreProperty.h:196
map< String, PropertyDef >::type PropertyDefMap
Map from property name to shared definition.
Definition: OgreProperty.h:184
virtual void set(T val)
Set the property value.
Definition: OgreProperty.h:235
static PropertyType getTypeForValue(const Quaternion &val)
Definition: OgreProperty.h:169
Property(PropertyDef *def, getter_func getter, setter_func setter)
Construct a property which is able to directly call a given getter and setter on a specific object in...
Definition: OgreProperty.h:226
void setValue(const String &name, const char *pChar)
Special-case char*, convert to String automatically.
Definition: OgreProperty.h:339