OgreProperty.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2013 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE SOFTWARE.
26-----------------------------------------------------------------------------
27*/
28#ifndef __OGRE_PROPERTY_H__
29#define __OGRE_PROPERTY_H__
30
32#include "OgreAny.h"
34#include "OgreString.h"
35#include "OgreException.h"
36#include "OgreVector2.h"
37#include "OgreVector3.h"
38#include "OgreVector4.h"
39#include "OgreColourValue.h"
40#include "OgreQuaternion.h"
41#include "OgreMatrix3.h"
42#include "OgreMatrix4.h"
43
44#include <boost/bind.hpp>
45#include <boost/function.hpp>
100namespace Ogre
101{
111 {
128
129 PROP_UNKNOWN = 999
130 };
131
138 {
139 public:
140
146 PropertyDef(const String& name, const String& desc, PropertyType pType)
147 : mName(name), mDesc(desc), mType(pType) {}
148
150 const String& getName() const { return mName; }
151
153 const String& getDescription() const { return mDesc; }
154
156 PropertyType getType() const { return mType; }
157
159 static const String& getTypeName(PropertyType theType);
160
161 static PropertyType getTypeForValue(const short& val) { return PROP_SHORT; }
162 static PropertyType getTypeForValue(const unsigned short& val) { return PROP_UNSIGNED_SHORT; }
163 static PropertyType getTypeForValue(const int& val) { return PROP_INT; }
164 static PropertyType getTypeForValue(const unsigned int& val) { return PROP_UNSIGNED_INT; }
165 static PropertyType getTypeForValue(const long& val) { return PROP_LONG; }
166 static PropertyType getTypeForValue(const unsigned long& val) { return PROP_UNSIGNED_LONG; }
167 static PropertyType getTypeForValue(const Real& val) { return PROP_REAL; }
168 static PropertyType getTypeForValue(const String& val) { return PROP_STRING; }
169 static PropertyType getTypeForValue(const Vector2& val) { return PROP_VECTOR2; }
170 static PropertyType getTypeForValue(const Vector3& val) { return PROP_VECTOR3; }
171 static PropertyType getTypeForValue(const Vector4& val) { return PROP_VECTOR4; }
173 static PropertyType getTypeForValue(const bool& val) { return PROP_BOOL; }
175 static PropertyType getTypeForValue(const Matrix3& val) { return PROP_MATRIX3; }
176 static PropertyType getTypeForValue(const Matrix4& val) { return PROP_MATRIX4; }
177
178 protected:
179 // no default construction
181
185
186 };
187
190
194 {
195 public:
197 PropertyBase(PropertyDef* def) : mDef(def) {}
198 virtual ~PropertyBase() {}
199
201 const String& getName() const { return mDef->getName(); }
202
204 const String& getDescription() const { return mDef->getDescription(); }
205
207 PropertyType getType() const { return mDef->getType(); }
208
210 virtual Ogre::Any getValue() const = 0;
211
212 protected:
213 // disallow default construction
216
217 };
218
220 template <typename T>
221 class Property : public PropertyBase
222 {
223 public:
224 typedef T value_type;
225 typedef boost::function< T (void) > getter_func;
226 typedef boost::function< void (T) > setter_func;
227
232 : PropertyBase(def)
233 , mGetter(getter)
234 , mSetter(setter)
235 {
236 }
237
240 virtual void set(T val)
241 {
242 mSetter(val);
243 }
244
245 virtual T get() const
246 {
247 return mGetter();
248 }
249
251 {
252 return Ogre::Any(get());
253 }
254
255 protected:
256 // disallow default construction
259
262 };
263
269 {
272 };
275
276
280 {
281 public:
284
290
298 PropertyBase* getProperty(const String& name) const;
299
301 bool hasProperty(const String& name) const;
302
304 void removeProperty(const String& name);
305
310
315
318 void setValueMap(const PropertyValueMap& values);
319
322 template<typename T>
323 void getValue(const String& name, T& value) const
324 {
325 getPropertyImpl(name, value, PropertyDef::getTypeForValue(value));
326 }
327
330 template<typename T>
331 void setValue(const String& name, const T* value)
332 {
333 setPropertyImpl(name, *value, PropertyDef::getTypeForValue(*value));
334 }
337 template<typename T>
338 void setValue(const String& name, T value)
339 {
340 setPropertyImpl(name, value, PropertyDef::getTypeForValue(value));
341 }
344 void setValue(const String& name, const char* pChar)
345 {
346 String v(pChar);
347 setPropertyImpl(name, v, PROP_STRING);
348 }
349
350
351 protected:
353
355 template <typename T>
356 void setPropertyImpl(const String& name, const T& val, PropertyType typeCheck)
357 {
358 PropertyBase* baseProp = getProperty(name);
359 if (baseProp->getType() != typeCheck)
360 {
362 msg << "Property error: type passed in: '" << PropertyDef::getTypeName(typeCheck)
363 << "', type of property: '" << PropertyDef::getTypeName(baseProp->getType()) << "'";
364 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, msg.str(), "PropertySet::setPropertyImpl");
365 }
366 static_cast<Property<T>*>(baseProp)->set(val);
367 }
368
370 template <typename T>
371 void getPropertyImpl(const String& name, T& refVal, PropertyType typeCheck) const
372 {
373 PropertyBase* baseProp = getProperty(name);
374 if (baseProp->getType() != typeCheck)
375 {
377 msg << "Property error: type requested: '" << PropertyDef::getTypeName(typeCheck)
378 << "', type of property: '" << PropertyDef::getTypeName(baseProp->getType()) << "'";
379 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, msg.str(), "PropertySet::getPropertyImpl");
380 }
381 refVal = static_cast<Property<T>*>(baseProp)->get();
382 }
383
384 };
385
389}
390
391#endif
#define _OgrePropertyExport
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Variant type that can hold Any other type.
Definition OgreAny.h:57
Class representing colour.
Concrete IteratorWrapper for nonconst access to the underlying key-value container.
A 3x3 matrix which can represent rotations around axes.
Definition OgreMatrix3.h:69
Class encapsulating a standard 4x4 homogeneous matrix.
Definition OgreMatrix4.h:79
Base interface for an instance of a property.
const String & getName() const
Get the name of the property.
virtual Ogre::Any getValue() const =0
Return the current value as an Any.
PropertyDef * mDef
PropertyType getType() const
Get the type of the property.
virtual ~PropertyBase()
PropertyBase(PropertyDef *def)
Constructor.
const String & getDescription() const
Get the description of the property.
Definition of a property of an object.
static PropertyType getTypeForValue(const Vector4 &val)
static PropertyType getTypeForValue(const unsigned short &val)
static PropertyType getTypeForValue(const Quaternion &val)
static PropertyType getTypeForValue(const ColourValue &val)
static const String & getTypeName(PropertyType theType)
Get a string name of a property type.
static PropertyType getTypeForValue(const unsigned int &val)
static PropertyType getTypeForValue(const bool &val)
static PropertyType getTypeForValue(const String &val)
PropertyDef(const String &name, const String &desc, PropertyType pType)
Construct a property.
PropertyType getType() const
Get the type of the property.
static PropertyType getTypeForValue(const Matrix3 &val)
PropertyType mType
static PropertyType getTypeForValue(const Vector3 &val)
static PropertyType getTypeForValue(const Vector2 &val)
static PropertyType getTypeForValue(const Matrix4 &val)
static PropertyType getTypeForValue(const Real &val)
static PropertyType getTypeForValue(const int &val)
static PropertyType getTypeForValue(const unsigned long &val)
const String & getDescription() const
Get the description of the property.
static PropertyType getTypeForValue(const long &val)
static PropertyType getTypeForValue(const short &val)
const String & getName() const
Get the name of the property.
Defines a complete set of properties for a single object instance.
void setPropertyImpl(const String &name, const T &val, PropertyType typeCheck)
Set a named property value, internal implementation (type match required)
void setValueMap(const PropertyValueMap &values)
Sets the current state from a given value map.
void getValue(const String &name, T &value) const
Get a named property value.
PropertyIterator getPropertyIterator()
Get an iterator over the available properties.
void removeProperty(const String &name)
Removes the named property from the property set.
void addProperty(PropertyBase *prop)
Adds a property to this set.
void setValue(const String &name, T value)
Set a named property value.
Ogre::MapIterator< PropertyMap > PropertyIterator
void getPropertyImpl(const String &name, T &refVal, PropertyType typeCheck) const
Get a named property value, internal implementation (type match required)
map< String, PropertyBase * >::type PropertyMap
PropertyMap mPropertyMap
PropertyBase * getProperty(const String &name) const
Gets the property object for a given property name.
void setValue(const String &name, const T *value)
Set a named property value (via pointer to avoid copy).
PropertyValueMap getValueMap() const
Gets an independently usable collection of property values from the current state.
void setValue(const String &name, const char *pChar)
Special-case char*, convert to String automatically.
bool hasProperty(const String &name) const
Reports whether this property set contains a named property.
Property instance with passthrough calls to a given object.
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...
boost::function< void(T) > setter_func
setter_func mSetter
virtual T get() const
Ogre::Any getValue() const
Return the current value as an Any.
boost::function< T(void) > getter_func
virtual void set(T val)
Set the property value.
getter_func mGetter
Implementation of a Quaternion, i.e.
StringStream StrStreamType
Definition OgreString.h:78
Standard 2-dimensional vector.
Definition OgreVector2.h:52
Standard 3-dimensional vector.
Definition OgreVector3.h:52
4-dimensional homogeneous vector.
Definition OgreVector4.h:46
#define OGRE_EXCEPT(num, desc, src)
PropertyType
The type of a property.
map< String, PropertyDef >::type PropertyDefMap
Map from property name to shared definition.
map< String, PropertyValue >::type PropertyValueMap
Defines a transferable map of properties using wrapped value types (Ogre::Any)
@ PROP_QUATERNION
@ PROP_SHORT
@ PROP_MATRIX3
@ PROP_REAL
@ PROP_LONG
@ PROP_BOOL
@ PROP_COLOUR
@ PROP_UNKNOWN
@ PROP_VECTOR2
@ PROP_STRING
@ PROP_UNSIGNED_SHORT
@ PROP_UNSIGNED_INT
@ PROP_VECTOR4
@ PROP_UNSIGNED_LONG
@ PROP_VECTOR3
@ PROP_MATRIX4
float Real
Software floating point type.
_StringBase String
A simple structure designed just as a holder of property values between the instances of objects they...
PropertyType propType
std::map< K, V, P, A > type

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.