Crazy Eddie's GUI System  0.8.4
TypedProperty.h
00001 /***********************************************************************
00002     created:    24/11/2010
00003     author:     Martin Preisler
00004     
00005     purpose:    Defines the TypedProperty base class which allows native
00006                 property setting / getting whilst still falling back
00007                 to Strings gracefully
00008 *************************************************************************/
00009 /***************************************************************************
00010  *   Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
00011  *
00012  *   Permission is hereby granted, free of charge, to any person obtaining
00013  *   a copy of this software and associated documentation files (the
00014  *   "Software"), to deal in the Software without restriction, including
00015  *   without limitation the rights to use, copy, modify, merge, publish,
00016  *   distribute, sublicense, and/or sell copies of the Software, and to
00017  *   permit persons to whom the Software is furnished to do so, subject to
00018  *   the following conditions:
00019  *
00020  *   The above copyright notice and this permission notice shall be
00021  *   included in all copies or substantial portions of the Software.
00022  *
00023  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00024  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00025  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00026  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00027  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00028  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00029  *   OTHER DEALINGS IN THE SOFTWARE.
00030  ***************************************************************************/
00031 #ifndef _CEGUITypedProperty_h_
00032 #define _CEGUITypedProperty_h_
00033 
00034 #include "CEGUI/Property.h"
00035 #include "CEGUI/PropertyHelper.h"
00036 #include "CEGUI/Exceptions.h"
00037 // Start of CEGUI namespace section
00038 namespace CEGUI
00039 {
00040 
00048 template<typename T>
00049 class TypedProperty : public Property
00050 {
00051 public:
00052     typedef PropertyHelper<T> Helper;
00053     
00054     // TODO: do we want less bug prone code but a bit slower (string conversion for default values at construction) or faster
00055     //       but more typo prone (passing string default value)?
00056     TypedProperty(const String& name, const String& help, const String& origin = "Unknown", typename Helper::pass_type defaultValue = T(), bool writesXML = true):
00057         Property(name, help, Helper::toString(defaultValue), writesXML, Helper::getDataTypeName(), origin)
00058     {}
00059     
00060     virtual ~TypedProperty()
00061     {}
00062     
00064     virtual String get(const PropertyReceiver* receiver) const
00065     {
00066         return Helper::toString(getNative(receiver));
00067     }
00068 
00070     virtual void set(PropertyReceiver* receiver, const String& value)
00071     {
00072         setNative(receiver, Helper::fromString(value));
00073     }
00074 
00080     virtual void setNative(PropertyReceiver* receiver, typename Helper::pass_type value)
00081     {
00082         if (isWritable())
00083             setNative_impl(receiver,value);
00084         else
00085             CEGUI_THROW(InvalidRequestException(String("Property ") + d_origin + ":" + d_name + " is not writable!"));
00086     }
00092     virtual typename Helper::safe_method_return_type getNative(const PropertyReceiver* receiver) const{
00093         if (isReadable())
00094             return getNative_impl(receiver);
00095         else
00096             CEGUI_THROW(InvalidRequestException(String("Property ") + d_origin + ":" + d_name+" is not readable!"));
00097     }
00098 protected:
00099     virtual void setNative_impl(PropertyReceiver* receiver, typename Helper::pass_type value) = 0;
00100     virtual typename Helper::safe_method_return_type getNative_impl(const PropertyReceiver* receiver) const = 0;
00101 };
00102 
00103 } // End of  CEGUI namespace section
00104 
00105 #endif  // end of guard _CEGUITypedProperty_h_
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends