1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
template <class Object>
class CMetaObject
{
public:
virtual ~CMetaObject()
{
}
class IMetaMethod
{
public:
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)=0;
};
template <class Return>
class CMetaMethod0 : public IMetaMethod
{
public:
typedef Return (Object::*tMeth)();
CMetaMethod0(const tMeth &_fMethod):
m_fMethod(_fMethod)
{
}
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)
{
return((_pkObject->*m_fMethod)());
}
tMeth m_fMethod;
};
template <>
class CMetaMethod0<void> : public IMetaMethod
{
public:
typedef void (Object::*tMeth)();
CMetaMethod0(const tMeth &_fMethod):
m_fMethod(_fMethod)
{
}
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)
{
(_pkObject->*m_fMethod)();
return(0);
}
tMeth m_fMethod;
};
template <class Return,class P1>
class CMetaMethod1 : public IMetaMethod
{
public:
typedef Return (Object::*tMeth)(P1);
CMetaMethod1(const tMeth &_fMethod):
m_fMethod(_fMethod)
{
}
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)
{
return((_pkObject->*m_fMethod)(boost::any_cast<P1>(_apsArgs[0])));
}
tMeth m_fMethod;
};
template <class P1>
class CMetaMethod1<void,P1> : public IMetaMethod
{
public:
typedef void (Object::*tMeth)(P1);
CMetaMethod1(const tMeth &_fMethod):
m_fMethod(_fMethod)
{
}
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)
{
(_pkObject->*m_fMethod)(boost::any_cast<P1>(_apsArgs[0]));
return(0);
}
tMeth m_fMethod;
};
template <class Return,class P1,class P2>
class CMetaMethod2 : public IMetaMethod
{
public:
typedef Return (Object::*tMeth)(P1,P2);
CMetaMethod2(const tMeth &_fMethod):
m_fMethod(_fMethod)
{
}
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)
{
return((_pkObject->*m_fMethod)(boost::any_cast<P1>(_apsArgs[0]),boost::any_cast<P2>(_apsArgs[1])));
}
tMeth m_fMethod;
};
template <class P1,class P2>
class CMetaMethod2<void,P1,P2> : public IMetaMethod
{
public:
typedef void (Object::*tMeth)(P1,P2);
CMetaMethod2(const tMeth &_fMethod):
m_fMethod(_fMethod)
{
}
virtual boost::any Call(Object *_pkObject,boost::any *_apsArgs)
{
(_pkObject->*m_fMethod)(boost::any_cast<P1>(_apsArgs[0]),boost::any_cast<P2>(_apsArgs[1]));
return(0);
}
tMeth m_fMethod;
};
class Desc
{
public:
void Register(const string &_sName,IMetaMethod *_pkMethod)
{
m_kMethods[_sName]=_pkMethod;
}
map<string,IMetaMethod*> m_kMethods;
};
static Desc &GetDesc()
{
static Desc kDesc;
return(kDesc);
}
template <class TypeResult>
void AssignNonVoid(TypeResult *_pkResult,const boost::any &_kValue)
{
*_pkResult=boost::any_cast<TypeResult>(_kValue);
}
template <>
void AssignNonVoid<void>(void *_pkResult,const boost::any &_kValue)
{
}
template <class TypeResult>
void MetaCall(const string &_sMethod,TypeResult *_pkResult=NULL)
{
boost::any kResult=GetDesc().m_kMethods[_sMethod]->Call(dynamic_cast<Object*>(this),NULL);
if (_pkResult)
AssignNonVoid<TypeResult>(_pkResult,kResult);
}
template <class TypeResult>
void MetaCall(const string &_sMethod,const boost::any &_kArg1,TypeResult *_pkResult=NULL)
{
boost::any asArgs[]={_kArg1};
boost::any kResult=GetDesc().m_kMethods[_sMethod]->Call(dynamic_cast<Object*>(this),asArgs);
if (_pkResult)
AssignNonVoid<TypeResult>(_pkResult,kResult);
}
template <class TypeResult>
void MetaCall(const string &_sMethod,const boost::any &_kArg1,const boost::any &_kArg2,TypeResult *_pkResult=NULL)
{
boost::any asArgs[]={_kArg1,_kArg2};
boost::any kResult=GetDesc().m_kMethods[_sMethod]->Call(dynamic_cast<Object*>(this),asArgs);
if (_pkResult)
AssignNonVoid<TypeResult>(_pkResult,kResult);
}
};
class CTest : public CMetaObject<CTest>
{
public:
void Pipo(int _iValue)
{
}
float Mito(const string &_s1,const string &_s2)
{
return(1.23456f);
}
void TestCharPtr(const char *_sz)
{
}
static void InitMetaClass()
{
GetDesc().Register("pipo",new CMetaMethod1<void,int>(&CTest::Pipo));
GetDesc().Register("mito",new CMetaMethod2<float,const string &,const string &>(&CTest::Mito));
//GetDesc().Register("testcharptr",new CMetaMethod1<void,const char *>(&CTest::TestCharPtr));
}
};
int Main()
{
CTest::InitMetaClass();
CTest kTest;
kTest.MetaCall<void>("pipo",1);
float fR;
kTest.MetaCall<float>("mito",string("hello"),string("world"),&fR);
//kTest.MetaCall<void>("testcharptr","yep!");
} |
Partager