Crazy Eddie's GUI System  0.8.6
TplProperty.h
1 /***********************************************************************
2  created: 23/11/2010
3  author: Martin Preisler
4 
5  purpose: Finger saving template property
6 *************************************************************************/
7 /***************************************************************************
8  * Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining
11  * a copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sublicense, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be
19  * included in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
24  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  ***************************************************************************/
29 #ifndef _CEGUITplProperty_h_
30 #define _CEGUITplProperty_h_
31 
32 #include "CEGUI/TypedProperty.h"
33 
34 // Start of CEGUI namespace section
35 namespace CEGUI
36 {
37 
38 template<class C, typename T>
39 class TplProperty : public TypedProperty<T>
40 {
41 public:
42  typedef PropertyHelper<T> Helper;
43 
44  typedef void (C::*Setter)(typename Helper::pass_type);
45 
50  {
51  template<typename DT> struct EnsurePlain { typedef DT result; };
52  template<typename DT> struct EnsurePlain<DT&> { typedef DT result; };
53  template<typename DT> struct EnsurePlain<const DT&> { typedef DT result; };
54 
55  template<typename DT> struct EnsureConstRef { typedef const DT& result; };
56  template<typename DT> struct EnsureConstRef<DT&> { typedef const DT& result; };
57  template<typename DT> struct EnsureConstRef<const DT&> { typedef const DT& result; };
58 
59  template<typename DT> struct EnsureRef { typedef DT& result; };
60  template<typename DT> struct EnsureRef<DT&> { typedef DT& result; };
61  template<typename DT> struct EnsureRef<const DT&> { typedef DT& result; };
62 
63  typedef typename EnsurePlain<typename Helper::safe_method_return_type>::result (C::*PlainGetter)() const;
64  typedef typename EnsureConstRef<typename Helper::safe_method_return_type>::result (C::*ConstRefGetter)() const;
65  typedef typename EnsureRef<typename Helper::safe_method_return_type>::result (C::*RefGetter)() const;
66 
67  GetterFunctor(PlainGetter getter):
68  d_plainGetter(getter)
69  //d_constRefGetter(0), no need to initialise these, we will never use them
70  //d_refGetter(0)
71  {}
72 
73  GetterFunctor(ConstRefGetter getter):
74  d_plainGetter(0),
75  d_constRefGetter(getter)
76  //d_refGetter(0) // no need to initialise this, we will never use it
77  {}
78 
79  GetterFunctor(RefGetter getter):
80  d_plainGetter(0),
81  d_constRefGetter(0),
82  d_refGetter(getter)
83  {}
84  // to set 0 as func
85  GetterFunctor(int /*val*/):
86  d_plainGetter(0),
87  d_constRefGetter(0),
88  d_refGetter(0)
89  {}
90  operator bool(void) const
91  {
92  return d_plainGetter || d_constRefGetter || d_refGetter;
93  }
94  typename Helper::safe_method_return_type operator()(const C* instance) const
95  {
96  // FIXME: Ideally we want this to be done during compilation, not runtime
97 
98  if (d_plainGetter)
99  return CEGUI_CALL_MEMBER_FN(*instance, d_plainGetter)();
100  if (d_constRefGetter)
101  return CEGUI_CALL_MEMBER_FN(*instance, d_constRefGetter)();
102  if (d_refGetter)
103  return CEGUI_CALL_MEMBER_FN(*instance, d_refGetter)();
104 
105  assert(false);
106  // just to get rid of the warning
107  return CEGUI_CALL_MEMBER_FN(*instance, d_plainGetter)();
108  }
109 
110  PlainGetter d_plainGetter;
111  ConstRefGetter d_constRefGetter;
112  RefGetter d_refGetter;
113  };
114 
115  TplProperty(const String& name, const String& help, const String& origin, Setter setter, GetterFunctor getter, typename Helper::pass_type defaultValue = T(), bool writesXML = true):
116  TypedProperty<T>(name, help, origin, defaultValue, writesXML),
117 
118  d_setter(setter),
119  d_getter(getter)
120  {}
121 
122  virtual ~TplProperty()
123  {}
124 
126  virtual bool isReadable() const
127  {
128  return d_getter;
129  }
131  virtual bool isWritable() const
132  {
133  return d_setter;
134  }
135 
136 protected:
137  Setter d_setter;
138  GetterFunctor d_getter;
139 };
140 
141 } // End of CEGUI namespace section
142 
143 #endif // end of guard _CEGUITplProperty_h_
144 
Definition: TplProperty.h:49
Main namespace for Crazy Eddie's GUI Library.
Definition: arch_overview.dox:1
base class for properties able to do native set/get
Definition: TypedProperty.h:49
Definition: TplProperty.h:39
virtual bool isReadable() const
Returns whether the property is readable.
Definition: TplProperty.h:126
Helper class used to convert various data types to and from the format expected in Property strings...
Definition: ForwardRefs.h:84
virtual bool isWritable() const
Returns whether the property is writable.
Definition: TplProperty.h:131