Skip to content

Instantly share code, notes, and snippets.

@abissell
Last active January 3, 2016 02:29
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 abissell/33ed8399d8ce6c6d122b to your computer and use it in GitHub Desktop.
Save abissell/33ed8399d8ce6c6d122b to your computer and use it in GitHub Desktop.
// "chkd_array_factory_head.h"
// Guards to ensure we don't reuse an old list of types
#ifdef CHKD_ARRAY_TYPES
#error "CHKD_ARRAY_TYPES defined before included header"
#endif
#ifdef CHKD_ARRAY_PRIMITIVE_TYPES
#error "CHKD_ARRAY_PRIMITIVE_TYPES defined before included header"
#endif
#include "generic_defs.h"
// Expansion macro for checked array struct with typename T_NM
#define CHKD_ARRAY(T_NM) TYPED_STRUCT(chkd_array, T_NM)
// Expansion macros for functions on chkd_arrays with typename T_NM
#define CREATE(T_NM) TYPED_FN(chkd_array, T_NM, create)
#define IS_EQUAL(T_NM) TYPED_FN(chkd_array, T_NM, is_equal)
#define SET(T_NM) TYPED_FN(chkd_array, T_NM, set)
#define GET(T_NM) TYPED_FN(chkd_array, T_NM, get)
#define DESC(T_NM) TYPED_FN(chkd_array, T_NM, desc)
// Declaration of the checked array typed struct and functions.
#define CHKD_ARRAY_DEC(T, T_NM, DESC_LIT) \
\
CHKD_ARRAY(T_NM) { \
T *data; \
unsigned length; \
T err_val; \
}; \
static CHKD_ARRAY(T_NM) * CREATE(T_NM)(unsigned length, T *err_val); \
static inline bool IS_EQUAL(T_NM)(T *first, T *second); \
static inline T SET(T_NM)(CHKD_ARRAY(T_NM) * self, unsigned idx, T *val); \
static inline T *GET(T_NM)(CHKD_ARRAY(T_NM) * self, unsigned idx); \
static inline char *DESC(T_NM)();
// Used later to declare the checked array for ALL_TYPES.
#define DECLARE(ALL_TYPES) ALL_TYPES(CHKD_ARRAY_DEC)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment