矿[S]没有计算器的帐户[/ S](@Francesco)的朋友已经重新阐述了@WhozCraig答案(由兰迪·迈尔斯感谢great article)将(很老)X macro打造通用结构数组和枚举女巫有助于访问数组的结构,让用户只编辑一个点;
回到工作我告诉过他,我想尝试使特定的结构getter(因为在数组中没有REPEATED STRUCT TYPE,如果你弄错了,编译时错误,所以在当你在开发的时候你会弯下腰,但是一旦编译它就会更加健壮),所以我们将对所请求的结构的类型进行编译检查; 回到家他已经实施了。奖励!
然后我又失去了一些时间,使开发人员只需键入一次结构,以防止在那一侧的错位; 这已经是优化螺旋的开始;
- 因为专门get和put,不再需要大小排列的,所以删除了通用的结构
- 没有初始化的结构需要,我们使用“匿名”结构初始化
- 因为“匿名”初始化,它是很难直接访问结构
- 直接访问需要的知识体系,有导致意识
- 代码的可读性更强
- 容易导出
GENERIC_TABLE
和外部独立的文件结构定义
- #ifdef的
GENERIC_TABLE
正常工作,将其移动到外部头以增加可读性
- 外局部变量不需要动态初始化(无垃圾,嵌入型)
- 使用极为方便
成本
- 也许有点臃肿预处理器?
- 看来很多OOP的xD
- 可以拨打得到与外行指针
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;
}
我觉得你的问题是,“我可以写一个函数,它需要一个'struct'和一个索引和'struct'目标地址作为输入 - 这样特定的'struct'被绑定'别的地方'?” – 2014-09-23 22:06:35
“有没有办法在编译时自动化sizeof数组的初始化?”唯一可用的机制是预处理器。这是抽象这种功能最常用的工具。 – 2014-09-23 22:07:13
是否可以将每个结构的大小存储在它自己内部?即,在每个结构中添加一个成员int mySize作为第一个成员,并将它(在运行时)设置为'x.mySize = sizeof(struct x)'。 – usr2564301 2014-09-23 22:09:36