Weiguang said:
I can accept some comments. Actually, it might be useful to hint to
the parser what option it should generate for each entry.
Well, ok, how would we do this? This is off the top of my head:
struct something1 {
int slen;
char last[1 /* @:len=$.slen */ ]; /* struct hack. */
};
struct something2 {
int qty;
typename * ptr /* @:len=$.qty */; /* variable length ptr. */
};
struct something3 {
typename * ptr /* @:len=1 */; /* Just a pointer. */
};
struct something4 {
char * ptr /* @:len=strlen($.ptr)+1 */; /* A C string. */
};
So presumably you would do a text substitution of $ with the current
structure variable name in you autogenerated (un)marshalling code. And
@:len would tell you the count of this field.
struct something5 {
enum kind { k_W1, k_W2 } which;
union {
typename1 field1;
typename2 field2;
} x /* @:union(field1:$$.which==k_W1)
@:union(field2:$$.which==k_W2) */ ;
};
So $$ would pop above the current structure (in this case a union) to
the containing structure, and you determine which of the union fields is
valid by looking at the which field in the structure above it. A little
annoying to parse it all out, but doable.
So for serializing a bstring (
http://bstring.sf.net/) we would need
something like:
struct tagbstring {
int mlen /* @:unmarshall=$.slen+1 */;
int slen;
unsigned char * data /* @:len=slen+1 */;
};
So this extra field @:unmarshall gives an expression for overriding the
value that is generated when it is unpacked (because it is defined by
the memory size, which is instance dependent.)
It would be interesting to see if this could be made into something
truly general and thus make things like CORBA and COM obselete. Being
able to automarshall directly from a .h file would be extremely
valuable, IMHO. But as we can see just from these few examples, what
started as a simple idea (the @:len= ... idea) has needed some twisted
extensions (@:unmarshall,
@union, and $$ macros) to make it general to
real world scenarios.