X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftcon%2Ftcon.h;h=93c3ea6b9f942bafca549b440e24b647f124214b;hp=0f6981d97917009dc816519e1485daaee0d61a59;hb=56023cca5f66a40646a1e807c3d10af6e5913623;hpb=3a34aa1a0c71b0be86511a8aa83af9935351dad1 diff --git a/ccan/tcon/tcon.h b/ccan/tcon/tcon.h index 0f6981d9..93c3ea6b 100644 --- a/ccan/tcon/tcon.h +++ b/ccan/tcon/tcon.h @@ -9,11 +9,11 @@ * * This declares a _tcon member for a structure. It should be the * last element in your structure; with sufficient compiler support it - * will not use any actual storage. tcon_to_raw() will compare + * will not use any actual storage. tcon_check() will compare * expressions with one of these "type canaries" to cause warnings if * the container is misused. * - * A type of "void *" will allow tcon_to_raw() to pass on any (pointer) type. + * A type of "void *" will allow tcon_check() to pass on any (pointer) type. * * Example: * // Simply typesafe linked list. @@ -61,6 +61,43 @@ #define tcon_check(x, canary, expr) \ (sizeof((x)->_tcon[0].canary == (expr)) ? (x) : (x)) +/** + * tcon_check_ptr - typecheck a typed container + * @x: the structure containing the TCON. + * @canary: which canary to check against. + * @expr: the expression whose type must match &TCON (not evaluated) + * + * This macro is used to check that the expression is a pointer to the type + * expected for this structure (note the "useless" sizeof() argument + * which contains this comparison with the type canary), or NULL. + * + * It evaluates to @x so you can chain it. + */ +#define tcon_check_ptr(x, canary, expr) \ + (sizeof(&(x)->_tcon[0].canary == (expr)) ? (x) : (x)) + + +/** + * tcon_type - the type within a container (or void *) + * @x: the structure containing the TCON. + * @canary: which canary to check against. + */ +#if HAVE_TYPEOF +#define tcon_type(x, canary) __typeof__((x)->_tcon[0].canary) +#else +#define tcon_type(x, canary) void * +#endif + +/** + * tcon_ptr_type - pointer to the type within a container (or void *) + * @x: the structure containing the TCON. + * @canary: which canary to check against. + */ +#if HAVE_TYPEOF +#define tcon_ptr_type(x, canary) __typeof__(&(x)->_tcon[0].canary) +#else +#define tcon_ptr_type(x, canary) void * +#endif /** * tcon_cast - cast to a canary type for this container (or void *) @@ -72,12 +109,7 @@ * platform doesn't HAVE_TYPEOF, then it casts to void * (which will * cause a warning if the user doesn't expect a pointer type). */ -#if HAVE_TYPEOF -#define tcon_cast(x, canary, expr) ((__typeof__((x)->_tcon[0].canary))(expr)) -#define tcon_cast_ptr(x, canary, expr) ((__typeof__(&(x)->_tcon[0].canary))(expr)) -#else -#define tcon_cast(x, canary, expr) ((void *)(expr)) -#define tcon_cast_ptr(x, canary, expr) ((void *)(expr)) -#endif +#define tcon_cast(x, canary, expr) ((tcon_type((x), canary))(expr)) +#define tcon_cast_ptr(x, canary, expr) ((tcon_ptr_type((x), canary))(expr)) #endif /* CCAN_TCON_H */