summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-16 19:41:27 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2024-10-16 19:41:27 -0600
commitc60b3a96cf9bfedbfc402a50e0b85f22d231d67b (patch)
tree3f4ac9d76683047d8c002758d306487fa468951e
parent46a33d3304ff01f431122a2849e86837c8f6d87d (diff)
wip lib9o
-rw-r--r--lib9o/CMakeLists.txt8
-rw-r--r--lib9o/include/lib9o/lib9o.h27
2 files changed, 35 insertions, 0 deletions
diff --git a/lib9o/CMakeLists.txt b/lib9o/CMakeLists.txt
new file mode 100644
index 0000000..815ea57
--- /dev/null
+++ b/lib9o/CMakeLists.txt
@@ -0,0 +1,8 @@
+# lib9o/CMakeLists.txt - A simple Go-ish object system built on GCC -fplan9-extensions
+#
+# Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+# SPDX-Licence-Identifier: AGPL-3.0-or-later
+
+add_library(lib9o INTERFACE)
+target_include_directories(lib9o SYSTEM INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
+target_compile_options(lib9o PUBLIC "-fplan9-extension")
diff --git a/lib9o/include/lib9o/lib9o.h b/lib9o/include/lib9o/lib9o.h
new file mode 100644
index 0000000..ed22293
--- /dev/null
+++ b/lib9o/include/lib9o/lib9o.h
@@ -0,0 +1,27 @@
+/* lib9o.h - A simple Go-ish object system built on GCC -fplan9-extensions
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-Licence-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _LIB9O_H_
+#define _LIB9O_H_
+
+#include <assert.h> /* for assert() and static_assert() */
+#include <stddef.h> /* for offsetof() */
+
+#define MCALL(o, m, ...) \
+ ({ \
+ assert(o); \
+ (o)->vtable->m(o __VA_OPT__(,) __VA_ARGS__); \
+ })
+
+#define MSELF(obj_typ, iface_typ, iface_ptr) \
+ ({ \
+ static_assert(_Generic(iface_ptr, iface_typ *: 1, default: 0), \
+ "typeof("#iface_ptr") != "#iface_typ); \
+ assert(iface_ptr); \
+ ((obj_typ*)(((void*)iface_ptr)-offsetof(obj_typ,iface_typ))); \
+ })
+
+#endif /* _LIB9O_H_ */