API Shakeup: user data (was Re: [cairo] Patch improving fallbacks)

Jost Boekemeier jost2345 at yahoo.de
Fri Feb 25 03:44:32 PST 2005


> C doesn't have first-class functions

What's worse is that (unless you use K&R C) you cannot
formulate a function which takes a pointer to itself
as the argument:

  typedef int (func_t) (func_t *param);

doesn't work because typedef is only syntactic shugar.

So in the most general case one may avoid passing
function pointers around. Instead store the function
pointers and the data they close over into a
structure.  Like this:

typedef struct cairo_something_func {
  int (*invoke) (struct cairo_something_func *_this);
  void *closure;
} cairo_something_func_t;

This also avoids passing untyped data via the argument
list which I think is very ugly.


Example:

typedef struct cairo_path_func {
 void (*move) (struct cairo_path_func*_this);
[...]
 void (*close)(struct cairo_path_func*_this);
 void *closure;
} cairo_path_func_t;

Usage:
cairo_path_func_t f = { ... }
cairo_current_path_flat( cr, &f);


Jost


	

	
		
___________________________________________________________ 
Gesendet von Yahoo! Mail - Jetzt mit 250MB Speicher kostenlos - Hier anmelden: http://mail.yahoo.de



More information about the cairo mailing list