diff options
-rw-r--r-- | lib9o/CMakeLists.txt | 8 | ||||
-rw-r--r-- | lib9o/include/lib9o/lib9o.h | 27 |
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_ */ |