From 84ad096896e9948c722a957d07ed6868e252a9f7 Mon Sep 17 00:00:00 2001 From: Jonas Kohl Date: Thu, 29 Aug 2024 17:00:11 +0200 Subject: Initial commit --- includes/oo.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ includes/point.c | 31 +++++++++++++++++++++++++++++++ includes/point.h | 27 +++++++++++++++++++++++++++ includes/rect.c | 43 +++++++++++++++++++++++++++++++++++++++++++ includes/rect.h | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+) create mode 100644 includes/oo.h create mode 100644 includes/point.c create mode 100644 includes/point.h create mode 100644 includes/rect.c create mode 100644 includes/rect.h (limited to 'includes') diff --git a/includes/oo.h b/includes/oo.h new file mode 100644 index 0000000..b75812f --- /dev/null +++ b/includes/oo.h @@ -0,0 +1,54 @@ +#ifndef OO_H +#define OO_H + +#define NAMEOF_(X) #X +#define NAMEOF(X) NAMEOF_(X) + +#define __CONCAT(x, y) x ## y + +#define CONCAT2(x, y) __CONCAT(x, y) +#define CONCAT3(x, y, z) CONCAT2(CONCAT2(x, y), z) +#define CONCAT4(x, y, z, w) CONCAT2(CONCAT2(x, y), CONCAT2(z, w)) + +#define CATCH_SELF_NULL(MNAME) CATCH_SELF_NULL_(MNAME) +#define CATCH_SELF_NULL_(MNAME) do { \ + if (NULL == self) { \ + fprintf(stderr, "[FATAL] " #MNAME " -- self is null\n"); \ + exit(139); \ + } \ +} while (0); + +// end of utils + +#define SIZEOF_OBJ(T) sizeof(DEFINE_OBJECT_STRUCTURE(T)) + +#define DEFINE_OBJECT_STRUCTURE(N) struct CONCAT3(__oo_, N, _struct) +#define DEFINE_OBJECT_PTR(N) typedef DEFINE_OBJECT_STRUCTURE(N) * N +#define DEFINE_OBJECT(N) DEFINE_OBJECT_STRUCTURE(N); DEFINE_OBJECT_PTR(N); + +#define CONSTRUCTOR(T) T CONCAT2(T, _alloc)() +#define CONSTRUCTOR_IMPLEMENT(T) CONSTRUCTOR(T) { \ + T ptr = (T)malloc(SIZEOF_OBJ(T)); \ + if (NULL == ptr) { \ + perror("Could not allocate new '" NAMEOF(T) "' instance"); \ + exit(1); \ + } \ + return ptr; \ +} + +#define METHOD(T, N) CONCAT3(T, _, N) (T self) +#define METHOD_ARG(T, N, ...) CONCAT3(T, _, N) (T self, __VA_ARGS__) + +#define GETTER(T, MT, M) MT CONCAT4(T, _, M, _get) (T self) +#define SETTER(T, MT, M) void CONCAT4(T, _, M, _set) (T self, MT v) + +#define GETTER_IMPLEMENT(T, MT, M) GETTER(T, MT, M) { \ + CATCH_SELF_NULL(CONCAT4(T, _, M, _get)); \ + return self-> M; \ +} +#define SETTER_IMPLEMENT(T, MT, M) SETTER(T, MT, M) { \ + CATCH_SELF_NULL(CONCAT4(T, _, M, _set)); \ + self-> M = v; \ +} + +#endif diff --git a/includes/point.c b/includes/point.c new file mode 100644 index 0000000..a68fcf2 --- /dev/null +++ b/includes/point.c @@ -0,0 +1,31 @@ +#include +#include +#include "point.h" + +#define TYPE Point + +DEFINE_OBJECT_STRUCTURE(TYPE) { + float x; + float y; +}; + +CONSTRUCTOR_IMPLEMENT(TYPE); + +void METHOD_ARG(TYPE, init, float x, float y) { + CATCH_SELF_NULL(TYPE_init); + self->x = x; + self->y = y; +} + +int METHOD_ARG(TYPE, toString, char* buffer) { + CATCH_SELF_NULL(TYPE_toString); + return sprintf(buffer, NAMEOF(TYPE) " { x = %.2f, y = %.2f }", self->x, self->y); +} + +GETTER_IMPLEMENT(TYPE, float, x); +SETTER_IMPLEMENT(TYPE, float, x); + +GETTER_IMPLEMENT(TYPE, float, y); +SETTER_IMPLEMENT(TYPE, float, y); + +#undef TYPE diff --git a/includes/point.h b/includes/point.h new file mode 100644 index 0000000..311d64d --- /dev/null +++ b/includes/point.h @@ -0,0 +1,27 @@ +#ifndef POINT_H +#define POINT_H + +#include "oo.h" + +#define TYPE Point + +DEFINE_OBJECT(TYPE); + +// Constructor +CONSTRUCTOR(TYPE); + +// Methods +void METHOD_ARG(TYPE, init, float x, float y); + +int METHOD_ARG(TYPE, toString, char* buffer); + +// Properties (getters & setters) +GETTER(TYPE, float, x); +SETTER(TYPE, float, x); + +GETTER(TYPE, float, y); +SETTER(TYPE, float, y); + +#undef TYPE + +#endif diff --git a/includes/rect.c b/includes/rect.c new file mode 100644 index 0000000..36abde7 --- /dev/null +++ b/includes/rect.c @@ -0,0 +1,43 @@ +#include +#include +#include "rect.h" + +#define TYPE Rect + +DEFINE_OBJECT_STRUCTURE(TYPE) { + int left, top, width, height; +}; + +CONSTRUCTOR_IMPLEMENT(TYPE); + +void METHOD_ARG(TYPE, init, int l, int t, int w, int h) { + self->left = l; + self->top = t; + self->width = w; + self->height = h; +} + +int METHOD_ARG(TYPE, toString, char* buffer) { + return sprintf(buffer, NAMEOF(TYPE) " { left = %i, top = %i, width = %i, height = %i }", self->left, self->top, self->width, self->height); +} + +GETTER_IMPLEMENT(TYPE, int, left); +SETTER_IMPLEMENT(TYPE, int, left); + +GETTER_IMPLEMENT(TYPE, int, top); +SETTER_IMPLEMENT(TYPE, int, top); + +GETTER_IMPLEMENT(TYPE, int, width); +SETTER_IMPLEMENT(TYPE, int, width); + +GETTER_IMPLEMENT(TYPE, int, height); +SETTER_IMPLEMENT(TYPE, int, height); + +GETTER(TYPE, int, right) { + return self->left + self->width; +} +GETTER(TYPE, int, bottom) { + return self->top + self->height; +} + +#undef TYPE diff --git a/includes/rect.h b/includes/rect.h new file mode 100644 index 0000000..fca9602 --- /dev/null +++ b/includes/rect.h @@ -0,0 +1,32 @@ +#ifndef RECT_H +#define RECT_H + +#include "oo.h" + +#define TYPE Rect + +DEFINE_OBJECT(TYPE); + +CONSTRUCTOR(TYPE); + +void METHOD_ARG(TYPE, init, int l, int t, int w, int h); +int METHOD_ARG(TYPE, toString, char* buffer); + +GETTER(TYPE, int, left); +SETTER(TYPE, int, left); + +GETTER(TYPE, int, top); +SETTER(TYPE, int, top); + +GETTER(TYPE, int, width); +SETTER(TYPE, int, width); + +GETTER(TYPE, int, height); +SETTER(TYPE, int, height); + +GETTER(TYPE, int, right); +GETTER(TYPE, int, bottom); + +#undef TYPE + +#endif -- cgit v1.2.3