aboutsummaryrefslogtreecommitdiff
path: root/src/square.c
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/square.c
Init commit.HEADmaster
Diffstat (limited to 'src/square.c')
-rw-r--r--src/square.c49
1 files changed, 49 insertions, 0 deletions
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