Wexus2 0.20
wexus2.src/wexus/MemberFunction.h
00001 
00002 /*
00003  * Copyright (c) 2011 Aleksander B. Demko
00004  * This source code is distributed under the MIT license.
00005  * See the accompanying file LICENSE.MIT.txt for details.
00006  */
00007 
00008 #ifndef __WEXUS_MEMBERFUNCTION_H__
00009 #define __WEXUS_MEMBERFUNCTION_H__
00010 
00011 #include <assert.h>
00012 
00013 #include <wexus/TR1.h>
00014 
00015 #include <typeinfo>
00016 
00017 #include <QString>
00018 
00019 namespace wexus
00020 {
00021   /**
00022    * Given a class T, returns a unique string for
00023    * that class.
00024    *
00025    * @author Aleksander Demko
00026    */ 
00027   template <class T> QString typeToString(void);
00028 
00029   class MemberFunction;
00030   template <class T> class MemberFunctionType;
00031 }
00032 
00033 template <class T>
00034   QString wexus::typeToString(void)
00035 {
00036   return QString(typeid(T).name());
00037 }
00038 
00039 /**
00040  * wexus::MemberFunction represents a pointer-to-member
00041  * as a comprable (well, only =) object.
00042  * This is used for wexus::Registry work
00043  *
00044  * @author Aleksander Demko
00045  */ 
00046 class wexus::MemberFunction
00047 {
00048   private:
00049     class Imp {
00050       public:
00051         virtual ~Imp() { }
00052           
00053         virtual bool equals(const Imp &rhs) const = 0;
00054     };
00055     template <class T> class ImpType : public Imp
00056     {
00057       public:
00058         ImpType<T>(T _mfn)
00059           : dm_mfn(_mfn)
00060         {
00061         }
00062         virtual bool equals(const Imp &rhs) const;
00063 
00064       protected:
00065         T dm_mfn;
00066     };
00067 
00068   public:
00069     /// constructs a null one
00070     MemberFunction(void);
00071 
00072     /// normal ctor
00073     template <class MFN>
00074       MemberFunction(MFN _mfn)
00075         : dm_typestring(wexus::typeToString<MFN>()), dm_imp(new ImpType<MFN>(_mfn)) { }
00076 
00077     virtual ~MemberFunction();
00078 
00079     /// cant do < because the basic pointer-to-member type doesnt do <
00080     inline bool operator ==(const MemberFunction &rhs) const { return equals(rhs); }
00081 
00082   private:
00083     bool equals(const MemberFunction &rhs) const;
00084 
00085   private:
00086     QString dm_typestring;
00087     std::shared_ptr<Imp> dm_imp;
00088 };
00089 
00090 template <class T>
00091 bool wexus::MemberFunction::ImpType<T>::equals(const wexus::MemberFunction::Imp &rhs) const
00092 {
00093   const MemberFunction::ImpType<T> *there = dynamic_cast<const MemberFunction::ImpType<T> * >(&rhs);
00094 
00095   assert(there);
00096 
00097   return (dm_mfn == there->dm_mfn);
00098 }
00099 
00100 #endif
00101 
 All Classes Namespaces Functions Variables Enumerations Enumerator