diff options
| author | J08nY | 2018-03-28 19:09:06 +0200 |
|---|---|---|
| committer | J08nY | 2018-03-28 19:09:06 +0200 |
| commit | f75446e573fb7d06be668b1dcdeb418f89b81af1 (patch) | |
| tree | 36d30b5caf5970dfbb8eaf4f3da3577a97d22ff0 /src/obj/obj.h | |
| parent | 2f4a68259dec833c77a9598f9c1a5da547e9324d (diff) | |
| download | ecgen-f75446e573fb7d06be668b1dcdeb418f89b81af1.tar.gz ecgen-f75446e573fb7d06be668b1dcdeb418f89b81af1.tar.zst ecgen-f75446e573fb7d06be668b1dcdeb418f89b81af1.zip | |
Introduce and use OBJ macro.
Diffstat (limited to 'src/obj/obj.h')
| -rw-r--r-- | src/obj/obj.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/obj/obj.h b/src/obj/obj.h new file mode 100644 index 0000000..5a7f9ab --- /dev/null +++ b/src/obj/obj.h @@ -0,0 +1,58 @@ + +#ifndef ECGEN_OBJ_H +#define ECGEN_OBJ_H + +#include "misc/types.h" +#include "util/memory.h" + +#define OBJ(obj_name, obj_type, copy_func, clone_func) \ + obj_type *obj_name##_new() { return try_calloc(sizeof(obj_type)); } \ + obj_type *obj_name##_new_copy(const obj_type *src) { \ + obj_type *result = obj_name##_new(); \ + return copy_func(src, result); \ + } \ + obj_type *obj_name##_new_clone(const obj_type *src) { \ + obj_type *result = obj_name##_new(); \ + return clone_func(src, result); \ + } + +#define OBJS(obj_name, obj_type, copy_func, clone_func) \ + obj_type **obj_name##s_new(size_t num) { \ + return try_calloc(num * sizeof(obj_type)); \ + } \ + obj_type **obj_name##s_copy(obj_type **const src, obj_type **dest, \ + size_t num) { \ + for (size_t i = 0; i < num; ++i) { \ + dest[i] = obj_name##_new_copy(src[i]); \ + } \ + return dest; \ + } \ + obj_type **obj_name##s_clone(obj_type **const src, obj_type **dest, \ + size_t num) { \ + for (size_t i = 0; i < num; ++i) { \ + dest[i] = obj_name##_new_clone(src[i]); \ + } \ + return dest; \ + } \ + obj_type **obj_name##s_new_copy(obj_type **const src, size_t num) { \ + obj_type **result = obj_name##s_new(num); \ + return obj_name##s_copy(src, result, num); \ + } \ + obj_type **obj_name##s_new_clone(obj_type **const src, size_t num) { \ + obj_type **result = obj_name##s_new(num); \ + return obj_name##s_clone(src, result, num); \ + } + +#define OBJ_H(obj_name, obj_type) \ + obj_type *obj_name##_new(); \ + obj_type *obj_name##_new_copy(const obj_type *src); \ + obj_type *obj_name##_new_clone(const obj_type *src); + +#define OBJS_H(obj_name, obj_type) \ + obj_type **obj_name##s_new(size_t num); \ + obj_type **obj_name##s_copy(obj_type **src, obj_type **dest, size_t num); \ + obj_type **obj_name##s_clone(obj_type **src, obj_type **dest, size_t num); \ + obj_type **obj_name##s_new_copy(obj_type **src, size_t num); \ + obj_type **obj_name##s_new_clone(obj_type **src, size_t num); + +#endif // ECGEN_OBJ_H |
