Sunday, January 20, 2019

Roll your own value types

typedef union {
Key key;
unsigned long l;
void * vp;
char txt[8];
struct Sym * sym;
} Val;
SYMBOL(Val);

 In this case, I define all the things I want a Val to be.

So if I get a symbol I cans assign the def field as Val being : sym->def.sym =Sym * or sym->def.l = 9 and so on.

 But I need Val defined before I define the symbol table entry, so maps.h provides the standard symbol entry layout, in a macro. That macro can define the def field the way the application needs it.

There is one other common form not mentioned, the arg dependency form. arg2 depends on arg1, arg3 depends on arg2, and so on. chars uses that on set up, yuo need a conn, then a win hen a rect to write a sting. So xchars maintains the dependencies on the command line to match the dependency links in its symbol table.

So, take the basic root type, create  typedef called ArgDep or something, a named typedef that all snippets using it form can check against.  chars seemed complicated until common forms, then we see xchars just uses arg dependency form to setup, and on of the string forms to fill in.  Ten basic forms over args list and we have nailed 85% of the use cases.

No comments: