boost:bind解析

mybind.h实现:

#ifndef BOOST_BIND_BIND_HPP_INCLUDED__Mybind
#define BOOST_BIND_BIND_HPP_INCLUDED__Mybind

#include <iostream>
using namespace std;
namespace boost {

	//占位符对象
	template<int I> struct arg {
		arg() {
		}
	};
	template<class T>
	//类型
	struct type {
	};
	namespace _bi // implementation details
	{
		//storage1存储一个元素
		template<class A1> struct storage1 {
			explicit storage1(A1 a1) :
			a1_(a1) {
				cout << "storage1  storage1(A1 a1)" << endl;
			}
			A1 a1_;
		};
		//storage1特例化
		template<int I> struct storage1<boost::arg<I> > {
			explicit storage1(boost::arg<I>) {
				cout << "storage1  storage1(boost::arg<I>)" << endl;
			}
			static boost::arg<I> a1_() {
				return boost::arg<I>();
			}
		};
		//storage1特例化
		template<int I> struct storage1<boost::arg<I>(*)()> {
			explicit storage1(boost::arg<I>(*)()) {
				cout << "storage1  storage1(boost::arg<I> (*)())" << endl;
			}
			static boost::arg<I> a1_() {
				return boost::arg<I>();
			}
		};
		//storage2存储2个元素,继承storage1
		template<class A1, class A2> struct storage2 : public storage1<A1> {
			typedef storage1<A1> inherited;
			storage2(A1 a1, A2 a2) :
				storage1<A1>(a1), a2_(a2) {
					cout << "storage2  storage2(A1 a1, A2 a2)" << endl;
				}
			A2 a2_;
		};
		//storage2特例化
		template<class A1, int I> struct storage2<A1, boost::arg<I> > : public storage1<
			A1>{
			typedef storage1<A1> inherited;
			storage2(A1 a1, boost::arg<I>) :
				storage1<A1>(a1) {
					cout << "storage2  storage2(A1 a1, boost::arg<I>)" << endl;
				}
			static boost::arg<I> a2_() {
				return boost::arg<I>();
			}
		};
		//storage2特例化
		template<class A1, int I> struct storage2<A1, boost::arg<I>(*)()> : public storage1<
			A1>{
			typedef storage1<A1> inherited;
			storage2(A1 a1, boost::arg<I>(*)()) :
				storage1<A1>(a1) {
					cout << "storage2  storage2(A1 a1, boost::arg<I> (*)())" << endl;
				}
			static boost::arg<I> a2_() {
				return boost::arg<I>();
			}
		};
		///////////////////////////////////////////
		// result_traits 萃取器,萃取返回值类型
		template<class R, class F> struct result_traits {
			typedef R type;
		};
		template<class T> class type {
		};
		struct unspecified {
		};
		template<class F> struct result_traits<unspecified, F> {
			typedef typename F::result_type type;
		};
		// type

		// unwrap
		template<class F> struct unwrapper {
			static inline F & unwrap(F & f, long) {
				return f;
			}
		};
		/////////////////////////////////////////

		// value 存储真实的值
		template<class T> class value {
		public:
			value(T const & t) :
				t_(t) {
			}
			T & get() {
				return t_;
			}
		private:
			T t_;
		};

		// list0 0个参数的组装函数
		class list0 {
		public:
			list0() {
			}
			//重载[]用来取值
			template<class T> T & operator[](_bi::value<T> & v) const {
				cout << "list0  T & operator[](_bi::value<T> & v)" << endl;
				return v.get();
			}
			//重载()用来调用
			template<class R, class F, class A> R operator()(type<R>, F & f, A &,
				long) {
				cout << "list0  R operator()(type<R>, F & f, A &, long)" << endl;
				return unwrapper<F>::unwrap(f, 0)();
			}
			//重载()用来调用的特例化
			template<class F, class A> void operator()(type<void>, F & f, A &, int) {
				cout << "list0  void operator()(type<void>, F & f, A &, int)" << endl;
				unwrapper<F>::unwrap(f, 0)();
			}
		};
		//list1 1个参数的组装函数 ,继承storage1存储一个值
		template<class A1> class list1 : private storage1<A1> {
		private:
			typedef storage1<A1> base_type;
		public:
			explicit list1(A1 a1) :
				base_type(a1) {//构造的时候存储值到storage1
			}
			//重载[]用来取值
			A1 operator[](boost::arg<1>) const {
				return base_type::a1_;
			}
			//重载[]用来取值
			A1 operator[](boost::arg<1>(*)()) const {
				return base_type::a1_;
			}
			//重载[]用来取值
			template<class T> T & operator[](_bi::value<T> & v) const {
				return v.get();
			}
			//重载()用来调用
			template<class R, class F, class A> R operator()(type<R>, F & f, A & a,
				long) {
				return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_]);
			}

		};
		//list2 1个参数的组装函数 ,继承storage2存储二个值
		template<class A1, class A2> class list2 : private storage2<A1, A2> {
		private:
			typedef storage2<A1, A2> base_type;
		public:
			list2(A1 a1, A2 a2) :
				base_type(a1, a2) { //构造的时候存储值到storage2
			}
			//重载[]用来取值
			A1 operator[](boost::arg<1>) const {
				cout << "list2  A1 operator[](boost::arg<1>)" << endl;
				return base_type::a1_;
			}
			//重载[]用来取值
			A2 operator[](boost::arg<2>) const {
				cout << "list2  A1 operator[](boost::arg<2>)" << endl;
				return base_type::a2_;
			}
			//重载[]用来取值
			A1 operator[](boost::arg<1>(*)()) const {
				cout << "list2  A1 operator[](boost::arg<1> (*)())" << endl;
				return base_type::a1_;
			}
			//重载[]用来取值
			A2 operator[](boost::arg<2>(*)()) const {
				cout << "list2  A1 operator[](boost::arg<2> (*)())" << endl;
				return base_type::a2_;
			}
			//重载[]用来取值
			template<class T> T & operator[](_bi::value<T> & v) const {
				cout << "T & operator[](_bi::value<T> & v)" << endl;
				return v.get();
			}
			//重载()用来调用
			template<class R, class F, class A> R operator()(type<R>, F & f, A & a,
				long) {
				cout << "/////////////" << typeid(a).name() << endl;
				cout << typeid(base_type::a1_).name() << typeid(base_type::a2_).name() << endl; //第一个是value<T>变量,第二个是arg<1>(*)()指针
				cout << typeid(a[base_type::a1_]).name() << typeid(a[base_type::a2_]).name() << endl;
				return f(a[base_type::a1_], a[base_type::a2_]);//
			}

		};
		// bind_t bind函数返回的对象
		template<class R, class F, class L> class bind_t {  //goto  209
		public:
			typedef bind_t this_type;
			bind_t(F f, L const & l) :
				f_(f), l_(l) { //初始化function=f,list=l  //构造结束返回bind_t对象209
				cout << "l_ typeid" << typeid(l_).name();
			}

			typedef typename result_traits<R, F>::type result_type;//定义返回值类型
			//重载()用来调用
			result_type operator()() {
				cout << "bind_t::result_type operator()()" << endl;
				list0 a;
				return l_(type<result_type>(), f_, a, 0);
			}
			//重载()用来调用
			template<class A1> result_type operator()(A1 & a1) {
				list1<A1 &> a(a1);
				return l_(type<result_type>(), f_, a, 0);
			}
			//重载()用来调用
			template<class A1, class A2> result_type operator()(A1 & a1, A2 & a2) { //调用开始 bind_t<R, F, list_av_2<int, arg<1>>>();
				list2<A1 &, A2 &> a(a1, a2);//list_av_2<int, int >(2,1)
				return l_(type<result_type>(), f_, a, 0); //list2<int, arg<1>>(R, F,list2<int, int >(2,1),0)   =>goto196
			}
		private:
			F f_;
			L l_; //list2<int, arg<1>>
		};
		//////////////////////////////////////////////
		//接收list_av_2,list_av_1传递的基本类型,并且返回value<T>
		template<class T> struct add_value {
			typedef typename value<T> type;
		};
		//接收list_av_2,list_av_1传递的<arg<I>>类型,并且返回arg<I>
		template<int I> struct add_value<arg<I> > {
			typedef boost::arg<I> type;
		};
		//////////////////////////////////////////////
		template<class R, class F, class L> struct add_value<bind_t<R, F, L> > {
			typedef bind_t<R, F, L> type;
		};
		// list_av_1 返回模板参数的类型

		template<class A1> struct list_av_1 {
			typedef typename add_value<A1>::type B1;
			typedef list1<B1> type;
		};
		// list_av_2 返回模板参数的类型
		template<class A1, class A2> struct list_av_2 {
			typedef typename add_value<A1>::type B1;//=> goto 233
			typedef typename add_value<A2>::type B2;//=> goto 237
			typedef list2<B1, B2> type;
		};
	}

	//bind2函数,接受0个参数的函数
	template<class R>
	_bi::bind_t<R, R(*)(), _bi::list0> bind2(R(*f)()) {
		typedef R(*F)();
		typedef _bi::list0 list_type;
		return _bi::bind_t<R, F, list_type>(f, list_type());
	}
	//bind2函数,接受1个参数的函数
	template<class R, class B1, class A1>
	_bi::bind_t<R, R(*)(B1), typename _bi::list_av_1<A1>::type> bind2(R(*f)(B1),
		A1 a1) {
		typedef R(*F)(B1);
		typedef typename _bi::list_av_1<A1>::type list_type;
		return _bi::bind_t<R, F, list_type>(f, list_type(a1));
	}
	//bind2函数,接受2个参数的函数
	template<class R, class B1, class B2, class A1, class A2>
	_bi::bind_t<R, R(*)(B1, B2), typename _bi::list_av_2<A1, A2>::type> bind2(
		R(*f)(B1, B2), A1 a1, A2 a2) {
		typedef R(*F)(B1, B2);
		typedef typename _bi::list_av_2<A1, A2>::type list_type;  //=> goto  251
		return _bi::bind_t<R, F, list_type>(f, list_type(a1, a2));//=> goto  203  
	}
}// namespace boost

namespace {
	boost::arg<1> _1;
	boost::arg<2> _2;
}
#endif // #ifndef BOOST_BIND_BIND_HPP_INCLUDED__Mybind

main:

#include <iostream>
#include "mybind.h"
using namespace std;
void tow_arguments(int i1, int i2) {
	std::cout << i1 << i2 << '\n';
}

class Test{
};
void testClass(Test t, int i){
	cout << "testClass,Test,int" << endl;
}

int main() {
	int i1 = 1, i2 = 2;

	(boost::bind2(&tow_arguments, 123, _1))(i2);
	(boost::bind2(&tow_arguments, _1, _2))(i1, i2);
	(boost::bind2(&tow_arguments, _2, _1))(i1, i2);
	(boost::bind2(&tow_arguments, _1, _1))(i1, i2);

	(boost::bind2(&tow_arguments, 222, 666))(i1, i2);

	Test t;
	(boost::bind2(&testClass, _1, 666))(t, i2);
	(boost::bind2(&testClass, _1, 666))(t);
	(boost::bind2(&testClass, t, 666))();
	getchar();
}

输出结果:

storage1  storage1(A1 a1)
storage2  storage2(A1 a1, boost::arg<I>)
l_ typeidclass boost::_bi::list2<class boost::_bi::value<int>,struct boost::arg<1> >storage1  storage1(A1 a1)
/////////////class boost::_bi::list1<int &>
class boost::_bi::value<int>struct boost::arg<1> __cdecl(void)
intint
1232
storage1  storage1(boost::arg<I>)
storage2  storage2(A1 a1, boost::arg<I>)
l_ typeidclass boost::_bi::list2<struct boost::arg<1>,struct boost::arg<2> >storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
/////////////class boost::_bi::list2<int &,int &>
struct boost::arg<1> __cdecl(void)struct boost::arg<2> __cdecl(void)
intint
list2  A1 operator[](boost::arg<2> (*)())
list2  A1 operator[](boost::arg<1> (*)())
12
storage1  storage1(boost::arg<I>)
storage2  storage2(A1 a1, boost::arg<I>)
l_ typeidclass boost::_bi::list2<struct boost::arg<2>,struct boost::arg<1> >storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
/////////////class boost::_bi::list2<int &,int &>
struct boost::arg<2> __cdecl(void)struct boost::arg<1> __cdecl(void)
intint
list2  A1 operator[](boost::arg<1> (*)())
list2  A1 operator[](boost::arg<2> (*)())
21
storage1  storage1(boost::arg<I>)
storage2  storage2(A1 a1, boost::arg<I>)
l_ typeidclass boost::_bi::list2<struct boost::arg<1>,struct boost::arg<1> >storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
/////////////class boost::_bi::list2<int &,int &>
struct boost::arg<1> __cdecl(void)struct boost::arg<1> __cdecl(void)
intint
list2  A1 operator[](boost::arg<1> (*)())
list2  A1 operator[](boost::arg<1> (*)())
11
storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
l_ typeidclass boost::_bi::list2<class boost::_bi::value<int>,class boost::_bi::value<int> >storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
/////////////class boost::_bi::list2<int &,int &>
class boost::_bi::value<int>class boost::_bi::value<int>
intint
T & operator[](_bi::value<T> & v)
T & operator[](_bi::value<T> & v)
222666
storage1  storage1(boost::arg<I>)
storage2  storage2(A1 a1, A2 a2)
l_ typeidclass boost::_bi::list2<struct boost::arg<1>,class boost::_bi::value<int> >storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
/////////////class boost::_bi::list2<class Test &,int &>
struct boost::arg<1> __cdecl(void)class boost::_bi::value<int>
class Testint
T & operator[](_bi::value<T> & v)
list2  A1 operator[](boost::arg<1> (*)())
testClass,Test,int
storage1  storage1(boost::arg<I>)
storage2  storage2(A1 a1, A2 a2)
l_ typeidclass boost::_bi::list2<struct boost::arg<1>,class boost::_bi::value<int> >storage1  storage1(A1 a1)
/////////////class boost::_bi::list1<class Test &>
struct boost::arg<1> __cdecl(void)class boost::_bi::value<int>
class Testint
testClass,Test,int
storage1  storage1(A1 a1)
storage2  storage2(A1 a1, A2 a2)
l_ typeidclass boost::_bi::list2<class boost::_bi::value<class Test>,class boost::_bi::value<int> >bind_t::result_type operator()()
/////////////class boost::_bi::list0
class boost::_bi::value<class Test>class boost::_bi::value<int>
class Testint
list0  T & operator[](_bi::value<T> & v)
list0  T & operator[](_bi::value<T> & v)
testClass,Test,int

猜你喜欢

转载自blog.csdn.net/qq_33762043/article/details/79945367