我有一堆#define,它们看起来都像这样#define D,0,它们包含A-D的字母和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
字母传递给宏时,匹配它们也很重要,因为有时我可以将图钉移到其他字母上。任何有关如何实现这一目标的建议都将受到欢迎!
最佳答案
除第一个字符外,不使用表示端口的字符。因此,我建议采用以下方式。
#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));