我的一个朋友 [s] 没有 stackoverflow 帐户[/s] (@Francesco) 重新阐述了@WhozCraig 的答案,添加了(非常老的)X macro(感谢 Randy Meyers 的great article)构建通用结构数组和枚举女巫帮助访问数组的结构,让用户只编辑一个点;
回去工作我告诉他我想尝试制作特定的 struct getter(因为数组中不会有 REPEATED STRUCT TYPE。如果你弄错了,编译时错误,所以最后一天你会在开发时低头,但一旦编译它会更健壮),所以我们将对请求的结构的类型进行编译检查;
回到家里,他已经实施了。赞!
然后我又浪费了一些时间,让开发人员只需键入一次结构,以防止那一侧的错位;
这是优化螺旋的开始;
- 由于专门的get和put,不再需要size数组,所以去掉了泛型结构
- 不需要初始化结构,我们使用“匿名”结构初始化
- 由于“匿名”初始化,直接访问结构更难
- 直接访问需要系统知识,并具有引导意识
- 编码更易读
- 易于导出
GENERIC_TABLE 和外部独立文件的结构定义
-
GENERIC_TABLE 上的#ifdef 工作正常,将其移至外部标题以增加可读性
- 在局部变量之外不需要动态初始化(无垃圾,嵌入式友好)
- 非常容易使用
成本
- 可能是预处理器有点臃肿?
- 似乎很多 OOP xD
- 您可以使用未启动的指针调用 get 和 put
TL;DR:这里是完整的编译代码和基本示例:
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
/* BEGIN: just some struct to test, fell free to move them on external header file */
struct A
{
int a1;
long a2;
};
struct B
{
float b1;
char b2[6];
};
struct C
{
unsigned int c1;
double c2[5];
};
/* END: just some struct to test, fell free to move them on external header file */
/* this macro will create the right X macro element, and also initiliaze the "anonymous" struct */
#define ADD_STRUCT_TO_ARRAY(xu) X(xu, &(struct xu){})SEP
/* BEGIN: add or remove your own struct here!
* fell free to move them on external header file
* just need to pass struct name without "struct" to ADD_STRUCT_TO_ARRAY macro */
#define GENERIC_TABLE \
ADD_STRUCT_TO_ARRAY(A) \
ADD_STRUCT_TO_ARRAY(B) \
ADD_STRUCT_TO_ARRAY(C)
/* END: add or remove your own struct here! */
/* here we initialize the enum, where the type of the struct is the key, and its position in the array the value */
#define SEP ,
#define X(a,b) a
enum STRUCT {
GENERIC_TABLE
};
#undef X
/* here we initalize the array of pointer to the structure */
#define X(a,b) b
void * const generic[] =
{
GENERIC_TABLE
};
#undef X
#undef SEP
/* here we create all the getter function. add here your array locking code */
#define SEP ;
#define X(a,b) void get_##a(struct a * dest){memcpy(dest, generic[a], sizeof(struct a) );}
GENERIC_TABLE
#undef X
#undef SEP
/* here we create all the putter function. add here your array locking code */
#define SEP ;
#define X(a,b) void put_##a(struct a * source){memcpy(generic[a], source, sizeof(struct a) );}
GENERIC_TABLE
#undef X
#undef SEP
/*run, code, run!*/
int main()
{
struct A a_copy;
struct B b_copy;
struct C c_copy;
get_A(&a_copy);
get_B(&b_copy);
get_C(&c_copy);
printf("STRUCTURE IN ARRAY BEFORE INITIALIZATION\n");
printf("A = %d\n", a_copy.a1);
printf("B = %f\n", b_copy.b1);
printf("C = %x\n", c_copy.c1);
a_copy.a1 = -1;
b_copy.b1 = 2.3;
c_copy.c1 = 3;
printf("STRUCTURE CHANGED TO\n");
printf("A = %d\n", a_copy.a1);
printf("B = %f\n", b_copy.b1);
printf("C = %x\n", c_copy.c1);
printf("STRUCTURE SAVED\n");
put_A(&a_copy);
put_B(&b_copy);
put_C(&c_copy);
get_A(&a_copy);
get_B(&b_copy);
get_C(&c_copy);
printf("STRUCTURE LOADED\n");
printf("A = %d\n", a_copy.a1);
printf("B = %f\n", b_copy.b1);
printf("C = %x\n", c_copy.c1);
a_copy.a1 = 1000;
b_copy.b1 = -50.576;
c_copy.c1 = 700;
printf("STRUCTURE CHANGED TO\n");
printf("A = %d\n", a_copy.a1);
printf("B = %f\n", b_copy.b1);
printf("C = %x\n", c_copy.c1);
get_A(&a_copy);
get_B(&b_copy);
get_C(&c_copy);
printf("STRUCTURE RELOADED WITHOUT SAVING\n");
printf("A = %d\n", a_copy.a1);
printf("B = %f\n", b_copy.b1);
printf("C = %x\n", c_copy.c1);
return 0;
}