From cf001bd2d6a906b9ee5ccc14c510924b34398edb Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 2 Nov 2023 14:25:53 +0100 Subject: [PATCH] added Drop trait to allow more automated memory management --- src/drop.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/drop.h diff --git a/src/drop.h b/src/drop.h new file mode 100644 index 0000000..db7040c --- /dev/null +++ b/src/drop.h @@ -0,0 +1,22 @@ +#ifndef _fencer_drop_h +#define _fencer_drop_h + +typedef struct { + void (*const drop)(void* self); +} IDrop; + +typedef struct { + void const* data; + IDrop const* tc; +} Drop; + +#define impl_Drop_for(T, drop_f)\ +static inline Drop T##_as_Drop(T* x) {\ + TC_FN_TYPECHECK(void, drop_f, T*);\ + static IDrop const tc = {\ + .drop = (void(*const)(void*)) drop_f,\ + };\ + return (Drop){.tc = &tc, .data = x};\ +} + +#endif // !_fencer_drop_h