c++ 從頭寫高效仿函數(function)

        在程序設計中,很多時候需要回調函數.傳統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);
	
}

         

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章