繁体   English   中英

迭代可变参数宏 Arguments

[英]Iterating Variadic Macro Arguments

我正在以编程方式生成一堆仿函数,为了使生成的代码更具可读性,我试图提出一个宏,它将扩展以下行,

SET_STATE(FunctorA,a,b);

ref a;
ref b;
FunctorA(ref a, ref b){
   this->a = a;
   this->b = b;
}

基本上它将扩展到第一个 arguments 构造函数。 可变参数部分是 arguments 的数量给构造函数。 是否可以在宏内部循环并在预处理期间生成此代码,即使它对于这种特殊情况没有意义,但我有一些具有 20 个左右变量的仿函数,它们可以访问它会清理我生成的代码很多。

所有 arguments 都是同一类型,只是名称不同。

如果boost::preprocessorSEQ表示( (a)(b)... ) 是允许的,那么下面的代码可能会达到目的:

#include <boost/preprocessor/seq/for_each.hpp>
#include <boost/preprocessor/seq/for_each_i.hpp>
#include <boost/preprocessor/punctuation/comma_if.hpp>

#define DEF_MEMBER( r, data, elem ) ref elem;
#define DEF_PARAM( r, data, i, elem ) BOOST_PP_COMMA_IF( i ) ref elem
#define DEF_ASSIGN( r, data, elem ) this->elem = elem;

#define SET_STATE( f, members )                         \
  BOOST_PP_SEQ_FOR_EACH( DEF_MEMBER,, members )         \
  f( BOOST_PP_SEQ_FOR_EACH_I( DEF_PARAM,, members ) ) { \
      BOOST_PP_SEQ_FOR_EACH( DEF_ASSIGN,, members )     \
  }

SET_STATE(FunctorA,(a)(b))

上面的代码扩展为

ref a; ref b; FunctorA( ref a , ref b ) { this->a = a; this->b = b; }

在我的环境中。

使用此链接http://cplusplus.co.il/2010/07/17/variadic-macro-to-count-number-of-arguments/中找到的欺骗来计算 arguments 的数量并使用一些非常丑陋的宏 I可以生成你想要的output。

我使用gcc (gcc -E test.cpp) 对其进行了测试,它可以工作,它不是便携式的

代码:

#define VA_NARGS_IMPL(_1,_2,_3,_4,_5,_6,_7,_8,N,...) N
#define VA_NARGS(...) VA_NARGS_IMPL(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1)
#define SET_STATEGENERATE(name, count, ...)             \
        dec ## count (__VA_ARGS__)                      \
        name(ref ## count (__VA_ARGS__)) {              \
            con ## count (__VA_ARGS__)                  \
        }
#define SET_STATEP(name, count, ...) SET_STATEGENERATE(name, count, __VA_ARGS__) 
#define SET_STATE(name, ...) SET_STATEP(name, VA_NARGS(__VA_ARGS__), __VA_ARGS__)
/* args */
#define dec1(a) ref a;
#define dec2(a,b) dec1(a) ref b;
#define dec3(a,b,c) dec2(a, b) ref c;
#define dec4(a,b,c,d) dec3(a,b,c) ref d;
#define dec5(a,b,c,d,e) dec4(a,b,c,d) ref e;
#define dec6(a,b,c,d,e,f) dec5(a,b,c,d,e) ref f;
#define dec7(a,b,c,d,e,f,g) dec6(a,b,c,d,e,f)ref g;
#define dec8(a,b,c,d,e,f,g,h) dec7(a,b,c,d,e,f,g) ref h;
#define ref1(a) ref a
#define ref2(a,b) ref1(a), ref b
#define ref3(a,b,c) ref2(a,b), ref c
#define ref4(a,b,c,d) ref3(a,b,c), ref d
#define ref5(a,b,c,d,e) ref4(a,b,c,d), ref e
#define ref6(a,b,c,d,e,f) ref5(a,b,c,d,e), ref f
#define ref7(a,b,c,d,e,f,g) ref6(a,b,c,d,e,f), ref g
#define ref8(a,b,c,d,e,f,g, h) ref7(a,b,c,d,e,f,g), ref h
#define con1(a) this->a = a;
#define con2(a,b) con1(a) this->b = b;
#define con3(a,b,c) con2(a,b) this->c = c;
#define con4(a,b,c,d) con3(a,b,c) this->d = d;
#define con5(a,b,c,d,e) con4(a,b,c,d) this->e = e;
#define con6(a,b,c,d,e,f) con5(a,b,c,d,e) this->f = f;
#define con7(a,b,c,d,e,f,g) con6(a,b,c,d,e,f) this->g = g;
#define con8(a,b,c,d,e,f,g,h) con7(a,b,c,d,e,f,g) this->h = h;

所以以下内容:

/* 2 args */
SET_STATE(FunctorAA, foo, bar)
/* 3 args */
SET_STATE(FunctorBB, foo, bar, baz)
/* 4 args */    
SET_STATE(FunctorCC, foo, bar, baz, qux)

将产生:

ref foo; ref bar; FunctorAA(ref foo, ref bar) { this->foo = foo; this->bar = bar; }
ref foo; ref bar; ref baz; FunctorBB(ref foo, ref bar, ref baz) { this->foo = foo; this->bar = bar; this->baz = baz; }
ref foo; ref bar; ref baz; ref qux; FunctorCC(ref foo, ref bar, ref baz, ref qux) { this->foo = foo; this->bar = bar; this->baz = baz; this->qux = qux; }

注意:您可以按照明显的模式继续编号 arguments。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM