Crazy Eddie's GUI System  0.8.4
TplProperty.h
00001 /***********************************************************************
00002     created:    23/11/2010
00003     author:     Martin Preisler
00004 
00005     purpose:    Finger saving template property
00006 *************************************************************************/
00007 /***************************************************************************
00008  *   Copyright (C) 2004 - 2010 Paul D Turner & The CEGUI Development Team
00009  *
00010  *   Permission is hereby granted, free of charge, to any person obtaining
00011  *   a copy of this software and associated documentation files (the
00012  *   "Software"), to deal in the Software without restriction, including
00013  *   without limitation the rights to use, copy, modify, merge, publish,
00014  *   distribute, sublicense, and/or sell copies of the Software, and to
00015  *   permit persons to whom the Software is furnished to do so, subject to
00016  *   the following conditions:
00017  *
00018  *   The above copyright notice and this permission notice shall be
00019  *   included in all copies or substantial portions of the Software.
00020  *
00021  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00022  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00023  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00024  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
00025  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00026  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00027  *   OTHER DEALINGS IN THE SOFTWARE.
00028  ***************************************************************************/
00029 #ifndef _CEGUITplProperty_h_
00030 #define _CEGUITplProperty_h_
00031 
00032 #include "CEGUI/TypedProperty.h"
00033 
00034 // Start of CEGUI namespace section
00035 namespace CEGUI
00036 {
00037 
00038 template<class C, typename T>
00039 class TplProperty : public TypedProperty<T>
00040 {
00041 public:
00042     typedef PropertyHelper<T> Helper;
00043     
00044     typedef void (C::*Setter)(typename Helper::pass_type);
00045 
00049     struct GetterFunctor
00050     {
00051         template<typename DT> struct EnsurePlain { typedef DT result; };
00052         template<typename DT> struct EnsurePlain<DT&> { typedef DT result; };
00053         template<typename DT> struct EnsurePlain<const DT&> { typedef DT result; };
00054 
00055         template<typename DT> struct EnsureConstRef { typedef const DT& result; };
00056         template<typename DT> struct EnsureConstRef<DT&> { typedef const DT& result; };
00057         template<typename DT> struct EnsureConstRef<const DT&> { typedef const DT& result; };
00058 
00059         template<typename DT> struct EnsureRef { typedef DT& result; };
00060         template<typename DT> struct EnsureRef<DT&> { typedef DT& result; };
00061         template<typename DT> struct EnsureRef<const DT&> { typedef DT& result; };
00062 
00063         typedef typename EnsurePlain<typename Helper::safe_method_return_type>::result (C::*PlainGetter)() const;
00064         typedef typename EnsureConstRef<typename Helper::safe_method_return_type>::result (C::*ConstRefGetter)() const;
00065         typedef typename EnsureRef<typename Helper::safe_method_return_type>::result (C::*RefGetter)() const;
00066 
00067         GetterFunctor(PlainGetter getter):
00068             d_plainGetter(getter)
00069             //d_constRefGetter(0), no need to initialise these, we will never use them
00070             //d_refGetter(0)
00071         {}
00072 
00073         GetterFunctor(ConstRefGetter getter):
00074             d_plainGetter(0),
00075             d_constRefGetter(getter)
00076             //d_refGetter(0) // no need to initialise this, we will never use it
00077         {}
00078 
00079         GetterFunctor(RefGetter getter):
00080             d_plainGetter(0),
00081             d_constRefGetter(0),
00082             d_refGetter(getter)
00083         {}
00084         // to set 0 as func
00085         GetterFunctor(int /*val*/):
00086             d_plainGetter(0),
00087             d_constRefGetter(0),
00088             d_refGetter(0)
00089         {}
00090         operator bool(void) const
00091         {
00092             return d_plainGetter || d_constRefGetter || d_refGetter;
00093         }
00094         typename Helper::safe_method_return_type operator()(const C* instance) const
00095         {
00096             // FIXME: Ideally we want this to be done during compilation, not runtime
00097 
00098             if (d_plainGetter)
00099                 return CEGUI_CALL_MEMBER_FN(*instance, d_plainGetter)();
00100             if (d_constRefGetter)
00101                 return CEGUI_CALL_MEMBER_FN(*instance, d_constRefGetter)();
00102             if (d_refGetter)
00103                 return CEGUI_CALL_MEMBER_FN(*instance, d_refGetter)();
00104 
00105             assert(false);
00106             // just to get rid of the warning
00107             return CEGUI_CALL_MEMBER_FN(*instance, d_plainGetter)();
00108         }
00109 
00110         PlainGetter d_plainGetter;
00111         ConstRefGetter d_constRefGetter;
00112         RefGetter d_refGetter;
00113     };
00114 
00115     TplProperty(const String& name, const String& help, const String& origin, Setter setter, GetterFunctor getter, typename Helper::pass_type defaultValue = T(), bool writesXML = true):
00116         TypedProperty<T>(name, help, origin, defaultValue, writesXML),
00117         
00118         d_setter(setter),
00119         d_getter(getter)
00120     {}
00121     
00122     virtual ~TplProperty()
00123     {}
00124 
00126     virtual bool isReadable() const
00127     {
00128         return d_getter;
00129     }
00131     virtual bool isWritable() const
00132     {
00133         return d_setter;
00134     }
00135 
00136 protected:
00137     Setter d_setter;
00138     GetterFunctor d_getter;
00139 };
00140 
00141 } // End of  CEGUI namespace section
00142 
00143 #endif  // end of guard _CEGUITplProperty_h_
00144 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends