使用两个参数重载宏

时间:2014-09-05 20:39:10

标签: c macros

我有一堆 #defines ,看起来像这样 #define D,0 ,它们包含来自AD的一封信和一个0-8的数字,用逗号分隔。

现在我正在尝试创建一个宏,如下所示:Overloading Macro on Number of Arguments,但这适用于我的情况。此站点上的宏仅适用于带有一个参数的#define,我的有两个。

这就是我目前所拥有的:

#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define TEST_MACRO(...)     GET_MACRO(__VA_ARGS__, _TEST_MACRO_8, _TEST_MACRO_7, _TEST_MACRO_6, _TEST_MACRO_5, _TEST_MACRO_4, _TEST_MACRO_3, _TEST_MACRO_2, _TEST_MACRO_1) (__VA_ARGS__)

#define _TEST_MACRO_8(letter1, number1, letter2, number2, letter3, number3, letter4, number4, letter5, number5, letter6, number6, letter7, number7, letter8, number8)           (PORT##letter1 |= ((1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5) | (1 << number6) | (1 << number7) | (1 << number8))
#define _TEST_MACRO_7(letter1, number1, letter2, number2, letter3, number3, letter4, number4, letter5, number5, letter6, number6, letter7, number7)         (PORT##letter1 |= ((1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5) | (1 << number6) | (1 << number7))
#define _TEST_MACRO_6(letter1, number1, letter2, number2, letter3, number3, letter4, number4, letter5, number5, letter6, number6)           (PORT##letter1 |= ((1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5) | (1 << number6))
#define _TEST_MACRO_5(letter1, number1, letter2, number2, letter3, number3, letter4, number4, letter5, number5)         (PORT##letter1 |= ((1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5))
#define _TEST_MACRO_4(letter1, number1, letter2, number2, letter3, number3, letter4, number4)           (PORT##letter1 |= ((1 << number1) | (1 << number2) | (1 << number3) | (1 << number4))
#define _TEST_MACRO_3(letter1, number1, letter2, number2, letter3, number3)         (PORT##letter1 |= ((1 << number1) | (1 << number2) | (1 << number3))
#define _TEST_MACRO_2(letter1, number1, letter2, number2)           (PORT##letter1 |= ((1 << number1) | (1 << number2))
#define _TEST_MACRO_1(letter1, number1)         (PORT##letter1 |= (1 << number1))

如果我这样做:

#define ONE    D, 0
#define TWO    D, 1
#define THREE  D, 2

TEST_MACRO(ONE); //Error: macro _TEST_MACRO_2 requires 4 arguments but only 2 given
TEST_MACRO(ONE,TWO); //Error: macro _TEST_MACRO_4 requires 8 arguments but only 4 given
TEST_MACRO(ONE,TWO,THREE); //Error: macro _TEST_MACRO_6 requires 12 arguments but only 6 given

这是什么问题?我该如何解决?

祝你好运!

修改

为了澄清一下,这是将要使用它的背景。我正在编程一个8位AVR微控制器。并且知道我有这样定义的引脚:#define PIN1 A,0,其中 A 代表PIN码, 0 代表PIN码。

如果我以这种方式定义了一大堆,当我想要更改它们的某些设置时,我必须逐个手动执行,如下所示:PIN_HIGH(PIN1);PIN_HIGH(PIN2);PIN_HIGH(PIN3)以及更多它们,代码变得非常混乱。

所以我正在寻找一种方法来使用一个宏:PIN_HIGH(PIN1,PIN2,PIN3);。当我将它们传递给宏时,PIN1,PIN2,PIN3字母匹配也很重要,因为有时,我可以将引脚移动到其他地方。

有关如何实现这一目标的任何建议都非常受欢迎!

2 个答案:

答案 0 :(得分:1)

除第一个之外,不使用代表端口的字符。所以,我建议按照以下方式。

#define GET_MACRO(_1, _2, _3, _4, _5, _6, _7, _8, NAME, ...) NAME
#define TEST_MACRO(letter,...)  (PORT##letter |=  GET_MACRO(__VA_ARGS__, _TEST_MACRO_8, _TEST_MACRO_7, _TEST_MACRO_6, _TEST_MACRO_5, _TEST_MACRO_4, _TEST_MACRO_3, _TEST_MACRO_2, _TEST_MACRO_1) (__VA_ARGS__) )

#define _TEST_MACRO_8(number1, number2, number3,  number4,  number5,  number6,  number7, number8) \
    (1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5) | (1 << number6) | (1 << number7) | (1 << number8)
#define _TEST_MACRO_7(number1, number2, number3,  number4,  number5,  number6,  number7)\
    (1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5) | (1 << number6) | (1 << number7)
#define _TEST_MACRO_6(number1, number2, number3,  number4,  number5,  number6)\
    (1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5) | (1 << number6)
#define _TEST_MACRO_5(number1, number2, number3,  number4,  number5)\
    (1 << number1) | (1 << number2) | (1 << number3) | (1 << number4) | (1 << number5)
#define _TEST_MACRO_4(number1, number2, number3,  number4)\
    (1 << number1) | (1 << number2) | (1 << number3) | (1 << number4)
#define _TEST_MACRO_3(number1, number2, number3)\
    (1 << number1) | (1 << number2) | (1 << number3)
#define _TEST_MACRO_2(number1, number2)\
    (1 << number1) | (1 << number2)
#define _TEST_MACRO_1(number1)\
    (1 << number1)


TEST_MACRO(D, 1);
TEST_MACRO(D, 0, 1);
TEST_MACRO(D, 0, 1, 2);

#define ONE    (D, 0)
#define TWO    (D, 1)
#define THREE  (D, 2)
#define CAR(a,b) a
#define CDR(a,b) b
#define F(x) CAR x
#define R(x) CDR x 
#define TEST_MACRO_WRAP(...) TEST_MACRO(__VA_ARGS__)
TEST_MACRO_WRAP(F(ONE), R(ONE));
TEST_MACRO_WRAP(F(ONE), R(ONE), R(TWO));
TEST_MACRO_WRAP(F(ONE), R(ONE), R(TWO), R(THREE));

答案 1 :(得分:0)

ONETWOTHREE都会在展开TEST_MACRO之前立即展开,所以当您说TEST_MACRO(ONE)时,__VA_ARGS__宏arg设置为D, 0ONE的扩展),这意味着你调用_TEST_MACRO_2,因为这是两个参数。

由于您总是需要偶数个参数,因此可以将TEST_MACRO定义为:

#define TEST_MACRO(...)     GET_MACRO(__VA_ARGS__, _TEST_MACRO_4, ERROR, _TEST_MACRO_3, ERROR, _TEST_MACRO_2, ERROR, _TEST_MACRO_1, ERROR) (__VA_ARGS__)
#define ERROR(...)    #error "Odd number of arguments to TEST_MACRO"