summaryrefslogtreecommitdiff
path: root/libmisc
diff options
context:
space:
mode:
Diffstat (limited to 'libmisc')
-rw-r--r--libmisc/include/libmisc/private.h37
1 files changed, 37 insertions, 0 deletions
diff --git a/libmisc/include/libmisc/private.h b/libmisc/include/libmisc/private.h
new file mode 100644
index 0000000..0eb8c42
--- /dev/null
+++ b/libmisc/include/libmisc/private.h
@@ -0,0 +1,37 @@
+/* libmisc/private.h - Utilities to hide struct fields
+ *
+ * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#ifndef _LIBMISC_CPPUTIL_H_
+#define _LIBMISC_CPPUTIL_H_
+
+/* primitive utilities */
+
+#define _SECOND(a, b, ...) b
+
+#define _CAT(a, b) a ## b
+#define _CAT2(a, b) _CAT(a, b)
+#define _EAT(...)
+#define _EXPAND(...) __VA_ARGS__
+
+/* conditionals */
+
+#define _T xxTxx
+#define _F xxFxx
+
+#define _SENTINEL() bogus, _T /* a magic sentinel value */
+#define _IS_SENTINEL(...) _SECOND(__VA_ARGS__, _F)
+
+#define _IF(cond) _CAT(_IF__, cond) /* _IF(cond)(then)(else) */
+#define _IF__xxTxx(...) __VA_ARGS__ _EAT
+#define _IF__xxFxx(...) _EXPAND
+
+/* high-level functionality */
+
+#define YES _SENTINEL()
+#define BEGIN_PRIVATE(name) _IF(_IS_SENTINEL(IMPLEMENTATION_FOR_##name))()(struct {)
+#define END_PRIVATE(name) _IF(_IS_SENTINEL(IMPLEMENTATION_FOR_##name))()(} _CAT2(_HIDDEN_, __COUNTER__);)
+
+#endif /* _LIBMISC_CPPUTIL_H_ */