什么是记录在 C(或可能是 C++)中使用 X-Macros 模式的良好参考? [英] What is a good reference documenting patterns of use of X-Macros in C (or possibly C++)?

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

问题描述

X-Macros" 在此C 预处理器上的维基百科条目中给出:

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?

推荐答案

我在代码中经常使用 X Macros().该值来自仅将新数据添加到X列表"而不修改任何其他代码.

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 Macros() 最常见的用途是将错误文本与错误代码相关联.当添加新的错误代码时,程序员必须记住添加代码和文本,通常在不同的地方.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.

不幸的是,这些机制使用了很多预编译器的魔法,这会使代码有些难以阅读(例如使用 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"列表中.其他代码无需修改.

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]
", ErrorEnum[i], ErrorVal[i], ErrorText[i]);

}

您也可以使用 X Macros() 来生成代码.例如,要测试错误值是否已知",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
",value);
         break;
      default:
         printf("Invalid error: %d
",value);
         break;
  }

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

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