#define DEFINE_BASECALSS class Call{ public: virtual RT call(TPARAM) = 0; virtual RT call(TPARAM) const = 0;};
#define DEFINE_FUNCALSS template<typename F> class FunCall : Call { public: FunCall(F f):m_func(f){} protected: F m_func;\
virtual RT call(TPARAM){ return m_func(PARAM);}\
virtual RT call(TPARAM) const { return m_func(PARAM);}};
#define DEFINE_MEMCALSS template<typename O,typename F> class MemCall : public Call{public:MemCall(O* obj,F f):m_obj(obj),m_func(f){} protected:O* m_obj;F m_func;\
virtual RT call(TPARAM){ return ((m_obj)->*m_func)(PARAM);}\
virtual RT call(TPARAM) const{ return ((m_obj)->*m_func)(PARAM);}};
#define DEFINE_CALL(CLASSNAME) template <typename RT,TEMPLATE size_t size = 4 * sizeof(void*)>\
class CLASSNAME\
{\
protected:\
DEFINE_BASECALSS\
DEFINE_MEMCALSS\
DEFINE_FUNCALSS\
public:\
template <typename O,typename F>\
CLASSNAME(O* obj,F f):m_call(0)\
{\
assert(sizeof(MemCall<O,F>) <= size);\
m_call = new (&m_buffer[0]) MemCall<O,F>(obj,f);\
}\
template<typename F>\
CLASSNAME(F f):m_call(0)\
{\
m_call = new (&m_buffer[0]) FunCall<F>(f);\
}\
CLASSNAME():m_call(0){}\
CLASSNAME(const CLASSNAME& cf)\
{\
m_call = (Call*)&m_buffer[0];\
memcpy(m_buffer,cf.m_buffer,sizeof(m_buffer));\
}\
void operator = (const CLASSNAME& cf)\
{\
m_call = (Call*)&m_buffer[0];\
memcpy(m_buffer,cf.m_buffer,sizeof(m_buffer));\
}\
RT call(TPARAM)\
{\
if(m_call)return m_call->call(PARAM);\
return RT();\
}\
RT call(TPARAM) const\
{\
if(m_call)return m_call->call(PARAM);\
return RT();\
}\
protected:\
Call* m_call;\
char m_buffer[size];\
};
template <typename RT, size_t size = 4 * sizeof(void*)>
class CallFunc_0
{
protected:
class Call
{
public:
virtual RT call() = 0;
virtual RT call() const = 0;
};
template<typename O,typename F>
class MemCall : public Call{
public:
MemCall(O* obj,F f):m_obj(obj),m_func(f){}
protected:
O* m_obj;
F m_func;
virtual RT call()
{
return ((m_obj)->*m_func)();
}
virtual RT call() const
{
return ((m_obj)->*m_func)();
}
};
template<typename F>
class FunCall : Call
{
public:
FunCall(F f):m_func(f){}
protected:
F m_func;
virtual RT call()
{ return m_func();}
virtual RT call() const
{ return m_func();}
};
public:
template <typename O,typename F>
CallFunc_0(O* obj,F f):m_call(0)
{
assert(sizeof(MemCall<O,F>) <= size);
m_call = new (&m_buffer[0]) MemCall<O,F>(obj,f);
}
template<typename F>
CallFunc_0(F f):m_call(0)
{
m_call = new (&m_buffer[0]) FunCall<F>(f);
}
CallFunc_0():m_call(0){}
CallFunc_0(const CallFunc_0& cf)
{
m_call = (Call*)&m_buffer[0];
memcpy(m_buffer,cf.m_buffer,sizeof(m_buffer));
}
void operator = (const CallFunc_0& cf)
{
m_call = (Call*)&m_buffer[0];
memcpy(m_buffer,cf.m_buffer,sizeof(m_buffer));
}
RT call()
{
if(m_call)return m_call->call();
return RT();
}
RT call() const
{
if(m_call)return m_call->call();
return RT();
}
protected:
Call* m_call;
char m_buffer[size];
};
#define TEMPLATE typename P1, 这种define 看不懂??
#define TPARAM P1 p1
#define PARAM p1
DEFINE_CALL(CallFunc_1)
#undef TPARAM
#undef PARAM
#undef TEMPLATE
#define TEMPLATE typename P1,typename P2, 同样看不懂
#define TPARAM P1 p1,P2 p2
#define PARAM p1,p2
DEFINE_CALL(CallFunc_2)
#undef TPARAM
#undef PARAM
#undef TEMPLATE
求大神解释 #define问题
最新推荐文章于 2021-02-23 00:21:26 发布