指针函数的结构体指针参数,如何优雅的适配不同结构体类型
typedef struct{
uint8_t reg;
uint32_t data;
}COEFFICIENT_STRUCT;
typedef struct{
uint8_t command;
uint8_t param;
} cfg_reg;
typedef struct{
uint8 Address;
uint8 Top_data;
uint8 Middle_data;
uint8 Botton_data;
}RAM1_TAB;
typedef struct{
BYTE Address;
BYTE First_data;
BYTE Top_data;
BYTE Middle_data;
BYTE Botton_data;
}RAM2_TAB;
上面四个结构体,会定义成结构体数组,现在每次选用不同的器件要打开相应宏定义,而且每增加一个器件又要增加宏定义、指针函数,很麻烦和不好看。
#define AMP_1
//#define AMP_2
//#define AMP_3
//#define AMP_4
#ifdef AMP_1
typedef int (*ampop_ctrl)( AmpList *pstAmpList, const COEFFICIENT_STRUCT *reg, int n);
#elif defined (AMP_2)
typedef int (*ampop_ctrl)( AmpList *pstAmpList, const cfg_reg *reg, int n);
#elif defined (AMP_3)
typedef int (*ampop_ctrl)( AmpList *pstAmpList, const RAM1_TAB *reg, int n);
#elif defined (AMP_4)
typedef int (*ampop_ctrl)( AmpList *pstAmpList, const RAM2_TAB *reg, int n);
#endif