boost::mpl::vector(GCC)源码分析

//GCC支持关键字typeof,MSVC不支持。所以boost::mpl::vector GCC 版本的便是基于类型的识别。 //在使用typeof关键字时,如果是求函数的返回类型,这个函数实际上无需定义,只需要声明即可。 //下面可以看到:这个版本的vector使用函数重载来标记不同的vector参数,比如:vector<int,double> //第0位就是int。实现的方式就是利用一个函数,这个函数的返回类型是使用一个类型包装器,也就是 //int 返回整形,double返回double,而函数的参数就是一个标记,也是重载参数,用来区分不同的值,如上面的 //0对应int, 1对应 double. # define BOOST_MPL_AUX_LAMBDA_SUPPORT_SPEC(i, name, params) /**/ //在这个编译器,没有实际的意义。 # define BOOST_MPL_AUX_LAMBDA_SUPPORT(i,name,params) /**/ template< typename BOOST_MPL_AUX_NA_PARAM(T)//typename T = na > struct next { //long_的下一个,比如long_<2>,下一个就是long_<3> typedef typename T::next type; BOOST_MPL_AUX_LAMBDA_SUPPORT(1,next,(T)) }; template< typename BOOST_MPL_AUX_NA_PARAM(T) > struct prior { //long_的上一个,比如现在是long_<2>,上一个就是long_<1> typedef typename T::prior type; BOOST_MPL_AUX_LAMBDA_SUPPORT(1,prior,(T)) //在GCC编译器下,没有实际的意义。 }; template< typename T , typename Base , int at_front = 0 > //继承模板参数Base,实际上这个类型无论是什么都是递归的包含所有的类型。 struct v_item //从下面可以看到,模板的特化将一直进行。 : Base { //long_<32768> typedef typename Base::upper_bound_ index_;//如果是初始值,就是long_<32768> typedef typename next<index_>::type upper_bound_; //next,就是下一个值,long_<32769> typedef typename next<typename Base::size>::type size;//如果是初始值就是long_<0> typedef Base base; typedef v_item type; // agurt 10/sep/04: MWCW <= 9.3 workaround here and below; the compiler // breaks if using declaration comes _before_ the new overload static aux::type_wrapper<T> item_(index_); using Base::item_; //让Base类的item_()函数和这个类里面的函数重载 }; template< typename T , typename Base > //这是一个特化版本。 struct v_item<T,Base,1> : Base { typedef typename prior<typename Base::lower_bound_>::type index_; typedef index_ lower_bound_; typedef typename next<typename Base::size>::type size; typedef Base base; typedef v_item type; static aux::type_wrapper<T> item_(index_); using Base::item_; }; //下面这个类是经过宏推到出来的,再以后的文章再讨论这个问题。 template< long N> struct long_ { enum { value = N }; typedef long type; typedef long_ value_type; typedef integral_c_tag tag; //这是一个简化版本,实际上是:typedef long_<static_cast<long>(vaule+1)> next typedef long_<value+1> next; //typedef long_<static_cast<long>(vaule-1)> prior; typedef long_<value-1> prior; // enables uniform function call syntax for families of overloaded // functions that return objects of both arithmetic ('int', 'long', // 'double', etc.) and wrapped integral types (for an example, see // "mpl/example/power.cpp") operator long() const { return static_cast<long>(this->value); } }; //主模版 template< typename Dummy = na > struct vector0; //特化版本. template<> struct vector0<na> { //GCC支持,GCC版本,MSVC并不支持这个版本 #if defined(BOOST_MPL_CFG_TYPEOF_BASED_SEQUENCES) typedef aux::vector_tag tag; typedef vector0 type; typedef long_<32768> lower_bound_; typedef lower_bound_ upper_bound_; typedef long_<0> size; static aux::type_wrapper<void_> item_(...); #else typedef aux::vector_tag<0> tag; typedef vector0 type; typedef void_ item0; typedef v_iter<vector0<>,0> begin; typedef v_iter<vector0<>,0> end; #endif }; template< typename T0 > struct vector1 : v_item< T0 //这个参数实际上就是将要使用的参数 , vector0< > //vector0<>实际上就是vector0<na> > { typedef vector1 type; }; template< typename T0, typename T1 > struct vector2 : v_item< T1 //存放的第二个参数 , vector1<T0> //T0为第一个参数,现在存放在vector1中 > { typedef vector2 type; }; template< typename T0, typename T1, typename T2 > struct vector3 : v_item< T2 , vector2< T0,T1 > //前两个参数存放在vector2这个序列中 > { typedef vector3 type; }; //假设现在存放int, double, float三个元素于vector3中,现在模拟编译器的推导过程: typedef vector3<int,double,float> s3; //=========> struct vector3 : v_item<float, vector2<int, double>>{ typedef vector3 type; } //=========> //构造vector3,先要构造其基类,这儿的基类是v_item,因此先要构造v_item //v_item 有一个特化版本,但是这儿显然是一个默认参数,所以不会调用特化版本。 struct v_item<float, vector2<int,double>>: vector2<int,double> { typedef typename vector2<int,double>::upper_bound_ index_; //long_<32770> typedef typename next<index_>::type upper_bound_; //long_<32771> typedef typename next<typename vector2<int,double>::size>::type size; //long_<3> typedef vector2<int,double> base; typedef v_item type; // agurt 10/sep/04: MWCW <= 9.3 workaround here and below; the compiler // breaks if using declaration comes _before_ the new overload //aux::type_wrapper<float>返回的类型为float //index_ 是重载参数,所以如果按照索引来访问,就不会出先偏差。 static aux::type_wrapper<float> item_(index_); using Base::item_; }; //=========> //这儿仍然要构造基类 vector2<int,double>,从上面的定义可以得出: struct vector2 : v_item<double, vector1<int> >{ typedef vector2 type; } //=========> //继续构造基类:v_item struct v_item<double, vector1<int> >: vector1<int> { typedef typename vector1<int>::upper_bound_ index_; //long_<32769> typedef typename next<index_>::type upper_bound_; //long_<32770> typedef typename next<typename vector1<int>::size>::type size; //long_<2> typedef vector1<int> base; typedef v_item type; // agurt 10/sep/04: MWCW <= 9.3 workaround here and below; the compiler // breaks if using declaration comes _before_ the new overload static aux::type_wrapper<double> item_(index_); using Base::item_; }; //===========> //继续要对vector1进行特化: struct vector1 : v_item<int, vector0<> > { typedef vector1 type; } //============> //还需要构造v_item struct v_item<int, vector0<> >: vector0<> { typedef typename vector0<>::upper_bound_ index_; //long_<32768> typedef typename next<index_>::type upper_bound_; //long_<32769> typedef typename next<typename vector<>::size>::type size; //long_<1> typedef vector0<> base; typedef v_item type; static aux::type_wrapper<int> item_(index_); using Base::item_; }; //vector0为: //============> template<> struct vector0<na> { typedef aux::vector_tag tag; typedef vector0 type; typedef long_<32768> lower_bound_; typedef lower_bound_ upper_bound_; typedef long_<0> size; static aux::type_wrapper<void_> item_(...); }; //这样编译器已经获得足够的信息,将返回。也就完成了特化过程。 //下面是at算法的代码。假设访问第0位元素。 template< typename BOOST_MPL_AUX_NA_PARAM(Sequence) //typename Sequence , typename BOOST_MPL_AUX_NA_PARAM(N) //typename N > struct at : at_impl< typename sequence_tag<Sequence>::type > //参数起标记作用 ::template apply< Sequence,N > //vector3<...>, long_<0> { BOOST_MPL_AUX_LAMBDA_SUPPORT(2,at,(Sequence,N))// }; //上面的at继承了at_impl //按照对象的构造原理,先构造积累,然后才是派生类。这样先要构造下面的at_impl //的特化版本 template<> struct at_impl< aux::vector_tag > { template< typename Vector, typename N > struct apply : v_at< Vector //vector3<...> , BOOST_MPL_AUX_VALUE_WKND(N)::value //N::vaule long_<0>::vaule 的值为0 > { }; }; //还要构造v_at这个基类 template< typename Vector, long n_ > struct v_at : aux::wrapped_type< typename v_at_impl<Vector,n_>::type > { }; template< typename Vector, long n_ > struct v_at_impl { //Vector::lower_bound_::vaule 的值为:32768 ,n 为0 typedef long_< (Vector::lower_bound_::value + n_) > index_; //这样long_<32768> typedef __typeof__( Vector::item_(index_()) ) type;//从上面可以看出: }; // typedef __typeof__( Vector::item_(long_<32768> ) ) type; 因此最佳匹配就能求出int //下面是vector n_c.hpp 代码,大同小异。 //下面是宏推导出来的,int_,long_皆是利用这段宏代码(宏代码稍后) template< typename T, T N> struct integral_c { enum{ value = N }; typedef integral_c type; typedef T value_type; typedef integral_c_tag tag; typedef integral_c<T, static_cast<T>(value + 1)> next; typedef integral_c<T, static_cast<T>(value - 1)> prior; // enables uniform function call syntax for families of overloaded // functions that return objects of both arithmetic ('int', 'long', // 'double', etc.) and wrapped integral types (for an example, see // "mpl/example/power.cpp") operator T() const { return static_cast<T>(this->value); } }; //下面就是这段宏代码: //在其他文件的宏定义,移到这儿来便于理解。 template< typename T, T N > struct integral_c; //这是在别处声明 #define AUX_WRAPPER_NAME integral_c #define AUX_WRAPPER_VALUE_TYPE T #define AUX_WRAPPER_INST(value) AUX_WRAPPER_NAME< T, value > # define BOOST_MPL_AUX_STATIC_CAST(T, expr) static_cast<T>(expr) # define AUX_WRAPPER_PARAMS(N) typename T, T N struct integral_c_tag { enum { value = 0 }; }; //这个宏定义模板可以生成long_,int_, integral_c等 template< AUX_WRAPPER_PARAMS(N) > struct AUX_WRAPPER_NAME //就是靠这个不同的宏替换 { //有两种版本,但实现效果没什么差别,一个是用static, 另外一个用 enum. BOOST_STATIC_CONSTANT(AUX_WRAPPER_VALUE_TYPE, value = N); // agurt, 08/mar/03: SGI MIPSpro C++ workaround, have to #ifdef because some // other compilers (e.g. MSVC) are not particulary happy about it #if BOOST_WORKAROUND(__EDG_VERSION__, <= 238) //这是EDG编译器,这儿不予考虑 typedef struct AUX_WRAPPER_NAME type; #else typedef AUX_WRAPPER_NAME type; #endif typedef AUX_WRAPPER_VALUE_TYPE value_type; //根据上面的宏定义,可以很轻松的推到 typedef integral_c_tag tag; // have to #ifdef here: some compilers don't like the 'N + 1' form (MSVC), // while some other don't like 'value + 1' (Borland), and some don't like // either #if BOOST_WORKAROUND(__EDG_VERSION__, <= 243)//这是EDG编译器,这儿不予考虑 private: BOOST_STATIC_CONSTANT(AUX_WRAPPER_VALUE_TYPE, next_value = BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (N + 1))); BOOST_STATIC_CONSTANT(AUX_WRAPPER_VALUE_TYPE, prior_value = BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (N - 1))); public: typedef AUX_WRAPPER_INST(next_value) next; typedef AUX_WRAPPER_INST(prior_value) prior; //其它编译器版本,咱不考虑这些 #elif BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x561)) / || BOOST_WORKAROUND(__IBMCPP__, BOOST_TESTED_AT(502)) / || (BOOST_WORKAROUND(__HP_aCC, <= 53800) && (BOOST_WORKAROUND(__hpxstd98, != 1))) typedef AUX_WRAPPER_INST( BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (N + 1)) ) next; typedef AUX_WRAPPER_INST( BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (N - 1)) ) prior; #else //下面是我们需要的版本,详情见上面已经推导出来的结果。 typedef AUX_WRAPPER_INST( BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (value + 1)) ) next; typedef AUX_WRAPPER_INST( BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (value - 1)) ) prior; #endif // enables uniform function call syntax for families of overloaded // functions that return objects of both arithmetic ('int', 'long', // 'double', etc.) and wrapped integral types (for an example, see // "mpl/example/power.cpp") operator AUX_WRAPPER_VALUE_TYPE() const { return static_cast<AUX_WRAPPER_VALUE_TYPE>(this->value); } }; //下面是实现vector_c<int,1,3,6,3> 类型的代码,同上面很相似,所以顺带提一下: //不同之处在于其封装方式不同, //也就是传入的参数是经过类的封装以后的代码: intergal_c template< typename T , T C0 > struct vector1_c : vector1< integral_c< T,C0 > >//利用了vector1 的封装 { typedef vector1_c type; typedef T value_type; }; template< typename T , T C0, T C1 > struct vector2_c //这些仅仅是封装方式不同,同以前的意思差不多。 : vector2< integral_c< T,C0 >, integral_c< T,C1 > > { typedef vector2_c type; typedef T value_type; }; template< typename T , T C0, T C1, T C2 > struct vector3_c : vector3< integral_c< T,C0 >, integral_c< T,C1 >, integral_c< T,C2 > > { typedef vector3_c type; typedef T value_type; }; //GCC版本的实现: template< typename T > struct vector0_c : vector0<> { typedef vector0_c type; typedef T value_type; }; template< typename T , T C0 > struct vector1_c : v_item< integral_c< T,C0 >//T , vector0_c<T> //base > { typedef vector1_c type; typedef T value_type; }; template< typename T , T C0, T C1 > struct vector2_c : v_item< integral_c< T,C1 > , vector1_c< T,C0 > > { typedef vector2_c type; typedef T value_type; }; //模板的重用性在这儿体现出来了,只要封装得好的代码, //以其他方式也很容易使用。 template< typename T , T C0, T C1, T C2 > struct vector3_c : v_item< integral_c< T,C2 > , vector2_c< T,C0,C1 > > { typedef vector3_c type; typedef T value_type; };

 

你可能感兴趣的:(c,vector,struct,gcc,wrapper,编译器)