summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Kohl <gitlab@jonaskohl.de>2024-08-29 17:00:11 +0200
committerJonas Kohl <gitlab@jonaskohl.de>2024-08-29 17:00:11 +0200
commit84ad096896e9948c722a957d07ed6868e252a9f7 (patch)
treeb8ad1afadd89f1dc7599436cfb0f7f5fb0c177b6
Initial commit
-rw-r--r--.gitignore3
-rwxr-xr-xbuild.sh12
-rw-r--r--includes/oo.h54
-rw-r--r--includes/point.c31
-rw-r--r--includes/point.h27
-rw-r--r--includes/rect.c43
-rw-r--r--includes/rect.h32
-rw-r--r--main.c30
8 files changed, 232 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..abe17bb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.DS_Store
+*.DS_Store
+/build/*
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..d2cd839
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+mkdir -p build
+
+rm -rf build/*
+
+gcc -o build/main main.c includes/*.c -Werror=incompatible-pointer-types
+
+if [ "$1" = "--run" ]; then
+ chmod +x ./build/main
+ ./build/main
+fi
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 <stdlib.h>
+#include <stdio.h>
+#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 <stdlib.h>
+#include <stdio.h>
+#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
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..535a0b4
--- /dev/null
+++ b/main.c
@@ -0,0 +1,30 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "includes/point.h"
+#include "includes/rect.h"
+
+int main(int argc, char** argv) {
+ Point point = Point_alloc();
+ Point_init(point, 0, 0);
+ Rect rect = Rect_alloc();
+ Rect_init(rect, 4, 5, 120, 90);
+
+ char buffer [64];
+
+ Rect_toString(rect, buffer);
+ printf("Rect: %s\n", buffer);
+ printf(" right = %i, bottom = %i\n", Rect_right_get(rect), Rect_bottom_get(rect));
+
+ Point_toString(point, buffer);
+ printf("Point (1): %s\n", buffer);
+
+ Point_init(point, 25, -3.5f);
+
+ Point_toString(point, buffer);
+ printf("Point (2): %s\n", buffer);
+
+ free(point);
+ free(rect);
+
+ return 0;
+}