[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
High level programming in C (was Re: GTC: Status Report & Roadmap)
> > struct _gtcMetaGfxVTbl
> > {
> > void (*put)(gtcMetaGfx *who, gtcReal where[4][4]);
> > float (*getsize)(gtcMetaGfx *who, gtcReal origin[3]);
> > void (*destroy)(gtcMetaGfx *who);
> > };
> >
>
> Gee, I'll have to work hard before getting familiar with
> plain C object oriented programming.
The main thing is being able to parse complicated declarations. There is a rule
for reading C declarations. Start by reading the identifier, then say "is a",
then start reading to the right of the identifier. If you hit a closing
parenthesis, you stop reading right and you start reading left. While reading
left if you hit an opening parenthesis, you stop reading left and you start
reading right. If you read a [], you say "array of". If you read *, you say
"pointer to" and if you see an open parenthesis while reading to the right, you
say "function whose parameters are (...) and return value is (...)".
Example:
char *(*(*x[][])[])(char **[]);
The identifier is x. This is how the declaration is read, with annotation:
"x is an array of arrays of [hit )] pointers to [hit (] arrays of [hit )]
pointers to [hit (] functions whose parameter is (an array of pointers to
pointers to chars) [hit ;] returning (a pointer to a char)."
When doing object-oriented programming, the "this" pointer which is implicitly
passed in C++ needs to be passed by hand, this is what the "who" pointer does in
the gtcMetaGfxVTbl functions.
If you want to do lisp-style closures in C, you need to pass a closure
parameter. I use this more often than object-oriented programming in GTC. Here
are a few instances where I use closures. In collider, you can pass a void *data
to gtc_collider, this pointer will get passed to the callback bong, which is
called for each colliding pair; this is a closure.
So we have closures and OO, the rest is syntactic sugar, as lispers say.
Also, in my experience, if you need complicated code generation, you are usually
doing something wrong. Examples are my gmt macro compiler and my lawyer
streaming code generator. Some simple code generation is however sometimes
useful, such as taking a text file and turning it into a C variable.
Sebastien Loisel
*************************************************************
To unsubscribe, send an e-mail to majordomo@gtc.seul.org with
unsubscribe gtc-dev in the body. http://gtc.seul.org/