C代码预处理器
我有一段代码如下C代码预处理器,c,macros,c-preprocessor,C,Macros,C Preprocessor,我有一段代码如下 Local_DATA[0] = * ((int32_T *) event_structure + 1); Local_DATA[1] = * ((int32_T *) event_structure + 2); Local_DATA[2] = * ((int32_T *) event_structure + 3); Local_DATA[3] = * ((int32_T *) event_structure + 4); 我必须做一个像这
Local_DATA[0] = * ((int32_T *) event_structure + 1);
Local_DATA[1] = * ((int32_T *) event_structure + 2);
Local_DATA[2] = * ((int32_T *) event_structure + 3);
Local_DATA[3] = * ((int32_T *) event_structure + 4);
我必须做一个像这样的预处理器
#ifdef ABC
Local_DATA[0] = * ((int32_T *) event_structure + 1);
Local_DATA[1] = * ((int32_T *) event_structure + 2);
Local_DATA[2] = * ((int32_T *) event_structure + 3);
Local_DATA[3] = * ((int32_T *) event_structure + 4);
#else
Local_DATA[0] = ntohl (* ((int32_T *) event_structure + 1));
Local_DATA[1] = ntohl (* ((int32_T *) event_structure + 2));
Local_DATA[2] = ntohl (* ((int32_T *) event_structure + 3));
Local_DATA[3] = ntohl (* ((int32_T *) event_structure + 4));
#endif
我有很多行代码需要手动执行。有没有办法定义类似宏的内容?当然,只需使用函数式宏:
#if ABC
#define MYORDER(x) (x)
#else
#define MYORDER(x) ntohl(x)
#endif
...
Local_DATA[0] = MYORDER(* ((int32_T *) event_structure + 1));
Local_DATA[1] = MYORDER(* ((int32_T *) event_structure + 2));
Local_DATA[2] = MYORDER(* ((int32_T *) event_structure + 3));
Local_DATA[3] = MYORDER(* ((int32_T *) event_structure + 4));
当然,只需使用函数样式宏:
#if ABC
#define MYORDER(x) (x)
#else
#define MYORDER(x) ntohl(x)
#endif
...
Local_DATA[0] = MYORDER(* ((int32_T *) event_structure + 1));
Local_DATA[1] = MYORDER(* ((int32_T *) event_structure + 2));
Local_DATA[2] = MYORDER(* ((int32_T *) event_structure + 3));
Local_DATA[3] = MYORDER(* ((int32_T *) event_structure + 4));
您只需执行以下操作:
Local_DATA[0] = ntohl (* ((int32_T *) event_structure + 1));
Local_DATA[1] = ntohl (* ((int32_T *) event_structure + 2));
Local_DATA[2] = ntohl (* ((int32_T *) event_structure + 3));
Local_DATA[3] = ntohl (* ((int32_T *) event_structure + 4));
就像网络订单与主机订单相同一样,ntohl
将是一个noop宏。
否则,
ntohl
将执行必要的操作您只需执行以下操作:
Local_DATA[0] = ntohl (* ((int32_T *) event_structure + 1));
Local_DATA[1] = ntohl (* ((int32_T *) event_structure + 2));
Local_DATA[2] = ntohl (* ((int32_T *) event_structure + 3));
Local_DATA[3] = ntohl (* ((int32_T *) event_structure + 4));
#ifdef ABC
#define REF_PTR(s, off) (* ((int32_T *) s + off))
#else
#define REF_PTR(s, off) (ntohl (* ((int32_T *) s + off)))
#endif
Local_DATA[0] = REF_PTR(event_structure, 1);
// etc
就像网络订单与主机订单相同一样,ntohl
将是一个noop宏。
否则,ntohl
将执行必要的操作
#ifdef ABC
#define REF_PTR(s, off) (* ((int32_T *) s + off))
#else
#define REF_PTR(s, off) (ntohl (* ((int32_T *) s + off)))
#endif
Local_DATA[0] = REF_PTR(event_structure, 1);
// etc
我们应该做到这一点
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
#ifdef ABC
#define PROC1(z, n, func) Local_DATA[n] = * ((int32_T *) event_structure + BOOST_PP_ADD(n, 1));
#else
#define PROC1(z, n, func) Local_DATA[n] = func (* ((int32_T *) event_structure + BOOST_PP_ADD(n, 1)));
#endif
#define PROC(func, n) BOOST_PP_REPEAT(n, PROC1, func)
...
PROC(ntohl, 4);
应该做到这一点。#包括
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/arithmetic/add.hpp>
#ifdef ABC
#define PROC1(z, n, func) Local_DATA[n] = * ((int32_T *) event_structure + BOOST_PP_ADD(n, 1));
#else
#define PROC1(z, n, func) Local_DATA[n] = func (* ((int32_T *) event_structure + BOOST_PP_ADD(n, 1)));
#endif
#define PROC(func, n) BOOST_PP_REPEAT(n, PROC1, func)
...
PROC(ntohl, 4);
#包括
#ifdef ABC
#定义PROC1(z,n,func)本地_数据[n]=*((int32_T*)事件_结构+BOOST_PP_ADD(n,1));
#否则
#定义PROC1(z,n,func)局部_数据[n]=func(*(int32_T*)事件_结构+BOOST_PP_ADD(n,1));
#恩迪夫
#定义PROC(func,n)BOOST\u PP\u REPEAT(n,PROC1,func)
...
PROC(ntohl,4);
#包括
#包括
#ifdef ABC
#定义PROC1(z,n,func)本地_数据[n]=*((int32_T*)事件_结构+BOOST_PP_ADD(n,1));
#否则
#定义PROC1(z,n,func)局部_数据[n]=func(*(int32_T*)事件_结构+BOOST_PP_ADD(n,1));
#恩迪夫
#定义PROC(func,n)BOOST\u PP\u REPEAT(n,PROC1,func)
...
PROC(ntohl,4);
确保数据始终以网络字节顺序排列可能更容易,这样您就不需要预处理器条件。您能告诉我们每个组的相似部分和不同部分是什么吗?如果不知道哪些部分是常量,哪些部分是变量,我们就无法显示最佳宏。ntohl是一个宏,int32_t是一个数据类型,event_结构,Local_数据是变量。确保数据始终以网络字节顺序排列可能更容易,那么你就不需要预处理器了。你能告诉我们每组的相似部分和不同部分是什么吗?如果不知道哪些部分是常量,哪些部分是变量,我们就无法显示最好的宏。ntohl是宏,int32_t是数据类型,event_结构,Local_数据是变量。在所有建议的方法中,我必须更改代码Local_data[0]=*((int32_t*)event_结构+1);本地_数据[1]=*((int32_T*)事件_结构+2);本地_数据[2]=*((int32_T*)事件_结构+3);本地_数据[3]=*((int32_T*)事件_结构+4);对我来说,代码中存在上述行,有没有办法保留上述行并定义宏?坦白说,没有!如果不在预期工作的行上以某种方式调用宏,则无法根据定义更改行。在建议的所有方式中,我必须更改代码本地_数据[0]=*((int32_T*)事件_结构+1);本地_数据[1]=*((int32_T*)事件_结构+2);本地_数据[2]=*((int32_T*)事件_结构+3);本地_数据[3]=*((int32_T*)事件_结构+4);对我来说,代码中存在上述行,有没有办法保留上述行并定义宏?坦白说,没有!如果不在预期工作的行上调用宏,则无法根据定义更改行。