00001 /* 00002 ----------------------------------------------------------------------------- 00003 This source file is part of OGRE 00004 (Object-oriented Graphics Rendering Engine) 00005 For the latest info, see http://www.ogre3d.org/ 00006 00007 Copyright (c) 2000-2009 Torus Knot Software Ltd 00008 00009 Permission is hereby granted, free of charge, to any person obtaining a copy 00010 of this software and associated documentation files (the "Software"), to deal 00011 in the Software without restriction, including without limitation the rights 00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 copies of the Software, and to permit persons to whom the Software is 00014 furnished to do so, subject to the following conditions: 00015 00016 The above copyright notice and this permission notice shall be included in 00017 all copies or substantial portions of the Software. 00018 00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 THE SOFTWARE. 00026 ----------------------------------------------------------------------------- 00027 */ 00028 #ifndef __OGRE_PROPERTY_H__ 00029 #define __OGRE_PROPERTY_H__ 00030 00031 #include "OgrePropertyPrerequisites.h" 00032 #include "OgreAny.h" 00033 #include "OgreIteratorWrappers.h" 00034 #include "OgreString.h" 00035 #include "OgreException.h" 00036 #include "OgreVector2.h" 00037 #include "OgreVector3.h" 00038 #include "OgreVector4.h" 00039 #include "OgreColourValue.h" 00040 #include "OgreQuaternion.h" 00041 #include "OgreMatrix3.h" 00042 #include "OgreMatrix4.h" 00043 00044 #include <boost/bind.hpp> 00045 #include <boost/function.hpp> 00100 namespace Ogre 00101 { 00109 00110 enum PropertyType 00111 { 00112 PROP_SHORT = 0, 00113 PROP_UNSIGNED_SHORT = 1, 00114 PROP_INT = 2, 00115 PROP_UNSIGNED_INT = 3, 00116 PROP_LONG = 4, 00117 PROP_UNSIGNED_LONG = 5, 00118 PROP_REAL = 6, 00119 PROP_STRING = 7, 00120 PROP_VECTOR2 = 8, 00121 PROP_VECTOR3 = 9, 00122 PROP_VECTOR4 = 10, 00123 PROP_COLOUR = 11, 00124 PROP_BOOL = 12, 00125 PROP_QUATERNION = 13, 00126 PROP_MATRIX3 = 14, 00127 PROP_MATRIX4 = 15, 00128 00129 PROP_UNKNOWN = 999 00130 }; 00131 00137 class _OgrePropertyExport PropertyDef : public PropertyAlloc 00138 { 00139 public: 00140 00141 /* Construct a property. 00142 @param name The name of the property 00143 @param desc A (potentially) long description of the property 00144 @param pType The type of the property 00145 */ 00146 PropertyDef(const String& name, const String& desc, PropertyType pType) 00147 : mName(name), mDesc(desc), mType(pType) {} 00148 00150 const String& getName() const { return mName; } 00151 00153 const String& getDescription() const { return mDesc; } 00154 00156 PropertyType getType() const { return mType; } 00157 00159 static const String& getTypeName(PropertyType theType); 00160 00161 static PropertyType getTypeForValue(const short& val) { return PROP_SHORT; } 00162 static PropertyType getTypeForValue(const unsigned short& val) { return PROP_UNSIGNED_SHORT; } 00163 static PropertyType getTypeForValue(const int& val) { return PROP_INT; } 00164 static PropertyType getTypeForValue(const unsigned int& val) { return PROP_UNSIGNED_INT; } 00165 static PropertyType getTypeForValue(const long& val) { return PROP_LONG; } 00166 static PropertyType getTypeForValue(const unsigned long& val) { return PROP_UNSIGNED_LONG; } 00167 static PropertyType getTypeForValue(const Real& val) { return PROP_REAL; } 00168 static PropertyType getTypeForValue(const String& val) { return PROP_STRING; } 00169 static PropertyType getTypeForValue(const Vector2& val) { return PROP_VECTOR2; } 00170 static PropertyType getTypeForValue(const Vector3& val) { return PROP_VECTOR3; } 00171 static PropertyType getTypeForValue(const Vector4& val) { return PROP_VECTOR4; } 00172 static PropertyType getTypeForValue(const ColourValue& val) { return PROP_COLOUR; } 00173 static PropertyType getTypeForValue(const bool& val) { return PROP_BOOL; } 00174 static PropertyType getTypeForValue(const Quaternion& val) { return PROP_QUATERNION; } 00175 static PropertyType getTypeForValue(const Matrix3& val) { return PROP_MATRIX3; } 00176 static PropertyType getTypeForValue(const Matrix4& val) { return PROP_MATRIX4; } 00177 00178 protected: 00179 // no default construction 00180 PropertyDef() {} 00181 00182 String mName; 00183 String mDesc; 00184 PropertyType mType; 00185 00186 }; 00187 00189 typedef map<String, PropertyDef>::type PropertyDefMap; 00190 00193 class _OgrePropertyExport PropertyBase : public PropertyAlloc 00194 { 00195 public: 00197 PropertyBase(PropertyDef* def) : mDef(def) {} 00198 virtual ~PropertyBase() {} 00199 00201 const String& getName() const { return mDef->getName(); } 00202 00204 const String& getDescription() const { return mDef->getDescription(); } 00205 00207 PropertyType getType() const { return mDef->getType(); } 00208 00210 virtual Ogre::Any getValue() const = 0; 00211 00212 protected: 00213 // disallow default construction 00214 PropertyBase() {} 00215 PropertyDef* mDef; 00216 00217 }; 00218 00220 template <typename T> 00221 class Property : public PropertyBase 00222 { 00223 public: 00224 typedef T value_type; 00225 typedef boost::function< T (void) > getter_func; 00226 typedef boost::function< void (T) > setter_func; 00227 00231 Property(PropertyDef* def, getter_func getter, setter_func setter) 00232 : PropertyBase(def) 00233 , mGetter(getter) 00234 , mSetter(setter) 00235 { 00236 } 00237 00240 virtual void set(T val) 00241 { 00242 mSetter(val); 00243 } 00244 00245 virtual T get() const 00246 { 00247 return mGetter(); 00248 } 00249 00250 Ogre::Any getValue() const 00251 { 00252 return Ogre::Any(get()); 00253 } 00254 00255 protected: 00256 // disallow default construction 00257 Property() {} 00258 ~Property() {} 00259 00260 getter_func mGetter; 00261 setter_func mSetter; 00262 }; 00263 00268 struct PropertyValue 00269 { 00270 PropertyType propType; 00271 Ogre::Any val; 00272 }; 00274 typedef map<String, PropertyValue>::type PropertyValueMap; 00275 00276 00279 class _OgrePropertyExport PropertySet : public PropertyAlloc 00280 { 00281 public: 00282 PropertySet(); 00283 ~PropertySet(); 00284 00289 void addProperty(PropertyBase* prop); 00290 00298 PropertyBase* getProperty(const String& name) const; 00299 00301 bool hasProperty(const String& name) const; 00302 00303 typedef map<String, PropertyBase*>::type PropertyMap; 00304 typedef Ogre::MapIterator<PropertyMap> PropertyIterator; 00306 PropertyIterator getPropertyIterator(); 00307 00311 PropertyValueMap getValueMap() const; 00312 00315 void setValueMap(const PropertyValueMap& values); 00316 00319 template<typename T> 00320 void getValue(const String& name, T& value) const 00321 { 00322 getPropertyImpl(name, value, PropertyDef::getTypeForValue(value)); 00323 } 00324 00327 template<typename T> 00328 void setValue(const String& name, const T* value) 00329 { 00330 setPropertyImpl(name, *value, PropertyDef::getTypeForValue(*value)); 00331 } 00334 template<typename T> 00335 void setValue(const String& name, T value) 00336 { 00337 setPropertyImpl(name, value, PropertyDef::getTypeForValue(value)); 00338 } 00341 void setValue(const String& name, const char* pChar) 00342 { 00343 String v(pChar); 00344 setPropertyImpl(name, v, PROP_STRING); 00345 } 00346 00347 00348 protected: 00349 PropertyMap mPropertyMap; 00350 00352 template <typename T> 00353 void setPropertyImpl(const String& name, const T& val, PropertyType typeCheck) 00354 { 00355 PropertyBase* baseProp = getProperty(name); 00356 if (baseProp->getType() != typeCheck) 00357 { 00358 StringUtil::StrStreamType msg; 00359 msg << "Property error: type passed in: '" << PropertyDef::getTypeName(typeCheck) 00360 << "', type of property: '" << PropertyDef::getTypeName(baseProp->getType()) << "'"; 00361 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, msg.str(), "PropertySet::setPropertyImpl"); 00362 } 00363 static_cast<Property<T>*>(baseProp)->set(val); 00364 } 00365 00367 template <typename T> 00368 void getPropertyImpl(const String& name, T& refVal, PropertyType typeCheck) const 00369 { 00370 PropertyBase* baseProp = getProperty(name); 00371 if (baseProp->getType() != typeCheck) 00372 { 00373 StringUtil::StrStreamType msg; 00374 msg << "Property error: type requested: '" << PropertyDef::getTypeName(typeCheck) 00375 << "', type of property: '" << PropertyDef::getTypeName(baseProp->getType()) << "'"; 00376 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, msg.str(), "PropertySet::getPropertyImpl"); 00377 } 00378 refVal = static_cast<Property<T>*>(baseProp)->get(); 00379 } 00380 00381 }; 00382 00386 } 00387 00388 #endif
Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52