Crazy Eddie's GUI System  0.8.7
TypedProperty.h
1 /***********************************************************************
2  created: 24/11/2010
3  author: Martin Preisler
4 
5  purpose: Defines the TypedProperty base class which allows native
6  property setting / getting whilst still falling back
7  to Strings gracefully
8 *************************************************************************/
9 /***************************************************************************
10  * Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining
13  * a copy of this software and associated documentation files (the
14  * "Software"), to deal in the Software without restriction, including
15  * without limitation the rights to use, copy, modify, merge, publish,
16  * distribute, sublicense, and/or sell copies of the Software, and to
17  * permit persons to whom the Software is furnished to do so, subject to
18  * the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be
21  * included in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  ***************************************************************************/
31 #ifndef _CEGUITypedProperty_h_
32 #define _CEGUITypedProperty_h_
33 
34 #include "CEGUI/Property.h"
35 #include "CEGUI/PropertyHelper.h"
36 #include "CEGUI/Exceptions.h"
37 // Start of CEGUI namespace section
38 namespace CEGUI
39 {
40 
48 template<typename T>
49 class TypedProperty : public Property
50 {
51 public:
52  typedef PropertyHelper<T> Helper;
53 
54  // TODO: do we want less bug prone code but a bit slower (string conversion for default values at construction) or faster
55  // but more typo prone (passing string default value)?
56  TypedProperty(const String& name, const String& help, const String& origin = "Unknown", typename Helper::pass_type defaultValue = T(), bool writesXML = true):
57  Property(name, help, Helper::toString(defaultValue), writesXML, Helper::getDataTypeName(), origin)
58  {}
59 
60  virtual ~TypedProperty()
61  {}
62 
64  virtual String get(const PropertyReceiver* receiver) const
65  {
66  return Helper::toString(getNative(receiver));
67  }
68 
70  virtual void set(PropertyReceiver* receiver, const String& value)
71  {
72  setNative(receiver, Helper::fromString(value));
73  }
74 
80  virtual void setNative(PropertyReceiver* receiver, typename Helper::pass_type value)
81  {
82  if (isWritable())
83  setNative_impl(receiver,value);
84  else
85  CEGUI_THROW(InvalidRequestException(String("Property ") + d_origin + ":" + d_name + " is not writable!"));
86  }
92  virtual typename Helper::safe_method_return_type getNative(const PropertyReceiver* receiver) const{
93  if (isReadable())
94  return getNative_impl(receiver);
95  else
96  CEGUI_THROW(InvalidRequestException(String("Property ") + d_origin + ":" + d_name+" is not readable!"));
97  }
98 protected:
99  virtual void setNative_impl(PropertyReceiver* receiver, typename Helper::pass_type value) = 0;
100  virtual typename Helper::safe_method_return_type getNative_impl(const PropertyReceiver* receiver) const = 0;
101 };
102 
103 } // End of CEGUI namespace section
104 
105 #endif // end of guard _CEGUITypedProperty_h_
String d_origin
Holds origin of this property.
Definition: Property.h:251
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
String d_name
String that stores the Property name.
Definition: Property.h:244
base class for properties able to do native set/get
Definition: TypedProperty.h:49
virtual void set(PropertyReceiver *receiver, const String &value)
Sets the value of the property.
Definition: TypedProperty.h:70
virtual bool isWritable() const
Returns whether the property is writable.
virtual void setNative(PropertyReceiver *receiver, typename Helper::pass_type value)
native set method, sets the property given a native type
Definition: TypedProperty.h:80
Dummy base class to ensure correct casting of receivers.
Definition: Property.h:45
virtual Helper::safe_method_return_type getNative(const PropertyReceiver *receiver) const
native get method, returns the native type by copy
Definition: TypedProperty.h:92
Helper class used to convert various data types to and from the format expected in Property strings...
Definition: ForwardRefs.h:84
virtual bool isReadable() const
Returns whether the property is readable.
Exception class used when some impossible request was made of the system.
Definition: Exceptions.h:304
An abstract class that defines the interface to access object properties by name. ...
Definition: Property.h:60
Property(const String &name, const String &help, const String &defaultValue="", bool writesXML=true, const String &dataType="Unknown", const String &origin="Unknown")
Creates a new Property object.
Definition: Property.h:91
String class used within the GUI system.
Definition: String.h:62