用前缀生成宏的宏函数(避免字符串化) [英] Macro-function to generate macro with prefix (avoid stringification)

查看:162
本文介绍了用前缀生成宏的宏函数(避免字符串化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景



我有一个项目,其中有两个独立的产品,它们具有几乎相同的宏名称,为此我想创建一个类似宏的函数快速检索宏的值。我写了一个 getTranslation 宏函数来提供给函数的文本文本,它应该被当作一个字符串和一个字符串前缀(如下所示)。






问题



如何完成这项操作提供给宏的参数,将它们连接在一起(在中间用下划线),并将该结果作为预处理宏而不是字符串处理?




代码清单



  / **************** ************************************************** ************* 
* coconut.h
************************* ************************************************** *** /
#define COCONUT(PRODUCT_COCONUT)
#define COCONUT_FX_REGISTER(100)
#define COCONUT_BASE_REGISTER(101)

/ ******* ************************************************** **********************
* pineapple.h
**************** ************ ************************************************** /
#define PINEAPPLE(PRODUCT_PINEAPPLE)
#define PINEAPPLE_FX_REGISTER(200)
#define PINEAPPLE_BASE_REGISTER(201)

/ ********** ************************************************** *******************
* test.c.
********************************************** ******************************** /
#include< stdio.h>
#includetranslation.h
#includecoconut.h

int main(void){
$ b $ int i = getTranslation(FX_REGISTER , 椰子);
printf(翻译:%d.\\\
,i);

返回0;
}

/ ************************************ *******************************************
*翻译。 h
********************************************* ********************************* /
#define FX_REGISTER(0)
#define BASE_REGISTER(1)

#define getTranslationInternal(x,y)#y_#x
#define getTranslation(x,y)getTranslationInternal(x,y)

enum Products {
PRODUCT_COCONUT = 0,
PRODUCT_PINEAPPLE,
PRODUCT_MAX,
PRODUCT_INVALID =(-1)
};






编译器警告



  test.c:在函数'main'中:
test.c:10:45:warning: Wint转换]
int i = getTranslation(FX_REGISTER,COCONUT);
^
translation.h:7:39:note:定义宏'getTranslationInternal'
#define getTranslationInternal(x,y)#y_#x
^
test.c:10:10:注意:在扩展宏'getTranslation'
int i = getTranslation(FX_REGISTER,COCONUT);






运行样本



 翻译:4195812。 


解决方案

  #define getTranslationInternal(x,y)y ## _ ## x 

在叮当时为我工作,如果你放弃宏定义的括号。


Background

I have a project where I have two separate products with near-identical macro names, for which I would like to create a macro-like-function to retrieve the values of the macros quickly. I have written a getTranslation macro-function to take the literal text provided to the "function", which should be treated as a string and a string prefix (shown below).


Question

How can I accomplish this operation of taking the arguments supplied to the macro, concatenating them together (with an underscore in the middle), and treating that result as a preprocessor macro instead of a string?


Code Listing

/*******************************************************************************
 * coconut.h
 ******************************************************************************/
#define COCONUT         (PRODUCT_COCONUT)
#define COCONUT_FX_REGISTER (100)
#define COCONUT_BASE_REGISTER   (101)

/*******************************************************************************
 * pineapple.h
 ******************************************************************************/
#define PINEAPPLE       (PRODUCT_PINEAPPLE)
#define PINEAPPLE_FX_REGISTER   (200)
#define PINEAPPLE_BASE_REGISTER (201)

/*******************************************************************************
 * test.c.
 ******************************************************************************/
#include <stdio.h>
#include "translation.h"
#include "coconut.h"

int main(void) {

    int i = getTranslation(FX_REGISTER, COCONUT);
    printf("Translation:%d.\n", i);

    return 0;
}

/*******************************************************************************
 * translation.h
 ******************************************************************************/
#define FX_REGISTER     (0)
#define BASE_REGISTER       (1)

#define getTranslationInternal(x, y)    #y "_" #x
#define getTranslation(x, y)        getTranslationInternal(x, y)

enum Products {
    PRODUCT_COCONUT = 0,
    PRODUCT_PINEAPPLE,
    PRODUCT_MAX,
    PRODUCT_INVALID = (-1)
};


Compiler Warnings

test.c: In function ‘main’:
test.c:10:45: warning: initialization makes integer from pointer without a cast [-Wint-conversion]
  int i = getTranslation(FX_REGISTER, COCONUT);
                                             ^
translation.h:7:39: note: in definition of macro ‘getTranslationInternal’
 #define getTranslationInternal(x, y) #y "_" #x
                                       ^
test.c:10:10: note: in expansion of macro ‘getTranslation’
  int i = getTranslation(FX_REGISTER, COCONUT);


Sample Run

Translation:4195812.

解决方案

#define getTranslationInternal(x, y)    y ## _ ## x

worked for me on clang, if you drop parentheses around the macro definitions.

这篇关于用前缀生成宏的宏函数(避免字符串化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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