[英]C++ namespace as a macro value
I have simple delegate functions in my C++ test code. 我的C ++测试代码中有简单的委托函数。 Since I cannot include the original implementation .cpp files(embedded ones), I use delegate .cpp file in the tests that are running on PC.
由于我无法包括原始实现.cpp文件(嵌入式文件),因此我在PC上运行的测试中使用委托.cpp文件。 My idea is to simply use the same macro as a body for the implementation, except the parentheses () and arguments which will supplied according to the signature.
我的想法是简单地使用与主体相同的宏作为实现,但括号()和参数将根据签名提供。
I tried something like: 我尝试了类似的东西:
void Flash::init()
{
DELEGATE_DESTINATION();
}
bool Flash::write(args)
{
DELEGATE_DESTINATION(args);
}
bool Flash::read(args)
{
DELEGATE_DESTINATION(args);
}
Where 哪里
void Flash::init()
bool Flash::write(args)
bool Flash::read(args)
Are identical to the ones in the embedded project implementation ones. 与嵌入式项目实现中的相同。 In the test files I simply relay the call to the fake implementations.
在测试文件中,我只是将调用中继到伪造的实现。 One possible solution would be to already include the signature and implementation in the fake classes not using relaying, I know.
我知道,一种可能的解决方案是已经在不使用中继的伪类中包括了签名和实现。
I am having hard time figuring out the macro. 我很难弄清楚宏。 I tried something like:
我尝试了类似的东西:
#define FAKE_PREFIX fakes::device::Flash::
#define DELEGATE_DESTINATION FAKE_PREFIX##__FUNCTION__
Which expands to FAKE_PREFIX__FUNCTION__ 扩展为FAKE_PREFIX__FUNCTION__
Well then after going through C Preprocessor, Stringify the result of a macro I can get only fakes expanding in place. 好了,经过C预处理程序后,将宏的结果字符串化,我只能得到伪造的扩展。
My goal would be to have a result like 我的目标是得到这样的结果
fakes::device::Flash::init
and so on for read and write . 等了读取和写入 。
What you want to do can be done far simpler. 您想要做的事情可以简单得多。 You don't need the full power of the pre-processor:
您不需要预处理器的全部功能:
You don't concatenate tokens ( ::
is not part of a valid token). 您不连接令牌(
::
不是有效令牌的一部分)。 You just need to print one macro next to another. 您只需要在另一个宏旁边打印一个宏即可。
_FUNCTION_
isn't a pre-processor macro, it's a string literal ( in gcc and other compilers). _FUNCTION_
不是预处理程序宏,它是字符串文字( 在gcc和其他编译器中)。
So to do what you want, you need to pass the function name into your macro: 因此,要做您想做的事情,您需要将函数名传递到宏中:
#define FAKE_PREFIX fakes::device::Flash::
#define DELEGATE_DESTINATION(func) FAKE_PREFIX func
Then you define your functions, like this: 然后定义函数,如下所示:
bool Flash::write(args)
{
DELEGATE_DESTINATION(write)(args);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.