【问题标题】:C Code PreprocessorC 代码预处理器
【发布时间】:2014-01-09 05:48:51
【问题描述】:

我有一段代码如下

    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

我有很多行代码必须手动执行。有没有办法定义宏之类的东西?

【问题讨论】:

  • 确保数据始终按网络字节顺序可能会更容易,那么您就不需要条件预处理器了。
  • 你能告诉我们每个组的相似和不同的部分是什么吗?如果不知道哪些部分是恒定的,哪些是可变的,我们就无法展示最好的宏。
  • ntohl 是宏,int32_T 是数据类型,event_structure 和 Local_Data 是变量。

标签: c macros c-preprocessor


【解决方案1】:

你只需要这样做:

   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 会做必要的操作

【讨论】:

    【解决方案2】:
    #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
    

    应该做的伎俩。

    【讨论】:

    • 在所有建议的方式中,我都必须更改代码 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);对我来说,我在代码中存在上述几行,有没有办法保留上述几行并定义宏?
    • 坦白说没有!如果不以某种方式在预期工作的行上调用宏,则无法根据定义更改行。
    【解决方案3】:

    当然,只需使用函数式宏:

    #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));
    

    【讨论】:

      【解决方案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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-29
        相关资源
        最近更新 更多