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