Skip to content

Instantly share code, notes, and snippets.

@johnbartholomew
Created April 6, 2013 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnbartholomew/5327959 to your computer and use it in GitHub Desktop.
Save johnbartholomew/5327959 to your computer and use it in GitHub Desktop.
#if 0 /* example use: */
#define EMIT_FIXUP_DEFINITIONS 1
/* if EMIT_FIXUP_DEFINITIONS is 0 or undefined then
* only the struct declaration will be emitted
* typically you want to only emit fixup function
* definitions in a single translation unit.
*/
#define DECLARE_STRUCT_NAME BITMAPFILEHEADER
#define DECLARE_STRUCT_COMPONENTS \
STRUCT_FIELD(u16le, bfType) \
STRUCT_FIELD(u32le, bfSize) \
STRUCT_FIELD(pad16, bfReserved1) \
STRUCT_FIELD(pad16, bfReserved2) \
STRUCT_FIELD(u32le, bfOffBits)
#include "declare_file_struct.h"
#undef DECLARE_STRUCT_NAME
#undef DECLARE_STRUCT_COMPONENTS
#endif /* end example */
#ifndef DECLARE_STRUCT_NAME
#error "declare_file_struct.h requires DECLARE_STRUCT_NAME to be defined first"
#endif
#ifndef DECLARE_STRUCT_COMPONENTS
#error "declare_file_struct.h requires DECLARE_STRUCT_COMPONENTS to be defined first"
#endif
#ifdef STRUCT_FIELD
#error "declare_file_struct.h requires STRUCT_FIELD to be undefined"
#endif
#ifndef IDENT_JOIN
#define IDENT_JOIN2(x,y) x ## y
#define IDENT_JOIN(x,y) IDENT_JOIN2(x,y)
#endif
struct DECLARE_STRUCT_NAME {
#define STRUCT_FIELD(type, field) type field;
DECLARE_STRUCT_COMPONENTS
#undef STRUCT_FIELD
} __attribute__((__packed__));
#if defined(EMIT_FIXUP_DEFINITIONS) && EMIT_FIXUP_DEFINITIONS
void IDENT_JOIN(fixup_, DECLARE_STRUCT_NAME)(struct DECLARE_STRUCT_NAME *obj) {
#define STRUCT_FIELD(type, field) fixup_ ## type (&obj->field);
DECLARE_STRUCT_COMPONENTS
#undef STRUCT_FIELD
}
#endif /* EMIT_FIXUP_DEFINITIONS */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment