[迁移]注册类中的成员函数,在需要的时候调用(c++)

这是一个C++实现,用于在需要时动态调用类的成员函数。通过定义抽象槽类AbstratSlot、FunctorSlot和MemberSlot,实现了函数指针和成员函数指针的绑定,以便在事件发生时执行相应的函数。示例展示了如何创建FHandle和EventHandle对象,以及如何连接和执行成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

声明:因网易博客将关闭,移到此

重新修改版本

#ifndef HANDLE_H
#define HANDLE_H

#include
#include

using namespace std;

//----------------------------------------------------------------------
//
class EventArgs
{
public:
string mType;
void * mData;

EventArgs() : mType(“unknow”), mData(NULL) {}
EventArgs(const string& type, void* data = NULL) : mType(type), mData(data) {}
};

//----------------------------------------------------------------------
//
class AbstratSlot
{
public:
virtual ~AbstratSlot() {}
virtual bool execute() = 0;
virtual bool execute(const EventArgs& e) = 0;
};

//----------------------------------------------------------------------
//
typedef bool (Function)(const EventArgs& e);
class FunctorSlot : public AbstratSlot
{
public:
FunctorSlot() : mFunc(NULL)
{}

FunctorSlot(Function func, const EventArgs& e)
mFunc(func), mArgs(e)
{}
bool execute() { if (mFunc) return (*mFunc)(mArgs); return false; }
bool execute(const EventArgs& e) { if (mFunc) return (*mFunc)(e); return false; }
protected:
EventArgs mArgs;
Function *mFunc;
};

//----------------------------------------------------------------------
//
template
class MemberSlot : public AbstratSlot
{
public:
MemberSlot() : mFunc(NULL)
{}

typedef bool (T::MFunction)(const EventArgs& e);
MemberSlot(T
obj, MFunction func, const EventArgs& e)
: mObj(obj), mFunc(func), mArgs(e)
{}

bool execute() { if (mFunc) return (mObj->*mFunc)(mArgs); return false; }
bool execute(const EventArgs& e) { if (mFunc) return (mObj->*mFunc)(e); return false; }

protected:
EventArgs mArgs;
MFunction mFunc;
T* mObj;
};

//----------------------------------------------------------------------
//
class FHandle
{
public:
FHandle() : mSlot(NULL) {}

FHandle(Function func) : mSlot(new FunctorSlot(func, EventArgs())) {}

FHandle(Function func, const EventArgs& e) : mSlot(new FunctorSlot(func, e)) {}

template
FHandle(T1* obj, bool (T1::*func)(const EventArgs&))
: mSlot(new MemberSlot(obj, func, EventArgs()))
{}

template
FHandle(T2* obj, bool (T2::*func)(const EventArgs&), const EventArgs& e)
: mSlot(new MemberSlot(obj, func, e))
{}

~FHandle() {}

void cleanup() { if (mSlot) delete mSlot; mSlot = NULL; }
bool execute() { if (mSlot) return mSlot->execute(); return false; }
bool execute(const EventArgs& e) { if (mSlot) return mSlot->execute(e); return false; }
protected:
AbstratSlot *mSlot;
};

//----------------------------------------------------------------------
//
class EventHandle
{
public:
EventHandle() {}
~EventHandle() { disconnect(); }

void connect(const FHandle& handle)
{
disconnect();
mHandle = handle;
}

void disconnect()
{
mHandle.cleanup();
}

bool execute()
{
return mHandle.execute();
}

bool execute(const EventArgs& e)
{
return mHandle.execute(e);
}

protected:
FHandle mHandle;
};

#endif // HANDLE_H

#include
#include
#include “handle.h”

using namespace std;

//----------------------------------------------------------------------
bool echo(const EventArgs& e)
{
cout<<“function echo”<<e.mType.c_str()<<endl;
return true;
}

//----------------------------------------------------------------------
class TC
{
public:
bool echo(const EventArgs& e)
{
cout<<“TC::echo”<<e.mType.c_str()<<endl;
return true;
}
};

//----------------------------------------------------------------------
// 测试类
class TestHandle
{
public:
void connect(const FHandle& handle)
{
mEcho.connect(handle);
}

bool call1() { return mEcho.execute(); }

bool call2()
{
EventArgs tmpE(“go”, “haha”);
return mEcho.execute(tmpE);
}

private:
EventHandle mEcho;
};

//----------------------------------------------------------------------

int main(int argc, char **argv)
{
TestHandle tmpHandle;

tmpHandle.connect(FHandle(echo));
tmpHandle.call1();

TC tmpTC;
tmpHandle.connect(FHandle(&tmpTC, &TC::echo));
tmpHandle.call2();

system(“pause”);

return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
下面是旧版本,今天刚意识到原来是有问题的
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include “FunCall.h”

/***********************************************************************/
/

需求:我想调用类中的函数如何实现?
这个是参考CEGUI中的事件来实现的

具体的需求是:我写了一个动画类,我想在动画播放中的每一帧调用我类中写的函数来进行一些处理,动画结束后调用我类中的函数类处理一些东西,所以出现了这样的需求

/
/
***********************************************************************/

class TestCall
{
public:
TestCall(void){}
~TestCall(void){}

void echo(void * msg)
{
    char *mMsg = (char*)msg;
    cout<<mMsg<<endl;
}

protected:
private:
};

//-----------------------------------------------------------------------------
// 这个是被调用的函数
void echo(void *msg)
{
char mMsg = (char)msg;
cout<<mMsg<<endl;
}

int main(int argc, char ** argv)
{
char *msg1 = “msg1”;
char *msg2 = “msg2”;

TestCall tmpTC;
UseHandler tmpUH1,tmpUH2;

tmpUH1.add(FunHandler(&tmpTC, &TestCall::echo, msg1));
tmpUH1.call();

tmpUH2.add(FunHandler(&echo, msg2));
tmpUH2.call();

system("pause");

return 0;

}

#ifndef FUNC_CALL_H
#define FUNC_CALL_H

#include
#include

using namespace std;

//
class SlotFunctorBase
{
public:
virtual ~SlotFunctorBase(void) {}
virtual void call(void) = 0;
};

//-----------------------------------------------------------------------------
// 这个是调用类的时候使用
template
class ClassSlotFunctor : public SlotFunctorBase
{
public:
typedef void (T::*func)(void *);
ClassSlotFunctor(T *obj, func f, void *data)
:obj(obj), mFun(f), mData(data)
{}

~ClassSlotFunctor(){}
void call(void){ (obj->*mFun)(mData); }

protected:
T *obj;
func mFun;
void *mData;
};

//-----------------------------------------------------------------------------
// 这个是需要调用函数的时候使用
class FunSlotFunctor : public SlotFunctorBase
{
public:
typedef void (*func)(void *data);
FunSlotFunctor(func f, void *data = NULL) : mFun(f) ,mData(data) {}

void call(void) { (*mFun)(mData); }

protected:
func mFun;
void *mData;
private:
};

//-----------------------------------------------------------------------------
// 这个是调用的接口
class FunHandler
{
protected:
SlotFunctorBase *mFun;
public:
FunHandler(void)
:mFun(0)
{}

~FunHandler(void)
{
     if (mFun) delete mFun;
}

template<typename T>
FunHandler(T *objPoiter, void (T::*fun)(void *data), void* data = NULL)
    :mFun(new ClassSlotFunctor<T>(objPoiter, fun, data))
{}

FunHandler(void (*fun)(void *data), void *data = NULL)
    :mFun(new FunSlotFunctor(fun, data))
{}

void call(void) { if (mFun) mFun->call(); }

};

class UseHandler
{
public:

void add(const FunHandler& fun) { mFun = fun; }
void call(void){ mFun.call(); }

protected:
FunHandler mFun;
private:
};

#endif // FUNC_CALL_H

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值