I have a structure that contains several function pointers. The generic interface is made in the header file.
我有一个包含几个函数指针的结构。通用接口在头文件中创建。
Header File
头文件
typedef struct
{
void (*Start)(void);
void (*ByteWrite)(uint8_t *pBuffer); // Modifies I2C buffer
uint8_t (*ByteRead)(uint8_t *pBuffer);
void (*ArrayWrite)(uint8_t *pBuffer);
uint8_t (*ArrayRead)(uint8_t *pBuffer);
bool (*Busy)(void);
} sI2C_t;
extern const sI2C_t I2C0;
extern const sI2C_t I2C1;
extern const sI2C_t I2C2;
Then in the C file each of the function-pointers are implemented to satisfy the structure interface.
然后在C文件中实现每个函数指针以满足结构接口。
C File
C文件
static void I2C0_Start(void) { ... }
static void I2C0_ByteWrite(*uint8_t) { ... }
static uint8_t I2C0_ByteRead(*uint8_t) { ... }
static void I2C0_ArrayWrite(*uint8_t) { ... }
static uint8_t I2C_ArrayRead(*uint8_t) { ... }
static bool I2C_Busy(void) { ... }
const sI2C I2C0 =
{
I2C0_Start,
I2C0_ByteWrite,
I2C0_ByteRead,
I2C0_ArrayWrite,
I2C0_ArrayRead,
I2C0_Busy
};
// Code-block repeated for I2C1, I2C2, etc. (REDUNDANT!)
This makes it relatively easy to access functions specific to the I2C interface:
这使得访问I2C接口特定的函数相对容易:
bool status;
I2C0.Start();
status = I2C1.Busy();
...
Although the function-pointers are basically the same for I2C0, I2C1, and I2C2, etc., I have to write out each of them individually for every new structure interface. Since this is redundant, is there a way for me to implement these function pointers only once?
虽然对于I2C0、I2C1和I2C2等函数指针基本上是相同的,但是我必须为每个新的结构接口分别写出它们。既然这是冗余的,我是否有办法只实现这些函数指针一次?
2 个解决方案
#1
1
The standard solution is to pass the structure pointer as the first parameter to the function. I.e. instead of:
标准的解决方案是将结构指针作为第一个参数传递给函数。即代替:
I2C0.Start();
you write:
你写的:
I2C0.Start(&I2C0);
You can then add some extra fields to the structure to identify which one it is (e.g. if you have fixed hardware addresses for each I2C bus, you might have the hardware addresses in an extra field of the structure).
然后,您可以向结构中添加一些额外的字段来识别它是哪个字段(例如,如果您为每个I2C总线拥有固定的硬件地址,那么您可能在结构的一个额外字段中拥有硬件地址)。
This is the normal C way to do the equivalent of C++ classes.
这是正常的C方法,可以执行与c++类相同的操作。
#2
0
You can write a constructor function. Example:
可以编写构造函数。例子:
typedef struct{
int a;
char b;
}example;
void constructor (example *pointer_to_struct, int a_value, char b_value){
pointer_to_struct->a = a_value;
pointer_to_struct->b = b_value; /*remember: if you have strings don't have any
assignments, since a string (like any other array) is a pointer to
its first element*/
}
int main (void){
example ex_struct;
constructor(&ex_struct, 10, 'C');
return 0;
}
EDIT: you can also write a function who makes the sames assignments for each structure of your selected type. Example:
编辑:您还可以编写一个函数,为所选类型的每个结构编写sames赋值。例子:
void constructor(structure *p){
p->a = 10;
p->b = 'C';
}
#1
1
The standard solution is to pass the structure pointer as the first parameter to the function. I.e. instead of:
标准的解决方案是将结构指针作为第一个参数传递给函数。即代替:
I2C0.Start();
you write:
你写的:
I2C0.Start(&I2C0);
You can then add some extra fields to the structure to identify which one it is (e.g. if you have fixed hardware addresses for each I2C bus, you might have the hardware addresses in an extra field of the structure).
然后,您可以向结构中添加一些额外的字段来识别它是哪个字段(例如,如果您为每个I2C总线拥有固定的硬件地址,那么您可能在结构的一个额外字段中拥有硬件地址)。
This is the normal C way to do the equivalent of C++ classes.
这是正常的C方法,可以执行与c++类相同的操作。
#2
0
You can write a constructor function. Example:
可以编写构造函数。例子:
typedef struct{
int a;
char b;
}example;
void constructor (example *pointer_to_struct, int a_value, char b_value){
pointer_to_struct->a = a_value;
pointer_to_struct->b = b_value; /*remember: if you have strings don't have any
assignments, since a string (like any other array) is a pointer to
its first element*/
}
int main (void){
example ex_struct;
constructor(&ex_struct, 10, 'C');
return 0;
}
EDIT: you can also write a function who makes the sames assignments for each structure of your selected type. Example:
编辑:您还可以编写一个函数,为所选类型的每个结构编写sames赋值。例子:
void constructor(structure *p){
p->a = 10;
p->b = 'C';
}