gdjn  Artifact [7c7116050b]

Artifact 7c7116050b349742b947150cbbbfc925e0d8e4e4d51cd5940f73138f4a5ba92b:


/* [ʞ] src/type.h
 *  ~ lexi hale <lexi@hale.su>
 *  🄯 AGPLv3
 *  ? miscellaneous useful types & macros
 */

#pragma once

#define _strDynSz(a) \
	__builtin_choose_expr( \
			/*   if */ __builtin_types_compatible_p(typeof(a), pstr), \
			/* then */ ((pstr*)&(a))->sz, \
			/* else */ strlen(*(char const**)&(a)))
#define _strDynMem(a) \
	__builtin_choose_expr( \
			/*   if */ __builtin_types_compatible_p(typeof(a), pstr), \
			/* then */ ((pstr*)&(a))->v, \
			/* else */ *(char**)&(a))
#define _pstrOf(a) ((pstr){.v=_strDynMem(a), .sz=_strDynSz(a)})

#define _array(t) struct {t* v; size_t sz;}
typedef _array(char) pstr;

#define _pstrLit(a) ((pstr){.v=(a),.sz=sizeof(a)-1})

#define _drop(x) ({ \
	if (x.v) { \
		_free (x.v); \
		x.v = nullptr; \
		x.sz = 0; \
	} \
})
#define _new(T, n) ((T){ \
	.v = _alloc( typeof( ((typeof(T)) {}).v ), n), \
	.sz = n, \
})