Warning
-
本文由熊春雷所写,绝对保证原创,在此特别严肃声明。本人简介:熊春 雷,男,1980年出生于湖北钟祥;七岁随父迁往宜昌开始学生生涯,小学和初中在 湖北宜昌樟村坪镇职工子弟学校就读;1996年考上宜昌县高中,开始三年的高中生 活;1999-2003就读于湖北大学物理系;2003-2006就读于武汉大学物理系。现就职 于盛大网络:)
-
绝对不能容忍他人说本文为他所写以及其他的侵权行为。一旦发现,一定 尽本人最大的能力以法律的形式严追到底,决不妥协。
-
引用本文,要保证本文的完整性,不可以删除此处的声明,并且务必注明出处。
Tip
-
本文编写的所有代码可以用于任何用途(包括商业用途)。
-
用于商业用途的需要在最后发布的软件中声明借鉴了本文的思想。具体事 宜可以协商解决,(代码决不收取任何费用)。
-
其他事项可以和我联系,包括技术讨论等等:)或者直接登陆网站论坛: http://www.autodev.net
Note
-
本文受到了《C++设计新思维》和《产生式编程》两本书的影响,同时也查阅了大 量的资料,从Loki库和Boost库中也吸收了不少营养,特此感谢之。
-
本文由于处于原创阶段,难免会出现各种各样的错误。代码出现错误的可能性非常 小(本来想说为零的),因为文档和代码是严格同步的,这是由VST文本的include 所保证的,代码都是测试成功之后才发布的。
-
本文所编写的代码,经过了VC2005编译器和g++编译器的测试,并且都通过了。
-
本文还没有彻底完成,算是一个初级版本,未来还将继续完善。暂时发布出来是为 了预知读者群有多少,读者越多,我的成就感越强,写作的时候也会更有动力:)
-
本文还会继续完善,欢迎各位读者的批评指正,也接受各种各样的建议,在权衡之 后以决定是否加入本书。
-
本书还没有最终完成,还会不断的进行完善,更新之后的内容将会发表于我的 网站或我的博客。所以还需要读者多多关心本文的进展:)
在分析C++代码的时候,特别是分析C++函数(仿函数)的时候,若是能够分析出在一个名 字空间(类域)里面的指定函数名称的函数的参数的数量将会是非常有用的:)这一章就 对这个问题进行详细的分析。
实现的基本原理如下:
-
假定一个比较大的函数参数数量,例如5个
-
从0开始递增的探测所有的函数参数数量
-
利用函数参数能够实现隐式转型,将any类型转换成指定的类型
下面是名字空间内的函数参数数量分析的实例:
namespace A { kind<9> on(...); int on(n<0>); int on(n<0>,n<1>,n<2>); int on(n<0>,n<1>,n<2>,n<3>); } static_assert<sizeof(kind<9>)==sizeof(A::on())>(); static_assert<sizeof(kind<9>)!=sizeof(A::on(any()))>(); static_assert<sizeof(kind<9>)==sizeof(A::on(any(),any()))>(); static_assert<sizeof(kind<9>)!=sizeof(A::on(any(),any(),any()))>(); static_assert<sizeof(kind<9>)!=sizeof(A::on(any(),any(),any(),any()))>(); static_assert<sizeof(kind<9>)==sizeof(A::on(any(),any(),any(),any(),any()))>();
下面是类域内的函数参数数量分析的实例:
struct A { kind<9> on(...); int on(n<0>); int on(n<0>,n<1>,n<2>); int on(n<0>,n<1>,n<2>,n<3>); }; static_assert<sizeof(kind<9>)==sizeof(A().on())>(); static_assert<sizeof(kind<9>)!=sizeof(A().on(any()))>(); static_assert<sizeof(kind<9>)==sizeof(A().on(any(),any()))>(); static_assert<sizeof(kind<9>)!=sizeof(A().on(any(),any(),any()))>(); static_assert<sizeof(kind<9>)!=sizeof(A().on(any(),any(),any(),any()))>(); static_assert<sizeof(kind<9>)==sizeof(A().on(any(),any(),any(),any(),any()))>();
从上面的代码可以看出:
-
any可以用来实现匹配任意类型,类似于正则表达式中的通配符“.”
-
函数重载和自动(隐式)转型
起到了极其重要的作用!
到此为止,已经成功的给出了函数参数数量探测的原理实现。在上面的代码中,大量出现 的static_assert可以看做是一个静态循环过程,现在的问题就是如何将这里的这些静态断 言的功能的原理实现 自动化 。关于这个问题的解决办法就要采用前面的章节中介绍 的代码产生功能了:)
从前面的原理实现可以看出,实际上就是一个静态循环过程,把这个静态循环过程用C++静 态代码封装一下就可以了。在封装之前需要知道结果是什么:
封装的结果就是函数参数数量的 数值类型列表(mkps<n<1>,n<3>,n<4> >) !前面的测 试用例里面有1个参数、3个参数和4个参数的函数,所以所得的结果就是n<1>,n<3>,n<4> 组成的类型串。
struct A { kind<9> on(...); int on(n<0>); int on(n<0>,n<1>,n<2>); int on(n<0>,n<1>,n<2>,n<3>); }; template<class T,class i>struct Filter; template<class T>struct Filter<T,n<0> > { enum{i = sizeof(T().on())!=sizeof(kind<9>)}; typedef n<i> type; }; template<class T>struct Filter<T,n<1> > { enum{i = sizeof(T().on(any()))!=sizeof(kind<9>)}; typedef n<i> type; }; template<class T>struct Filter<T,n<2> > { enum{i = sizeof(T().on(any(),any()))!=sizeof(kind<9>)}; typedef n<i> type; }; template<class T>struct Filter<T,n<3> > { enum{i = sizeof(T().on(any(),any(),any()))!=sizeof(kind<9>)}; typedef n<i> type; }; template<class T>struct Filter<T,n<4> > { enum{i = sizeof(T().on(any(),any(),any(),any()))!=sizeof(kind<9>)}; typedef n<i> type; }; template<class T>struct Filter<T,n<5> > { enum{i = sizeof(T().on(any(),any(),any(),any(),any()))!=sizeof(kind<9>)}; typedef n<i> type; };
下面就是上面的初步自动化的测试用例:
typedef filter<range<0,5+1>::type,A,Filter>::type RESULT; static_assert_same<mkps<n<1>,n<3>,n<4> >::type,RESULT>();
上面的代码就已经自动化实现了分析函数参数的数量的功能了,只不过函数的名称被定死 了(on)!
从上面的代码还可以看出自动化的实现需要:
-
给出一个比较大的函数参数数量(前面的函数参数数量最多是4个,所以给5就可以了)
-
针对于每一个函数参数数量,分别提供一个静态测量函数Filter
-
上面的被测试函数名称被限制成了on,这是不必要的,但是似乎只能通过宏的方式实现
通常来说,给出一个比较大的参数数量就可以了,一般情况下,给定十个参数就够用了:
#define AUTOCXX_DEFINE_FUNCTION(Func,Class) \ template<class Type,class Kind,class ArgPS>struct Class; \ template<class Type,class Kind> \ struct Class<Type,Kind,nil \ > \ { \ enum{i=(sizeof(Type().Func())!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ template<class Type,class Kind,class A0> \ struct Class<Type,Kind,p<A0> \ > \ { \ enum{i=(sizeof(Type().Func(A0()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1> \ struct Class<Type,Kind,p<A0,p<A1> \ > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2> \ struct Class<Type,Kind,p<A0,p<A1,p<A2> \ > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2,class A3> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3> \ > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2,class A3,class A4> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3,p<A4> \ > > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3(),A4()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2,class A3,class A4, \ class A5> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3,p<A4,p<A5> \ > > > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3(),A4(),A5()))!=sizeof(Kind))};\ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2,class A3,class A4, \ class A5,class A6> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3,p<A4,p<A5,p<A6> \ > > > > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3(),A4(),A5(), \ A6()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2,class A3,class A4, \ class A5,class A6,class A7> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3,p<A4,p<A5,p<A6,p<A7> \ > > > > > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3(),A4(),A5(), \ A6(),A7()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ \ template<class Type,class Kind,class A0,class A1,class A2,class A3,class A4, \ class A5,class A6,class A7,class A8> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3,p<A4,p<A5,p<A6,p<A7,p<A8> \ > > > > > > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3(),A4(),A5(), \ A6(),A7(),A8()))!=sizeof(Kind))}; \ typedef n<i> type; \ }; \ template<class Type,class Kind,class A0,class A1,class A2,class A3,class A4, \ class A5,class A6,class A7,class A8,class A9> \ struct Class<Type,Kind,p<A0,p<A1,p<A2,p<A3,p<A4,p<A5,p<A6,p<A7,p<A8,p<A9> \ > > > > > > > > > > \ { \ enum{i=(sizeof(Type().Func(A0(),A1(),A2(),A3(),A4(),A5(), \ A6(),A7(),A8(),A9()))!=sizeof(Kind))}; \ typedef n<i> type; \ };
从上面的代码中可以看出:由于需要替换函数名称,所以必须给出宏的实现,否则会比较 麻烦。虽然可以采用类似于BOOST里面的preprocessor库处理宏来简化这里的重复代码,但 为了简洁,暂时不采用(我还没有研究明白)。
下面给出封装的更好的自动化实现(函数参数数量最多为10个):
template<class Type,class Kind,template<class,class,class>class Function,class N=n<10> > class function_parameter_numbers { template<class T,class i>struct Filter { typedef typename repeat<any,i>::type ArgPS; typedef typename Function<T,Kind,ArgPS>::type type; }; typedef typename range<0,N::value>::type Range; public: typedef typename filter<Range,Type,Filter>::type type; };
下面就是上面的更进一步自动化的测试用例:
struct A { kind<9> on(...); int on(n<0>); int on(n<0>,n<1>,n<2>); int on(n<0>,n<1>,n<2>,n<3>); }; AUTOCXX_DEFINE_FUNCTION(on,ON); struct B { kind<9> func(...); int func(n<0>); int func(n<0>,n<1>,n<2>); int func(n<0>,n<1>,n<2>,n<3>); }; AUTOCXX_DEFINE_FUNCTION(func,FUNC); struct C { kind<9> operator()(...); int operator()(n<0>); int operator()(n<0>,n<1>,n<2>); int operator()(n<0>,n<1>,n<2>,n<3>); }; AUTOCXX_DEFINE_FUNCTION(operator(),FUNCTOR);
typedef function_parameter_numbers<A,kind<9>,ON>::type _A; static_assert_same<mkps<n<1>,n<3>,n<4> >::type,_A>(); typedef function_parameter_numbers<B,kind<9>,FUNC>::type _B; static_assert_same<mkps<n<1>,n<3>,n<4> >::type,_B>(); typedef function_parameter_numbers<C,kind<9>,FUNCTOR>::type _C; static_assert_same<mkps<n<1>,n<3>,n<4> >::type,_C>();
这里的之所以给出探测函数参数数量的实例,是因为它比较简单而且能够为即将研究的探 测函数参数的种类形成类似于XML的层级结构的功能做好准备:)
发表于 @ 2007年10月23日 21:52:00|评论(loading...)|编辑