From f84e3a3a27d626405173bd84055c299969cf7033 Mon Sep 17 00:00:00 2001 From: Sara Date: Tue, 10 Sep 2024 09:47:55 +0200 Subject: [PATCH] feat: added _as_drop for numeric types --- drop.c | 32 ++++++++++++++++++++++++++++++++ drop.h | 13 +++++++++++++ 2 files changed, 45 insertions(+) diff --git a/drop.c b/drop.c index 7bd372a..fdb2b5d 100644 --- a/drop.c +++ b/drop.c @@ -1,6 +1,38 @@ #include "drop.h" #include "stdlib.h" +static IDrop _builtin_drop = { + .drop = free +}; + void default_drop(void* data) { free(data); } + +#define impl_builtin_Drop_for(T)\ +Drop T##_as_Drop(T *x) {\ + return (Drop){.tc = &_builtin_drop, .data = x};\ +}\ +Drop T##_to_Drop(T x) {\ + float *data = malloc(sizeof(float));\ + return (Drop){.tc = &_builtin_drop, .data = data};\ +} + +void TEST_drop_builtins() { + // TEST float + float *ff = new(float); + free(ff); + Drop f = new_as(float, Drop); + f.tc->drop(f.data); + // TEST int + int *ii = new(int); + free(ii); + Drop i = new_as(int, Drop); + i.tc->drop(i.data); +} + +impl_builtin_Drop_for(float); +impl_builtin_Drop_for(double); +impl_builtin_Drop_for(int); +impl_builtin_Drop_for(unsigned); +impl_builtin_Drop_for(size_t); diff --git a/drop.h b/drop.h index c9fff22..46cef19 100644 --- a/drop.h +++ b/drop.h @@ -2,6 +2,7 @@ #define CUTES_DROP_H #include "typeclass_helpers.h" +#include "stddef.h" typedef struct { void (*const drop)(void* self); @@ -30,4 +31,16 @@ Drop T##_as_Drop(T* x) {\ };\ return (Drop){.tc = &tc, .data = x};\ } + +Drop float_as_Drop(float *x); +Drop float_to_Drop(float x); +Drop double_as_Drop(double *x); +Drop double_to_Drop(double x); +Drop int_as_Drop(int *x); +Drop int_to_Drop(int x); +Drop unsigned_as_Drop(unsigned *x); +Drop unsigned_to_Drop(unsigned x); +Drop size_t_as_Drop(size_t *x); +Drop size_t_to_Drop(size_t x); + #endif // !CUTES_DROP_H