Crazy Eddie's GUI System  0.8.4
PropertySet.h
00001 /***********************************************************************
00002         created:        21/2/2004
00003         author:         Paul D Turner
00004         
00005         purpose:        Defines interface for the PropertySet class
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUIPropertySet_h_
00030 #define _CEGUIPropertySet_h_
00031 
00032 #include "CEGUI/Base.h"
00033 #include "CEGUI/String.h"
00034 #include "CEGUI/IteratorBase.h"
00035 #include "CEGUI/Property.h"
00036 #include "CEGUI/PropertyHelper.h"
00037 #include "CEGUI/TypedProperty.h"
00038 // not needed in this header but you are likely to use it if you include this,
00039 // we also define the CEGUI_DEFINE_PROPERTY macro that relies on this here
00040 #include "CEGUI/TplWindowProperty.h"
00041 #include "CEGUI/Exceptions.h"
00042 #include <map>
00043 
00044 #if defined(_MSC_VER)
00045 #       pragma warning(push)
00046 #       pragma warning(disable : 4251)
00047 #endif
00048 
00049 // Start of CEGUI namespace section
00050 namespace CEGUI
00051 {
00056 class CEGUIEXPORT PropertySet : public PropertyReceiver
00057 {
00058 public:
00063         PropertySet(void) {}
00064 
00065 
00070         virtual ~PropertySet(void) {}
00071 
00072 
00086         void    addProperty(Property* property);
00087 
00088 
00099         void    removeProperty(const String& name);
00100 
00101 
00112         Property* getPropertyInstance(const String& name) const;
00113 
00114 
00122         void    clearProperties(void);
00123 
00124 
00135         bool    isPropertyPresent(const String& name) const;
00136 
00137 
00150         const String&   getPropertyHelp(const String& name) const;
00151 
00152 
00165         String  getProperty(const String& name) const;
00166 
00173     template<typename T>
00174     typename PropertyHelper<T>::return_type getProperty(const String& name) const
00175     {
00176         PropertyRegistry::const_iterator pos = d_properties.find(name);
00177 
00178         if (pos == d_properties.end())
00179         {
00180             CEGUI_THROW(UnknownObjectException("There is no Property named '" + name + "' available in the set."));
00181         }
00182 
00183         Property* baseProperty = pos->second;
00184         TypedProperty<T>* typedProperty = dynamic_cast<TypedProperty<T>* >(baseProperty);
00185         
00186         if (typedProperty)
00187         {
00188             // yay, we can get native!
00189             return typedProperty->getNative(this);
00190         }
00191         else
00192         {   
00193             // fall back to string get
00194             return PropertyHelper<T>::fromString(baseProperty->get(this));
00195         }
00196     }
00197 
00214         void    setProperty(const String& name, const String& value);
00215 
00222     template<typename T>
00223     void    setProperty(const String& name, typename PropertyHelper<T>::pass_type value)
00224     {
00225         PropertyRegistry::iterator pos = d_properties.find(name);
00226 
00227         if (pos == d_properties.end())
00228         {
00229             CEGUI_THROW(UnknownObjectException("There is no Property named '" + name + "' available in the set."));
00230         }
00231 
00232         Property* baseProperty = pos->second;
00233         TypedProperty<T>* typedProperty = dynamic_cast<TypedProperty<T>* >(baseProperty);
00234         
00235         if (typedProperty)
00236         {
00237             // yay, we can set native!
00238             typedProperty->setNative(this, value);
00239         }
00240         else
00241         {
00242             // fall back to string set
00243             baseProperty->set(this, PropertyHelper<T>::toString(value));
00244         }
00245     }
00246 
00258         bool    isPropertyDefault(const String& name) const;
00259 
00260 
00271         String  getPropertyDefault(const String& name) const;
00272 
00273 private:
00274         typedef std::map<String, Property*, StringFastLessCompare
00275         CEGUI_MAP_ALLOC(String, Property*)> PropertyRegistry;
00276         PropertyRegistry        d_properties;
00277 
00278 
00279 public:
00280         /*************************************************************************
00281                 Iterator stuff
00282         *************************************************************************/
00283     typedef     ConstMapIterator<PropertyRegistry> PropertyIterator;
00284 
00290     PropertyIterator getPropertyIterator(void) const;
00291 };
00292 
00305 #define CEGUI_DEFINE_PROPERTY(class_type, property_native_type, name, help, setter, getter, default_value)\
00306 {\
00307     static ::CEGUI::TplWindowProperty<class_type, property_native_type> sProperty(\
00308             name, help, propertyOrigin, setter, getter, default_value);\
00309     \
00310     this->addProperty(&sProperty);\
00311 }
00312 
00327 #define CEGUI_DEFINE_PROPERTY_NO_XML(class_type, property_native_type, name, help, setter, getter, default_value)\
00328 {\
00329     static ::CEGUI::TplWindowProperty<class_type, property_native_type> sProperty(\
00330             name, help, propertyOrigin, setter, getter, default_value, false);\
00331     \
00332     this->addProperty(&sProperty);\
00333 }
00334 
00335 } // End of  CEGUI namespace section
00336 
00337 #if defined(_MSC_VER)
00338 #       pragma warning(pop)
00339 #endif
00340 
00341 #endif  // end of guard _CEGUIPropertySet_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends