intmain() { user u1; PRINT(u1) user u2 = {0, 1}; PRINT(u2) user u3 = {.id=1, .age=2}; PRINT(u3) user u4 = {.id=2}; PRINT(u4) user u5 = {.age=5}; PRINT(u5) user u6 = {.age=6, .id=6}; PRINT(u6) return0; }
intmain(int argc, char **argv) { // male int id1 = 111; int age1 = 111; male m = {.id=id1, .age=age1, .male_uniq=id1}; data data1 = {.m=&m}; person p1 = {.data=&data1, .interface=&male_fn}; p1.interface(p1.data);
int id2 = 222; int age2 = 222; female f = {.id=id2, .age=age2, .female_uniq=id2}; data data2 = {.f=&f}; person p2 = {.data=&data2, .interface=&female_fn}; p2.interface(p2.data); return0; }
4 initConfigValues
有了上面内容的铺垫,再来看一下redis中initConfigValues方法的多态实现。
4.1 configs数组
1
standardConfig configs[];
1 2 3 4 5 6 7
typedefstructstandardConfig { constchar *name; /* The user visible name of this config */ constchar *alias; /* An alias that can also be used for this config */ constunsignedint flags; /* Flags for this specific config */ typeInterface interface; /* The function pointers that define the type interface */ typeData data; /* The type specific data exposed used by the interface */ } standardConfig;
// 实例的方法 函数指针 真正实现后面创建实例的时候指定 高级语言的getter/setter typedefstructtypeInterface { /* Called on server start, to init the server with default value */ void (*init)(typeData data); /* Called on server startup and CONFIG SET, returns 1 on success, 0 on error * and can set a verbose err string, update is true when called from CONFIG SET */ int (*set)(typeData data, sds value, int update, constchar **err); /* Called on CONFIG GET, required to add output to the client */ void (*get)(client *c, typeData data); /* Called on CONFIG REWRITE, required to rewrite the config state */ void (*rewrite)(typeData data, constchar *name, struct rewriteConfigState *state); } typeInterface;