diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-16 19:41:27 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-16 19:41:27 -0600 |
commit | c60b3a96cf9bfedbfc402a50e0b85f22d231d67b (patch) | |
tree | 3f4ac9d76683047d8c002758d306487fa468951e /lib9o/include | |
parent | 46a33d3304ff01f431122a2849e86837c8f6d87d (diff) |
wip lib9o
Diffstat (limited to 'lib9o/include')
-rw-r--r-- | lib9o/include/lib9o/lib9o.h | 27 |
1 files changed, 27 insertions, 0 deletions
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_ */ |