Compare commits
3 Commits
7b20909c54
...
f84e3a3a27
Author | SHA1 | Date |
---|---|---|
Sara | f84e3a3a27 | |
Sara | 85dd8e5fec | |
Sara | ff87fa96e1 |
32
drop.c
32
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);
|
||||
|
|
13
drop.h
13
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
|
||||
|
|
2
list.c
2
list.c
|
@ -29,7 +29,7 @@ List list_with_len(size_t element_size, size_t len) {
|
|||
|
||||
List list_copy(const List* source) {
|
||||
List self = list_init(source->element_size);
|
||||
list_set_len(&self, source->cap);
|
||||
list_set_len(&self, source->len);
|
||||
if(self.cap > 0) {
|
||||
memcpy(self.data, source->data, source->element_size * source->len);
|
||||
} else {
|
||||
|
|
|
@ -7,4 +7,7 @@ __Return (*const __Name##_)(__VA_ARGS__) = __Name; (void)__Name##_
|
|||
#define decl_typeclass_impl(__Typeclass, __Type)\
|
||||
extern __Typeclass __Type##_as_##__Typeclass(__Type*);
|
||||
|
||||
#define new(T) malloc(sizeof(T))
|
||||
#define new_as(T, TC) T##_as_##TC(new(T))
|
||||
|
||||
#endif // !CUTES_TYPECLASS_HELPERS_H
|
||||
|
|
Loading…
Reference in New Issue