aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSopár Adrián <dev.adrian.sopar@protonmail.com>2023-01-27 00:05:24 +0100
committerSopár Adrián <dev.adrian.sopar@protonmail.com>2023-01-27 00:05:24 +0100
commit2c5a5f7679a5f450e483fdedc99c80622f260a4d (patch)
treec96f4f9020275e8853c92f008fa9f041e0529a57 /src
Init commit.HEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/main.c24
-rw-r--r--src/shape.c32
-rw-r--r--src/shape.h21
-rw-r--r--src/square.c49
-rw-r--r--src/square.h16
5 files changed, 142 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c
new file mode 100644
index 0000000..4386a05
--- /dev/null
+++ b/src/main.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+#include "square.h"
+
+void print_square(struct square *square)
+{
+ double area = square_area(square);
+ printf("square_area=%f\n", area);
+}
+
+void print_shape(struct shape *shape)
+{
+ double area = shape_area(shape);
+ printf("shape_area=%f\n", area);
+}
+
+int main(int argc, char **argv)
+{
+ struct square *square = square_create(5);
+ print_square(square);
+
+ print_shape(square_as_shape(square));
+
+ shape_destroy(square_as_shape(square));
+}
diff --git a/src/shape.c b/src/shape.c
new file mode 100644
index 0000000..cb72c05
--- /dev/null
+++ b/src/shape.c
@@ -0,0 +1,32 @@
+#include <stdlib.h>
+#include "shape.h"
+
+struct shape
+{
+ shape_area_t area;
+ shape_destroy_t destroy;
+};
+
+double shape_area(struct shape *shape)
+{
+ return shape->area(shape);
+}
+
+void shape_destroy(struct shape *shape)
+{
+ shape->destroy(shape);
+}
+
+void shape_init(
+ struct shape *shape,
+ shape_area_t area,
+ shape_destroy_t destroy)
+{
+ shape->area = area;
+ shape->destroy = destroy;
+}
+
+int shape_sizeof()
+{
+ return sizeof(struct shape);
+} \ No newline at end of file
diff --git a/src/shape.h b/src/shape.h
new file mode 100644
index 0000000..641b440
--- /dev/null
+++ b/src/shape.h
@@ -0,0 +1,21 @@
+#ifndef POC__SHAPE_H
+#define POC__SHAPE_H
+
+struct shape;
+
+/* Calculates the area of the shape and returns the value. */
+double shape_area(struct shape *shape);
+typedef double (*shape_area_t)(struct shape *);
+
+/* Deallocates the object. */
+void shape_destroy(struct shape *shape);
+typedef void (*shape_destroy_t)(struct shape *);
+
+void shape_init(
+ struct shape *shape,
+ shape_area_t area,
+ shape_destroy_t destroy);
+
+int shape_sizeof();
+
+#endif
diff --git a/src/square.c b/src/square.c
new file mode 100644
index 0000000..8aef283
--- /dev/null
+++ b/src/square.c
@@ -0,0 +1,49 @@
+#include <stdlib.h>
+#include <stddef.h>
+#include "square.h"
+
+struct square
+{
+ double width;
+};
+
+/* Shape interface */
+
+double shape_area_impl(struct shape *shape)
+{
+ struct square *square = ((void *)shape) + shape_sizeof();
+ return square_area(square);
+}
+
+void shape_destroy_impl(struct shape *shape)
+{
+ struct square *square = ((void *)shape) + shape_sizeof();
+ square_destroy(square);
+}
+
+struct shape *square_as_shape(struct square *square)
+{
+ return ((void *)square) - shape_sizeof();
+}
+
+/* End of shape interface */
+
+struct square *square_create(double width)
+{
+ struct shape *shape = malloc(shape_sizeof() + sizeof(struct square));
+ struct square *square = ((void *)shape) + shape_sizeof();
+ square->width = width;
+
+ shape_init(shape, shape_area_impl, shape_destroy_impl);
+ return square;
+}
+
+double square_area(struct square *square)
+{
+ return square->width * square->width;
+}
+
+void square_destroy(struct square *square)
+{
+ free(((void *)square) - shape_sizeof());
+} \ No newline at end of file
diff --git a/src/square.h b/src/square.h
new file mode 100644
index 0000000..177c9c7
--- /dev/null
+++ b/src/square.h
@@ -0,0 +1,16 @@
+#ifndef POC__SQUARE_H
+#define POC__SQUARE_H
+
+#include "shape.h"
+
+struct square;
+
+struct square *square_create(double width);
+
+double square_area(struct square *square);
+
+void square_destroy(struct square *square);
+
+struct shape *square_as_shape(struct square *square);
+
+#endif