diff --git a/src/asset.h b/src/asset.h new file mode 100644 index 0000000..c113ed5 --- /dev/null +++ b/src/asset.h @@ -0,0 +1,34 @@ +#ifndef _fencer_asset_h +#define _fencer_asset_h + +#include "drop.h" +#include "typeclass_helpers.h" +#include + +typedef uintptr_t asset_id; + +typedef struct { + asset_id (*const get_id)(void*); + void (*const set_id)(void*, asset_id); +} IAsset; + +typedef struct Asset { + void* data; + IAsset const* tc; + IDrop const* drop; +} Asset; + +#define impl_Asset_for(T, get_id_f, set_id_f)\ +static inline Asset T##_as_Asset(T* x) {\ + TC_FN_TYPECHECK(asset_id, get_id_f, T*);\ + TC_FN_TYPECHECK(void, set_id_f, T*, asset_id);\ + TC_FN_TYPECHECK(Drop, T##_as_Drop, T*);\ + static IAsset tc = (IAsset){\ + .get_id = (asset_id(*const)(void*)) get_id_f,\ + .set_id = (void(*const)(void*,asset_id)) set_id_f,\ + };\ + IDrop const* drop = T##_as_Drop(x).tc;\ + return (Asset){.data=x, .tc=&tc, .drop=drop};\ +} + +#endif // !_fencer_asset_h