在程序设计中,很多时候需要回调函数.传统c回调函数,和c++的关系并不友好,需要定义静态函数,再转接给对象.这样子虽然代码不多.但是每次都需要绕个圈子,不够直爽.
c++ 新标准以后(>11?)std自带function,可以使用这个类实现任意类型函数作为回调函数.倒是非常方便了.但是几次调试后发现,封装的比较多,一次调用至少需要5-10次函数调用(未具体计算,反正调用堆栈很深),才能最终到达我们自己设定的函数.在某些计算密集的地方,感觉还是浪费了太多的cpu周期. 也许有人会说,直接定一个一个 handle类,讲handle对象传入作为回调.用户重写handle类里面的函数即可. 这样效率也很高,不会有额外调用.确实可以,但是这样就多出来一个类,仍然不如直接传入需要的函数和对象(任意类型对象)来得直接.所以按照分析,保存对象,和函数地址,包括封装抽象,每次回调应该不会超过2次函数调用(函数本身一次,一次额外包装调用).
这里利用了新的c++变参模板特性.对各种安全可能没做周全考虑,可能还有一些实现比较笨拙的地方,以后找到了更好的办法逐渐优化修改(一直的编程思想是:代码实现,不求一次最好,但求持久优化修改打磨).仅仅满足:
1)尽量少的额外函数调用.(仅仅增加一次额外调用)
2)可以传入任意函数:自由函数或者成员函数.只要参数和返回值类型一致即可.
具体简单实现(基于vs2015):
主要分为三个部分: 1函数的存储, 2函数的调用,3函数的绑定.
1函数的存储/保存
首先是函数的存储(统一保存),名字不一定合适,沿用了标准c++的binder名字.抽象基类:
template<typename T_Ret, typename... T_Args>
class CBaseFunctionBinder
{
typedef T_Ret(*T_Fun)(T_Args...);
public:
virtual T_Ret operator()(T_Args...args)
{
T_Ret ret;
return ret;
};
virtual CBaseFunctionBinder* Clone()
{
CBaseFunctionBinder<T_Ret, T_Args...>* pNew = new CBaseFunctionBinder<T_Ret, T_Args...>();
pNew->m_pFun = m_pFun;
pNew->m_pObject = m_pObject;
return pNew;
}
void* m_pObject = 0;
void* m_pFun = 0;
};
基于这个基类,分别进行自由函数和成员函数的偏特化,以便对bind过程的调用形式统一 .
自由函数的偏特化:
template< typename T_Ret, typename... T_Args>
class CFunctionBinder :
public CBaseFunctionBinder<T_Ret, T_Args...>
{
typedef T_Ret(*T_Fun)(T_Args...);
public:
CFunctionBinder()
{
}
CFunctionBinder(T_Ret(*pFun)(T_Args...))
{
m_pFun = (void*)pFun;
}
//
T_Ret operator()(T_Args...args)
{
T_Ret ret;
ret = (*(T_Fun)m_pFun)(args...);
return ret;
};
virtual CBaseFunctionBinder<T_Ret, T_Args...>* Clone()
{
CBaseFunctionBinder<T_Ret, T_Args...>* pNewObject = new CFunctionBinder<T_Ret, T_Args...>();
pNewObject->m_pFun = m_pFun;
pNewObject->m_pObject = m_pObject;
return pNewObject;
}
//T_Fun m_pFun;
};
成员函数的偏特化实现:
template<typename T_Ret, typename T_Class, typename... T_Args>
class CFunctionBinder<T_Ret(T_Class::*)(T_Args...)>
: public CBaseFunctionBinder<T_Ret, T_Args...>
//class CFunction<T_Ret, T_Class, ...T_Args>
//class CFunction
{
typedef T_Ret(T_Class::*T_Fun)(T_Args...);
public:
CFunctionBinder()
{
}
CFunctionBinder(T_Ret(T_Class::*pFun)(T_Args...), T_Class* pObject)
{
m_pFun = (void*)*(int64*)&pFun;
m_pObject = (void*)pObject;
}
// CFunction(T_Ret (*T_Class) fun, T_Args&& ... a)
// {
//
// }
T_Ret operator()(T_Args...args)
{
T_Ret ret;
ret = (((T_Class*)m_pObject)->*(*(T_Fun*)&m_pFun))(args...);
return ret;
};
virtual CBaseFunctionBinder<T_Ret, T_Args...>* Clone()
{
CBaseFunctionBinder<T_Ret, T_Args...>* pNewObject
= new CFunctionBinder<T_Ret(T_Class::*)(T_Args...)>();
pNewObject->m_pFun = m_pFun;
pNewObject->m_pObject = m_pObject;
return pNewObject;
}
//T_Class* m_pObject;
//T_Fun m_pFun;
};
2 函数的调用(外部使用类)
为了外部使用的一致性,使用一个类进行封装后.完全统一自由函数和成员函数的调用(回调)过程.
template<typename T_Ret, typename... T_Args>
class CFunction
{
typedef T_Ret(*T_Fun)(T_Args...);
public:
CFunction() {
ScDebug("%s:%p++\n", __FUNCTION__, this);
}
CFunction(const CFunction& other) {
if (other.m_pBinder)
m_pBinder = other.m_pBinder->Clone();
ScDebug("%s:%p++\n", __FUNCTION__, this);
}
virtual ~CFunction() {
ScDebug("%s:%p--\n", __FUNCTION__, this);
if (m_pBinder)
{
delete m_pBinder;
m_pBinder = nullptr;
}
}
CFunction& operator=(const CFunction&other)
{
ScDebug("%s:%p--\n", __FUNCTION__, this);
if (other.m_pBinder)
m_pBinder = other.m_pBinder->Clone();
return *this;
}
virtual T_Ret operator()(T_Args...args)
{
T_Ret ret;
ret = (*m_pBinder)(args...);
return ret;
};
CBaseFunctionBinder<T_Ret, T_Args...>* m_pBinder = 0;
};
3 函数的绑定过程
template<typename T_Ret, typename T_Class, typename... T_Args >
CFunction<T_Ret, T_Args...> Bind(T_Ret(T_Class::*pFun)(T_Args...), T_Class* pObject)
{
//CFunction<T_Ret, T_Args...>* pFunction
// = new CFunctionBinder<T_Ret(T_Class::*)(T_Args...)>(pFun, pObject);;
// CFunctionBinder<T_Ret(T_Class::*)(T_Args...)> function(pFun, pObject);
CFunction<T_Ret, T_Args...> function;
function.m_pBinder = new CFunctionBinder<T_Ret(T_Class::*)(T_Args...)>(pFun, pObject);;
return function;
};
template<typename T_Ret, typename... T_Args >
CFunction<T_Ret, T_Args...> Bind(T_Ret(*pFun)(T_Args...))
{
// CFunction<T_Ret, T_Args...>* pFunction
// = new CFunctionBinder<T_Ret, T_Args...>(pFun);
//CFunctionBinder<T_Ret, T_Args...> function(pFun);
CFunction<T_Ret, T_Args...> function;
function.m_pBinder = new CFunctionBinder<T_Ret, T_Args...>(pFun);
return function;
};
三个模块基本实现.以下是使用过程:
class CTestFunction {
public:
int Test()
{
ScDebug("%s: \n", __FUNCTION__);
return 0;
}
int Test1(int nValue)
{
ScDebug("%s: %d\n", __FUNCTION__, nValue);
return 0;
}
};
int Test1(int nValue)
{
ScDebug("%s: %d\n", __FUNCTION__, nValue);
return 0;
}
void TestFunction()
{
CTestFunction testFun;
CFunctionBinder<int(CTestFunction::*)(int)> function(&CTestFunction::Test1, &testFun);;
//CFunction<int, int> function(&CTestFunction::Test1, &testFun);;
CBaseFunctionBinder<int, int>* pFunction = &function;
(*pFunction)(10);
CFunctionBinder<int, int> function1(&Test1);
pFunction = &function1;
(*pFunction)(11);
//function(10);
CFunction<int, int> function2 = Bind(&CTestFunction::Test1, &testFun);
function2(12);
}