1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
|
/*
MIT License
Copyright (c) 2021-2025 hirrolot
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// The official repository: <https://github.com/hirrolot/interface99>.
#ifndef INTERFACE99_H
#define INTERFACE99_H
#include <metalang99.h>
#if !ML99_VERSION_COMPATIBLE(1, 13, 2)
#error Please, update Metalang99 to v1.13.2 or later.
#endif
#ifndef IFACE99_NO_ALIASES
#define interface(iface) interface99(iface)
#define impl(iface, implementer) impl99(iface, implementer)
#define implExtern(iface, implementer) implExtern99(iface, implementer)
#define declImpl(iface, implementer) declImpl99(iface, implementer)
#define declImplExtern(iface, implementer) declImplExtern99(iface, implementer)
#define vfunc(ret_ty, name, ...) vfunc99(ret_ty, name, __VA_ARGS__)
#define vfuncDefault(ret_ty, name, ...) vfuncDefault99(ret_ty, name, __VA_ARGS__)
#define VCALL(obj, ...) VCALL99(obj, __VA_ARGS__)
#define VCALL_OBJ(obj, ...) VCALL_OBJ99(obj, __VA_ARGS__)
#define VCALL_SUPER(obj, superiface, ...) VCALL_SUPER99(obj, superiface, __VA_ARGS__)
#define VCALL_SUPER_OBJ(obj, superiface, ...) VCALL_SUPER_OBJ99(obj, superiface, __VA_ARGS__)
#define DYN(implementer, iface, ...) DYN99(implementer, iface, __VA_ARGS__)
#define DYN_LIT(implementer, iface, ...) DYN_LIT99(implementer, iface, __VA_ARGS__)
#define VTABLE(implementer, iface) VTABLE99(implementer, iface)
#define VSelf VSelf99
#define VSELF(T) VSELF99(T)
#endif // IFACE99_NO_ALIASES
// Public stuff {
// Metalang99-compliant macros {
#define IFACE99_interface(iface) ML99_call(IFACE99_interface, iface)
#define IFACE99_impl(iface, implementer) ML99_call(IFACE99_impl, iface, implementer)
#define IFACE99_implExtern(iface, implementer) ML99_call(IFACE99_implExtern, iface, implementer)
#define interface99(iface) ML99_EVAL(IFACE99_interface_IMPL(iface))
#define impl99(iface, implementer) ML99_EVAL(IFACE99_impl_IMPL(iface, implementer))
#define implExtern99(iface, implementer) ML99_EVAL(IFACE99_implExtern_IMPL(iface, implementer))
// } (Metalang99-compliant macros)
#define vfunc99(ret_ty, name, ...) ML99_CHOICE(vfunc, ret_ty, name, __VA_ARGS__)
#define vfuncDefault99(ret_ty, name, ...) ML99_CHOICE(vfuncDefault, ret_ty, name, __VA_ARGS__)
#define DYN99(implementer, iface, ...) \
((iface){.self = (void *)(__VA_ARGS__), .vptr = &VTABLE99(implementer, iface)})
#define DYN_LIT99(implementer, iface, ...) DYN99(implementer, iface, &(implementer)__VA_ARGS__)
#define VTABLE99(implementer, iface) ML99_CAT4(implementer, _, iface, _impl)
#define VSelf99 void *restrict iface99_self
// clang-format off
#define VSELF99(T) T *restrict self = (T *restrict)(iface99_self)
// clang-format on
#define IFACE99_MAJOR 1
#define IFACE99_MINOR 0
#define IFACE99_PATCH 2
#define IFACE99_VERSION_COMPATIBLE(x, y, z) \
(IFACE99_MAJOR == (x) && \
((IFACE99_MINOR == (y) && IFACE99_PATCH >= (z)) || (IFACE99_MINOR > (y))))
#define IFACE99_VERSION_EQ(x, y, z) \
(IFACE99_MAJOR == (x) && IFACE99_MINOR == (y) && IFACE99_PATCH == (z))
// } (Public stuff)
// Interface generation {
#define IFACE99_interface_IMPL(iface) \
ML99_TERMS( \
v(typedef struct iface##VTable iface##VTable;), \
v(typedef struct iface iface;), \
ML99_semicoloned(ML99_struct(v(iface##VTable), IFACE99_PRIV_genVTableFields(iface))), \
v(struct iface { \
void *self; \
const iface##VTable *vptr; \
}))
/*
* // Only if <iface> is a marker interface without superinterfaces:
* char dummy;
*
* <func-ret-ty>0 (*<func-name>0)(<func-params>0);
* ...
* <func-ret-ty>N (*<func-name>N)(<func-params>N);
*
* const <requirement>0VTable *<requirement>;
* ...
* const <requirement>NVTable *<requirement>;
*/
#define IFACE99_PRIV_genVTableFields(iface) \
ML99_uncomma(ML99_QUOTE( \
IFACE99_PRIV_genDummy(iface), \
ML99_IF( \
IFACE99_PRIV_IS_MARKER_IFACE(iface), \
ML99_empty(), \
IFACE99_PRIV_genFuncPtrForEach(iface)), \
ML99_IF( \
IFACE99_PRIV_IS_SUB_IFACE(iface), \
IFACE99_PRIV_genRequirementForEach(iface), \
ML99_empty())))
#define IFACE99_PRIV_genDummy(iface) \
ML99_IF(IFACE99_PRIV_IS_EMPTY_VTABLE(iface), v(char dummy;), ML99_empty())
/*
* <func-ret-ty>0 (*<func-name>0)(<func-params>0);
* ...
* <func-ret-ty>N (*<func-name>N)(<func-params>N);
*/
#define IFACE99_PRIV_genFuncPtrForEach(iface) \
ML99_seqForEach(v(IFACE99_PRIV_genFuncPtr), v(iface##_IFACE))
#define IFACE99_PRIV_genFuncPtr_IMPL(_tag, ret_ty, name, ...) v(ret_ty (*name)(__VA_ARGS__);)
/*
* const <requirement>0VTable *<requirement>;
* ...
* const <requirement>NVTable *<requirement>;
*/
#define IFACE99_PRIV_genRequirementForEach(iface) \
ML99_tupleForEach(v(IFACE99_PRIV_genRequirement), v(iface##_EXTENDS))
#define IFACE99_PRIV_genRequirement_IMPL(requirement) v(const requirement##VTable *requirement;)
// } (Interface generation)
// Interface implementation generation {
#define IFACE99_impl_IMPL(iface, implementer) \
IFACE99_PRIV_implCommon(IFACE99_PRIV_STORAGE_CLASS_STATIC, iface, implementer)
#define IFACE99_implExtern_IMPL(iface, implementer) \
IFACE99_PRIV_implCommon(IFACE99_PRIV_STORAGE_CLASS_EXTERN, iface, implementer)
#define IFACE99_PRIV_STORAGE_CLASS_STATIC static
#define IFACE99_PRIV_STORAGE_CLASS_EXTERN /* If no storage-class specifier is provided, the \
default for objects is `extern` (file scope) or \
`auto` (block scope). */
#define IFACE99_PRIV_implCommon(storage_class, iface, implementer) \
ML99_assignInitializerList( \
v(storage_class const iface##VTable VTABLE99(implementer, iface)), \
IFACE99_PRIV_genImplInitList(iface, implementer))
/*
* // Only if <iface> is a marker interface without superinterfaces:
* .dummy = '\0',
*
* <func-name>0 = either <implementer>_<func-name>0 or <iface>_<func-name>0,
* ...
* <func-name>N = either <implementer>_<func-name>N or <iface>_<func-name>N,
*
* <requirement>0 = &VTABLE(<implementer>, <requirement>0),
* ...
* <requirement>N = &VTABLE(<implementer>, <requirement>N),
*/
#define IFACE99_PRIV_genImplInitList(iface, implementer) \
ML99_uncomma(ML99_QUOTE( \
IFACE99_PRIV_genDummyInit(iface), \
ML99_IF( \
IFACE99_PRIV_IS_MARKER_IFACE(iface), \
ML99_empty(), \
IFACE99_PRIV_genImplFuncNameForEach(iface, implementer)), \
ML99_IF( \
IFACE99_PRIV_IS_SUB_IFACE(iface), \
IFACE99_PRIV_genRequirementsImplForEach(iface, implementer), \
ML99_empty())))
#define IFACE99_PRIV_genDummyInit(iface) \
ML99_IF(IFACE99_PRIV_IS_EMPTY_VTABLE(iface), v(.dummy = '\0'), ML99_empty())
/*
* <func-name>0 = either <implementer>_<func-name>0 or <iface>_<func-name>0,
* ...
* <func-name>N = either <implementer>_<func-name>N or <iface>_<func-name>N,
*/
#define IFACE99_PRIV_genImplFuncNameForEach(iface, implementer) \
ML99_seqForEach( \
ML99_appl(v(IFACE99_PRIV_genImplFuncName), v(iface, implementer)), \
v(iface##_IFACE))
#define IFACE99_PRIV_genImplFuncName_IMPL(iface, implementer, tag, _ret_ty, name, ...) \
ML99_match(ML99_choice(v(tag), v(iface, implementer, name)), v(IFACE99_PRIV_genImpl_))
#define IFACE99_PRIV_genImpl_vfunc_IMPL(_iface, implementer, name) v(.name = implementer##_##name, )
#define IFACE99_PRIV_genImpl_vfuncDefault_IMPL(iface, implementer, name) \
ML99_IF( \
IFACE99_PRIV_IS_CUSTOM(implementer, name), \
IFACE99_PRIV_genImpl_vfunc_IMPL(~, implementer, name), \
v(.name = iface##_##name, ))
/*
* <requirement>0 = &VTABLE(<implementer>, <requirement>0),
* ...
* <requirement>N = &VTABLE(<implementer>, <requirement>N),
*/
#define IFACE99_PRIV_genRequirementsImplForEach(iface, implementer) \
ML99_tupleForEach( \
ML99_appl(v(IFACE99_PRIV_genRequirementImpl), v(implementer)), \
v(iface##_EXTENDS))
#define IFACE99_PRIV_genRequirementImpl_IMPL(implementer, requirement) \
v(.requirement = &VTABLE99(implementer, requirement), )
// } (Interface implementation generation)
// Implementation declaration {
#define declImpl99(iface, implementer) static IFACE99_PRIV_DECL_IMPL_COMMON(iface, implementer)
#define declImplExtern99(iface, implementer) \
extern IFACE99_PRIV_DECL_IMPL_COMMON(iface, implementer)
#define IFACE99_PRIV_DECL_IMPL_COMMON(iface, implementer) \
const ML99_CAT(iface, VTable) VTABLE99(implementer, iface)
// } (Implementation declaration)
// Virtual calls {
#define VCALL99(obj, ...) \
((obj).vptr->IFACE99_PRIV_VCALL_OVERLOAD(__VA_ARGS__)((obj).self, __VA_ARGS__))
#define VCALL_OBJ99(obj, ...) \
((obj).vptr->IFACE99_PRIV_VCALL_OVERLOAD(__VA_ARGS__)((obj), __VA_ARGS__))
#define VCALL_SUPER99(obj, superiface, ...) \
((obj).vptr->superiface->IFACE99_PRIV_VCALL_OVERLOAD(__VA_ARGS__)((obj).self, __VA_ARGS__))
#define VCALL_SUPER_OBJ99(obj, superiface, ...) \
((obj).vptr->superiface->IFACE99_PRIV_VCALL_OVERLOAD(__VA_ARGS__)( \
((superiface){ \
.self = (obj).self, \
.vptr = (obj).vptr->superiface, \
}), \
__VA_ARGS__))
#define IFACE99_PRIV_VCALL_OVERLOAD(...) \
ML99_CAT(IFACE99_PRIV_VCALL_, ML99_VARIADICS_IS_SINGLE(__VA_ARGS__))
#define IFACE99_PRIV_VCALL_1(obj, func_name) func_name(obj)
#define IFACE99_PRIV_VCALL_0(obj, func_name, ...) func_name(obj, __VA_ARGS__)
// } (Virtual calls)
// Various predicates {
#define IFACE99_PRIV_IS_EMPTY_VTABLE(iface) \
ML99_AND(IFACE99_PRIV_IS_MARKER_IFACE(iface), ML99_NOT(IFACE99_PRIV_IS_SUB_IFACE(iface)))
#define IFACE99_PRIV_IS_MARKER_IFACE(iface) ML99_SEQ_IS_EMPTY(iface##_IFACE)
#define IFACE99_PRIV_IS_SUB_IFACE(iface) ML99_IS_TUPLE(iface##_EXTENDS)
#define IFACE99_PRIV_IS_CUSTOM(implementer, func_name) \
ML99_IS_TUPLE(implementer##_##func_name##_CUSTOM)
// } (Various predicates)
// Arity specifiers {
#define IFACE99_PRIV_genRequirement_ARITY 1
#define IFACE99_PRIV_genRequirementImpl_ARITY 2
#define IFACE99_PRIV_genFuncPtr_ARITY 1
#define IFACE99_PRIV_genImplFuncName_ARITY 2
// Public:
#define IFACE99_interface_ARITY 1
#define IFACE99_impl_ARITY 2
#define IFACE99_implExtern_ARITY 2
// } (Arity specifiers)
#endif // INTERFACE99_H
|