stl function擴展(一)

#ifndef _FUNCTION_LIB_H_
#define _FUNCTION_LIB_H_

#include <functional>

namespace function_lib
{
	/*
	*仿函數功能:將二元仿函數的第一個參數綁定,使之成爲一元仿函數;
	*/
	template<class _Fn2>
	class binder1st
		: public std::binary_function<typename _Fn2::first_argument_type,
		typename _Fn2::second_argument_type,
		typename _Fn2::result_type>
	{
	public:
		typedef std::binary_function<typename _Fn2::first_argument_type,
			typename _Fn2::second_argument_type,
			typename _Fn2::result_type> _Base;
		typedef typename _Base::first_argument_type first_argument_type;
		typedef typename _Base::second_argument_type second_argument_type;
		typedef typename _Base::result_type result_type;

		binder1st(_Fn2& _Func,
			const first_argument_type &_Left)
			: op(_Func), value(_Left)
		{
		}

		result_type operator()(const second_argument_type &_Right)
		{
			return (op(value, _Right));
		}

		result_type operator()(second_argument_type &_Right)
		{
			return (op(value, _Right));
		}

	protected:
		_Fn2 op;
		typename _Fn2::first_argument_type value;
	};

	template<class _Fn2,
		class _Ty>
	function_lib::binder1st<_Fn2> bind1st(_Fn2& _Func, const _Ty& _Left)
		{
			typename _Fn2::first_argument_type _Val(_Left);
		return (function_lib::binder1st<_Fn2>(_Func, _Val));
	}

	/*
	*仿函數功能:將二元仿函數的第一個參數綁定,使之成爲一元仿函數;
	*/
	template<class _Fn2>
	class const_binder1st
		: public std::binary_function<typename _Fn2::first_argument_type,
		typename _Fn2::second_argument_type,
		typename _Fn2::result_type>
	{
	public:
		typedef std::binary_function<typename _Fn2::first_argument_type,
			typename _Fn2::second_argument_type,
			typename _Fn2::result_type> _Base;
		typedef typename _Base::first_argument_type first_argument_type;
		typedef typename _Base::second_argument_type second_argument_type;
		typedef typename _Base::result_type result_type;

		const_binder1st(_Fn2 &_Func,
			const first_argument_type &_Left)
			: op(_Func), value(_Left)
		{
		}

		result_type operator()(second_argument_type &_Right)
		{
			return (op(value, _Right));
		}

	protected:
		_Fn2 op;
		typename _Fn2::first_argument_type value;
	};

	template<class _Fn2,
	class _Ty>
		function_lib::const_binder1st<_Fn2> const_bind1st(_Fn2 &_Func, const _Ty& _Left)
	{
		typename _Fn2::first_argument_type _Val(_Left);
		return (function_lib::const_binder1st<_Fn2>(_Func, _Val));
	}

	/*
	*仿函數功能:通過指針,調用一個對象的成員函數,並傳遞一個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
	class _Ty,
	class _Arg>
	class obj_mem_fun_t : public std::unary_function<_Arg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_Arg);

		obj_mem_fun_t(_Ty *_pObj, _MemFunPtr _pMemFunPtr)
			: m_pObj(_pObj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(const _Arg &_arg)
		{
			(m_pObj->*m_pMemFunPtr)(_arg);
		}

		void operator ()(_Arg &_arg)
		{
			(m_pObj->*m_pMemFunPtr)(_arg);
		}

	private:
		_Ty *m_pObj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result,
	class _Ty,
	class _Arg>
	obj_mem_fun_t<_Result, _Ty, _Arg>
	obj_mem_fun(_Ty *_pObj, _Result (_Ty::*_pm)(_Arg))
	{
		return obj_mem_fun_t<_Result, _Ty, _Arg>(_pObj, _pm);
	}

	/*
	*仿函數功能:通過指針,調用一個對象的成員函數,並傳遞兩個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
		class _Ty,
		class _LeftArg,
		class _RightArg>
	class obj_mem_fun1_t : public std::binary_function<_LeftArg, _RightArg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg);

		obj_mem_fun1_t(_Ty *_pObj, _MemFunPtr _pMemFunPtr)
			: m_pObj(_pObj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(const _LeftArg &_left, const _RightArg &_right)
		{
			(m_pObj->*m_pMemFunPtr)(_left, _right);
		}
		void operator ()(_LeftArg &_left, _RightArg &_right)
		{
			(m_pObj->*m_pMemFunPtr)(_left, _right);
		}

	private:
		_Ty *m_pObj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result,
		class _Ty,
		class _LeftArg,
		class _RightArg>
	obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>
	obj_mem_fun(_Ty *_pObj, _Result (_Ty::*_pm)(_LeftArg, _RightArg))
	{
		return obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>(_pObj, _pm);
	}

	/*
	*仿函數功能:通過指針,調用一個對象的const成員函數,並傳遞一個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
		class _Ty,
		class _Arg>
	class const_obj_mem_fun_t : public std::unary_function<_Arg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_Arg) const;

		const_obj_mem_fun_t(const _Ty *_pObj, _MemFunPtr _pMemFunPtr)
			: m_pObj(_pObj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(_Arg _arg)
		{
			(m_pObj->*m_pMemFunPtr)(_arg);
		}

	private:
		const _Ty *m_pObj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result,
	class _Ty,
	class _Arg>
	const_obj_mem_fun_t<_Result, _Ty, _Arg>
	const_obj_mem_fun(const _Ty *_pObj, _Result (_Ty::*_pm)(_Arg) const)
	{
		return const_obj_mem_fun_t<_Result, _Ty, _Arg>(_pObj, _pm);
	}

	/*
	*仿函數功能:通過指針,調用一個對象的const成員函數,並傳遞兩個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
		class _Ty,
		class _LeftArg,
		class _RightArg>
	class const_obj_mem_fun1_t : public std::binary_function<_LeftArg, _RightArg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg) const;

		const_obj_mem_fun1_t(const _Ty *_pObj, _MemFunPtr _pMemFunPtr)
			: m_pObj(_pObj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(_LeftArg &_left, _RightArg &_right)
		{
			(m_pObj->*m_pMemFunPtr)(_left, _right);
		}

	private:
		const _Ty *m_pObj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result,
		class _Ty,
		class _LeftArg,
		class _RightArg>
	const_obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>
	const_obj_mem_fun(const _Ty *_pObj, _Result (_Ty::*_pm)(_LeftArg, _RightArg) const)
	{
		return const_obj_mem_fun1_t<_Result, _Ty, _LeftArg, _RightArg>(_pObj, _pm);
	}

	/*
	*仿函數功能:通過對象引用,調用對象的成員函數,並傳遞一個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
	class _Ty,
	class _Arg>
	class obj_mem_fun_ref_t : public std::unary_function<_Arg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_Arg);

		obj_mem_fun_ref_t(_Ty &_obj, _MemFunPtr _pMemFunPtr)
			: m_obj(_obj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(_Arg _arg)
		{
			(m_obj.*m_pMemFunPtr)(_arg);
		}

	private:
		_Ty &m_obj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result, 
	class _Ty, 
	class _Arg>
		obj_mem_fun_ref_t<_Result, 
		_Ty, 
		_Arg>
		obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_Arg))
	{
		return obj_mem_fun_ref_t<_Result, _Ty, _Arg>(_obj, _pm);
	}

	/*
	*仿函數功能:通過對象引用,調用對象的const成員函數,並傳遞一個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
	class _Ty,
	class _Arg>
	class const_obj_mem_fun_ref_t : public std::unary_function<_Arg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_Arg) const;

		const_obj_mem_fun_ref_t(const _Ty &_obj, _MemFunPtr _pMemFunPtr)
			: m_obj(_obj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(_Arg _arg)
		{
			(m_obj.*m_pMemFunPtr)(_arg);
		}

	private:
		const _Ty &m_obj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result, 
	class _Ty, 
	class _Arg>
		const_obj_mem_fun_ref_t<_Result, 
		_Ty, 
		_Arg>
		const_obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_Arg) const)
	{
		return const_obj_mem_fun_ref_t<_Result, _Ty, _Arg>(_obj, _pm);
	}

	/*
	*仿函數功能:通過對象的引用,調用對象的成員函數,並傳遞兩個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
		class _Ty,
		class _LeftArg,
		class _RightArg>
	class obj_mem_fun1_ref_t : public std::binary_function<_LeftArg, _RightArg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg);

		obj_mem_fun1_ref_t(_Ty &_obj, _MemFunPtr _pMemFunPtr)
			: m_obj(_obj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(_LeftArg _left, _RightArg _right)
		{
			(m_obj.*m_pMemFunPtr)(_left, _right);
		}

	private:
		_Ty &m_obj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result, 
		class _Ty, 
		class _LeftArg,
		class _RightArg>
	obj_mem_fun1_ref_t<_Result, 
		_Ty, 
		_LeftArg,
		_RightArg>
	obj_mem_fun_ref(_Ty &_obj, _Result (_Ty::*_pm)(_LeftArg, _RightArg))
	{
		return obj_mem_fun1_ref_t<_Result, _Ty, _LeftArg, _RightArg>(_obj, _pm);
	}

	/*
	*仿函數功能:通過對象的引用,調用對象的const成員函數,並傳遞兩個參數;
		如果成員函數聲明中該參數爲引用,則傳遞爲引用;
	*/
	template<class _Result,
	class _Ty,
	class _LeftArg,
	class _RightArg>
	class const_obj_mem_fun1_ref_t : public std::binary_function<_LeftArg, _RightArg, void>
	{
	public:
		typedef _Result (_Ty::*_MemFunPtr)(_LeftArg, _RightArg) const;

		const_obj_mem_fun1_ref_t(const _Ty &_obj, _MemFunPtr _pMemFunPtr)
			: m_obj(_obj), 
			m_pMemFunPtr(_pMemFunPtr)
		{
		}

		void operator ()(_LeftArg _left, _RightArg _right)
		{
			(m_obj.*m_pMemFunPtr)(_left, _right);
		}

	private:
		const _Ty &m_obj;
		_MemFunPtr m_pMemFunPtr;
	};

	template<class _Result, 
	class _Ty, 
	class _LeftArg,
	class _RightArg>
		const_obj_mem_fun1_ref_t<_Result, 
		_Ty, 
		_LeftArg,
		_RightArg>
		const_obj_mem_fun_ref(const _Ty &_obj, _Result (_Ty::*_pm)(_LeftArg, _RightArg) const)
	{
		return const_obj_mem_fun1_ref_t<_Result, _Ty, _LeftArg, _RightArg>(_obj, _pm);
	}
}

#endif


調用示例:

定義

//這裏省略組合框窗口創建過程...
CComboBox m_combo;
CString arString[] = {
		_T("1"), 
		_T("2"), 
		_T("3"), 
		_T("4"), 
		_T("5"), 
		_T("6")
	};
int nCount = sizeof(arString) / sizeof(CString);


1、

auto combo1 = function_lib::obj_mem_fun(&m_combo, &CComboBox::AddString);
std::for_each(arString, arString + nCount, combo1);
m_combo.ResetContent();


2、

auto combo2 = function_lib::bind1st(function_lib::obj_mem_fun(&m_combo, &CComboBox::InsertString), -1);
std::for_each(arString, arString + nCount, combo2);


3、

auto func = function_lib::const_obj_mem_fun1_t<void, CComboBox, int, CString&>(&m_combo, &CComboBox::GetLBText);
auto combo3 = function_lib::const_bind1st(func, 0);
std::for_each(arString, arString + nCount, combo3);
//失敗,無法將CString轉化爲LPTSTR
//auto func = function_lib::const_obj_mem_fun1_t<int, CComboBox, int, LPTSTR >(&m_combo, &CComboBox::GetLBText);
//auto combo3 = function_lib::const_bind1st(func, 0);
//std::for_each(arString, arString + nCount, combo3);


4、

m_combo.ResetContent();
auto combo4 = function_lib::obj_mem_fun_ref(m_combo, &CComboBox::AddString);
std::for_each(arString, arString + nCount, combo4);


5、

m_combo.ResetContent();
auto combo5 = function_lib::bind1st(function_lib::obj_mem_fun_ref(m_combo, &CComboBox::InsertString), 0);
std::for_each(arString, arString + nCount, combo5);


6、

auto func6 = function_lib::const_obj_mem_fun1_ref_t<void, CComboBox, int, CString&>(m_combo, &CComboBox::GetLBText);
auto combo6 = function_lib::const_bind1st(func6, 0);
std::for_each(arString, arString+ nCount, combo6);


 

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