From 640ed84c1d4923bd566150421c65031904122eb0 Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 25 Jan 2024 14:13:42 +0100 Subject: [PATCH] feat: added Drop trait --- drop.c | 6 ++++++ drop.h | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 drop.c create mode 100644 drop.h diff --git a/drop.c b/drop.c new file mode 100644 index 0000000..7bd372a --- /dev/null +++ b/drop.c @@ -0,0 +1,6 @@ +#include "drop.h" +#include "stdlib.h" + +void default_drop(void* data) { + free(data); +} diff --git a/drop.h b/drop.h new file mode 100644 index 0000000..7b51877 --- /dev/null +++ b/drop.h @@ -0,0 +1,33 @@ +#ifndef _fencer_drop_h +#define _fencer_drop_h + +#include "typeclass_helpers.h" + +typedef struct { + void (*const drop)(void* self); +} IDrop; + +typedef struct { + void* data; + IDrop const* tc; +} Drop; + +#define impl_Drop_for(T, drop_f)\ +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};\ +} + +extern void default_drop(void*); + +#define impl_default_Drop_for(T)\ +Drop T##_as_Drop(T* x) {\ + static IDrop const tc = {\ + .drop = default_drop,\ + };\ + return (Drop){.tc = &tc, .data = x};\ +} +#endif // !_fencer_drop_h