注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

为着理想勇敢前进

 
 
 

日志

 
 

Variable Preprocess Metaprogramming?  

2007-04-14 14:27:09|  分类: 默认分类 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
As we known, there is a preprocess metaprogramming library in boost, but it is not flexible enough. we are unable to define an custom preprocess loop(something like #for or #while ?), and we are also unable to use recursion.

I have an idea to do that, C/C++ allows recursion of #include, so we can put the codes we want to evaluate repeatedly to a head, and include itself depends on a preprocess condition.

But if we wrote code below, we would meet a error of macro redefinition.
/**** foo.hpp ****/
#if FOO < 10
// do something with I
#    define FOO FOO + 1
#    include "foo.hpp"
#endif
/**** foo.hpp ****/


/**** bar.hpp ****/
#define FOO 0
#include "foo.hpp"
/**** bar.hpp ****/



The problem is that an macro must not depends on itself. Thus, to change a preprocess variant, I use a bit of #if to transfer value of a macro to some temporary macro. see my code below:

/****  begin_set_value.hpp  ****/
#if (BOOST_VPP_VALUE & (1 << 0)) == 0
#define BOOST_VPP_TEMPORARY_BIT_0 0
#else
#define BOOST_VPP_TEMPORARY_BIT_0 1
#endif
#if (BOOST_VPP_VALUE & (1 << 1)) == 0
#define BOOST_VPP_TEMPORARY_BIT_1 0
#else
#define BOOST_VPP_TEMPORARY_BIT_1 1
#endif
/* ...  */
#if (BOOST_VPP_VALUE & (1 << 30)) == 0
#define BOOST_VPP_TEMPORARY_BIT_30 0
#else
#define BOOST_VPP_TEMPORARY_BIT_30 1
#endif
#if (BOOST_VPP_VALUE & (1 << 31)) == 0
#define BOOST_VPP_TEMPORARY_BIT_31 0
#else
#define BOOST_VPP_TEMPORARY_BIT_31 1
#endif

#undef BOOST_VPP_VALUE

#define BOOST_VPP_VALUE \
    (BOOST_VPP_TEMPORARY_BIT_0 << 0 ) \
    + (BOOST_VPP_TEMPORARY_BIT_1 << 1 ) \
    + (BOOST_VPP_TEMPORARY_BIT_2 << 2 ) \
    + (BOOST_VPP_TEMPORARY_BIT_3 << 3 ) \
    /* ...  */ \
    + (BOOST_VPP_TEMPORARY_BIT_29 << 29 ) \
    + (BOOST_VPP_TEMPORARY_BIT_30 << 30 ) \
    + (BOOST_VPP_TEMPORARY_BIT_31 << 31 ) \
    /*BOOST_VPP_VALUE*/
/****  begin_set_value.hpp  ****/

Now, the BOOST_VPP_VALUE is depends on some of BOOST_VPP_TEMPORARY_BIT_n, and can be used to change the former macro:
#define FOO 0
#define BOOST_VPP_VALUE FOO
#include "begin_set_value.hpp"
#undef FOO
#define FOO (BOOST_VPP_VALUE + 1)

But, FOO depends on BOOST_VPP_TEMPORARY_BIT_ns, and can not be change again, the solution is that A must a variant depends on some other BIT_n definition instead of directly depends on BOOST_VPP_VALUE. Actually, I implements an variant stack to allocate and release these variant.

I think the concept of variable preprocess is useful, if Boost.Typeof or Boost.Bind use that, these code they need would be less than copying same codes.

I have upload a sample on vault, file name is variable_preprocess.zip.
http://boost-consulting.com/vault/index.php?&direction=0&order=&directory=Preprocessor%20Metaprogramming
http://boost-consulting.com/vault/index.php?action=downloadfile&filename=variable_preprocess.zip&directory=Preprocessor%20Metaprogramming&

All code is validated by wave, thank you for your attention.


  评论这张
 
阅读(254)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018