Crazy Eddie's GUI System  0.8.4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
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_