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