feat: added _as_drop for numeric types

main
Sara 2024-09-10 09:47:55 +02:00
parent 85dd8e5fec
commit f84e3a3a27
2 changed files with 45 additions and 0 deletions

32
drop.c
View File

@ -1,6 +1,38 @@
#include "drop.h" #include "drop.h"
#include "stdlib.h" #include "stdlib.h"
static IDrop _builtin_drop = {
.drop = free
};
void default_drop(void* data) { void default_drop(void* data) {
free(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);

13
drop.h
View File

@ -2,6 +2,7 @@
#define CUTES_DROP_H #define CUTES_DROP_H
#include "typeclass_helpers.h" #include "typeclass_helpers.h"
#include "stddef.h"
typedef struct { typedef struct {
void (*const drop)(void* self); void (*const drop)(void* self);
@ -30,4 +31,16 @@ Drop T##_as_Drop(T* x) {\
};\ };\
return (Drop){.tc = &tc, .data = 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 #endif // !CUTES_DROP_H