什么是一个很好的参考文档使用的是X-宏的模式在C(或者可能是C ++)? [英] What is a good reference documenting patterns of use of X-Macros in C (or possibly C++)?

查看:141
本文介绍了什么是一个很好的参考文档使用的是X-宏的模式在C(或者可能是C ++)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个基本定义和示例和的X宏数引用中对C pre处理器这维基百科条目指定:

A basic definition and example and a few references for "X-Macros" is given in this wikipedia entry on the C pre-processor:

的X-Macro是头文件(通常
  使用.DEF扩展名,而不是
  包含传统的.H)
  类似的宏话费清单(可
  被称为组分宏)。

An X-Macro is a header file (commonly using a ".def" extension instead of the traditional ".h") that contains a list of similar macro calls (which can be referred to as "component macros").

什么是信息如何使用这个强大的技术,一些好的资源?
是否有知名的使用这种方法的开源库?

What are some good sources of information on how to use this powerful technique? Are there well-known open source libraries using this method?

推荐答案

我code大量使用的是X宏()。该值只来自添加新的数据只对X名单,不修改任何其它code。

I use X Macros() in code a lot. The value comes from only adding new data only to the "X list" and not modifying any other code.

最常见使用的是X宏()是错误的文字,错误codeS关联。当新的错误codeS的加入,程序员必须记住添加code和文字,一般在不同的地方。在X宏允许在一个地方要添加新的错误数据,并随时随地自动填充它是必要的。

The most common use of X Macros() is for associating error text with error codes. When new error codes are added, programmers must remember to add the code and the text, typically in separate places. The X Macro allows the new error data to be added in a single place and get automatically populated anywhere it is needed.

不幸的是,机制使用了大量的pre-编译器法宝,可以使code有点难以阅读(如字符串 ## TOKEN1 token2 ,字符串创建与 #token )。正因为如此我通常解释在X宏在评论做。

Unfortunately, the mechanisms use a lot of pre-compiler magic that can make the code somewhat hard to read (e.g. string joining with token1##token2, string creation with #token). Because of this I typically explain what the X Macro is doing in the comments.

下面是使用误差/返回值的例子。所有新数据被添加到 X_ERROR 名单。其他code竟没有被修改。

Here is an example using the error/return values. All new data gets added to the "X_ERROR" list. None of the other code hast to be modified.

/* 
 * X Macro() data list
 * Format: Enum, Value, Text
 */
#define X_ERROR \
  X(ERROR_NONE,   1, "Success") \
  X(ERROR_SYNTAX, 5, "Invalid syntax") \
  X(ERROR_RANGE,  8, "Out of range")

/* 
 * Build an array of error return values
 *   e.g. {0,5,8}
 */
static int ErrorVal[] =
{
  #define X(Enum,Val,Text)     Val,
   X_ERROR
  #undef X
};

/* 
 * Build an array of error enum names
 *   e.g. {"ERROR_NONE","ERROR_SYNTAX","ERROR_RANGE"}
 */

static char * ErrorEnum[] = {
  #define X(Enum,Val,Text)     #Enum,
   X_ERROR
  #undef X
};

/* 
 * Build an array of error strings
 *   e.g. {"Success","Invalid syntax","Out of range"}
 */
static char * ErrorText[] = {
  #define X(Enum,Val,Text)     Text,
   X_ERROR
  #undef X
};

/* 
 * Create an enumerated list of error indexes
 *   e.g. 0,1,2
 */
enum {
  #define X(Enum,Val,Text)     IDX_##Enum,
   X_ERROR
  #undef X
  IDX_MAX   /* Array size */
};

void showErrorInfo(void)
{
    int i;

    /* 
     * Access the values
     */
    for (i=0; i<IDX_MAX; i++)
        printf(" %s == %d [%s]\n", ErrorEnum[i], ErrorVal[i], ErrorText[i]);

}

您还可以使用的是X宏()生成code。对于测试例如,如果一个误差值已知,在X宏可以生成一个switch语句的情况:

You can also use X Macros() to generate code. For example to test if an error value is "known", the X Macro can generate cases in a switch statement:

 /*
  * Test validity of an error value
  *      case ERROR_SUCCESS:
  *      case ERROR_SYNTAX:
  *      case ERROR_RANGE:
  */

  switch(value)
  {

  #define X(Enum,Val,Text)     case Val:
   X_ERROR
  #undef X
         printf("Error %d is ok\n",value);
         break;
      default:
         printf("Invalid error: %d\n",value);
         break;
  }

这篇关于什么是一个很好的参考文档使用的是X-宏的模式在C(或者可能是C ++)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆