shithub: libnate

ref: aab184987c79fc7a8e5467200f9ac448700b797f
dir: /nate_construct.h/

View raw version
void nc_push(Nelem*);
void nc_pop(void);
Nelem* nc_get(void);

// #define N_TYPE in your implementation
#define COND(N) (N->type == N_TYPE)
#define GUARD(N) assert(N->type == N_TYPE)

// create with benefits
Nelem *makenelem(long size, char *type, Nelemfunctions *funcs, Nelemaccessors*, char *name, int nchildren);
#define MakeNelem(Type, Char, F, ACCS, N, NUM) ((Type*)makenelem(sizeof(Type), Char, F, ACCS, N, NUM));

void nc_addchild(Nelem*, Nelem*);

// internal stuff
void nc_init(void);

// trace hits
extern int nctracehitlevel;
void nctracehit(Nelem *el, int passthrough);

// default functionality
Nelem* nd_checkhit(Nelem* nelem, Image* screen, Mouse m);
void nd_free(Nelem *nelem);

// syntactic sugar: get accessor of type
#define NCACCS(Type, nel) ((Type*)(nel->accs))

// syntactic sugar: default slot
#define DEF_SLOTFUNC(Func) \
	static NACCS* \
	Func(Nslot info, Nelemaccessors *elaccs) \
	{ \
		Nelem *el = nc_get(); \
		nc_pop(); \
		assert(el->type == elaccs->type); \
		NTYPE *b = (NTYPE*)nc_get(); \
		GUARD(b); \
		el->slot = info; \
		nc_addchild(b, el); \
		return (NACCS*)b->accs; \
	}

// syntactic sugar: default accessors
#define DEF_ACCESSOR_OneParam(Func, T1, N1) \
	static NACCS* \
	Func(T1 N1) \
	{ \
		NTYPE *b = (NTYPE*)nc_get(); \
		GUARD(b); \
		b->N1 = N1; \
		return (NACCS*)b->accs; \
	}

#define DEF_ACCESSOR_TwoParams(Func, T1, N1, T2, N2) \
	static NACCS* \
	Func(T1 N1, T2 N2) \
	{ \
		NTYPE *b = (NTYPE*)nc_get(); \
		GUARD(b); \
		b->N1 = N1; \
		b->N2 = N2; \
		return (NACCS*)b->accs; \
	}

#define DEF_ACCESSOR_ThreeParams(Func, T1, N1, T2, N2, T3, N3) \
	static NACCS* \
	Func(T1 N1, T2 N2, T3 N3) \
	{ \
		NTYPE *b = (NTYPE*)nc_get(); \
		GUARD(b); \
		b->N1 = N1; \
		b->N2 = N2; \
		b->N3 = N3; \
		return (NACCS*)b->accs; \
	}