类似函数的宏定义

时间:2017-11-11 09:08:13

标签: function c-preprocessor

问题是,我不太明白以下定义的作用。有人可以向我解释一下吗?

#define Vpc3MemSet_( _pToVpc3Memory, _bValue, _wLength )\
           Vpc3MemSet( _pToVpc3Memory, _bValue, _wLength )

2 个答案:

答案 0 :(得分:1)

你的宏几乎什么也没做。它将Vpc3MemSet_(带下划线)替换为Vpc3MemSet(不带下划线)。如果存在不同的,依赖于平台的变体(例如Linux,macOS和Windows),这些宏通常是有意义的。对于(虚构的)例子:

#ifdef __WINDOWS__
    #define Vpc3MemSet_( _pToVpc3Memory, _bValue, _wLength )\
       Vpc3MemSet( _pToVpc3Memory, _bValue, _wLength )
#else
    #define Vpc3MemSet_( _pToVpc3Memory, _bValue, _wLength )\
       memset(_pToVpc3Memory, _bValue, _wLength * sizeof(Vpc3))
#endif

您可以使用相同的宏调用调用与平台相关的函数:

Vpc3MemSet_(memory, value, length);

这将扩展到Windows和Linux上的不同函数调用。

答案 1 :(得分:0)

然而还有一件事。这是整个#define序列:

#if VPC3_SERIAL_MODE


 #define CopyToVpc3_( _pToVpc3Memory, _pLocalMemory, _wLength )\
           CopyToVpc3( _pToVpc3Memory, _pLocalMemory, _wLength )

 #define CopyFromVpc3_( _pLocalMemory, _pToVpc3Memory, _wLength )\
           CopyFromVpc3( _pLocalMemory, _pToVpc3Memory, _wLength )

 #define Vpc3MemSet_( _pToVpc3Memory, _bValue, _wLength )\
           Vpc3MemSet( _pToVpc3Memory, _bValue, _wLength )

 #define Vpc3MemCmp_( _pToVpc3Memory1, _pToVpc3Memory2, _wLength )\
           Vpc3MemCmp( _pToVpc3Memory1, _pToVpc3Memory2, _wLength )

#else

 #define CopyToVpc3_( _pToVpc3Memory, _pLocalMemory, _wLength )\
           memcpy( _pToVpc3Memory, _pLocalMemory, _wLength )

 #define CopyFromVpc3_( _pLocalMemory, _pToVpc3Memory, _wLength )\
           memcpy( _pLocalMemory, _pToVpc3Memory, _wLength )

 #define Vpc3MemSet_( _pToVpc3Memory, _bValue, _wLength )\
           memset( _pToVpc3Memory, _bValue, _wLength )

 #define Vpc3MemCmp_( _pToVpc3Memory1, _pToVpc3Memory2, _wLength )\
           memcmp( _pToVpc3Memory1, _pToVpc3Memory2, _wLength )

#endif /* #if VPC3_SERIAL_MODE */

因此,这四个函数有两个定义,具体取决于您是处于串行模式还是并行模式。然而,memset()函数在应用程序源文件中调用,如下所示:

 /*-----------------------------------------------------------------------*/
 /* init application data                                                 */
 /*-----------------------------------------------------------------------*/
  memset( &sDpAppl, 0, sizeof( sDpAppl ) );

 /*-----------------------------------------------------------------------*/
 /* initialize VPC3                                                       */
 /*-----------------------------------------------------------------------*/
 #if VPC3_SERIAL_MODE
  Vpc3AsicAddress = (VPC3_ADR)VPC3_ASIC_ADDRESS;
  pVpc3 = &sVpc3OnlyForInit;
  pDpSystem = &sDpSystemChannel1;

  memset( pVpc3, 0, sizeof( VPC3_STRUC ) );
#else
  pVpc3Channel1 = (VPC3_STRUC_PTR)VPC3_ASIC_ADDRESS;
  Vpc3AsicAddress = (VPC3_ADR)VPC3_ASIC_ADDRESS;
  pVpc3 = pVpc3Channel1;
  pDpSystem = &sDpSystemChannel1;
#endif//#if VPC3_SERIAL_MODE

好像这是一个不同的功能。或者,我还缺少什么? :/ 顺便说一下,我处于串行模式,我已经定义了CopyToVpc3(),CopyFromVpc3(),Vpc3MemSet(),Vpc3MemCmp()。