|
| 1 | +#include <stddef.h> |
| 2 | +#include <stdint.h> |
| 3 | + |
| 4 | +#include <iostream> |
| 5 | +#include <tuple> |
| 6 | + |
| 7 | +/* --- Eval ----------------------------------------------------------------- */ |
| 8 | + |
| 9 | +#define __eval0(...) __VA_ARGS__ |
| 10 | +#define __eval1(...) __eval0(__eval0(__eval0(__VA_ARGS__))) |
| 11 | +#define __eval2(...) __eval1(__eval1(__eval1(__VA_ARGS__))) |
| 12 | +#define __eval3(...) __eval2(__eval2(__eval2(__VA_ARGS__))) |
| 13 | +#define __eval4(...) __eval3(__eval3(__eval3(__VA_ARGS__))) |
| 14 | + |
| 15 | +#define eval$(...) __eval4(__eval4(__eval4(__VA_ARGS__))) |
| 16 | + |
| 17 | +/* --- Map ------------------------------------------------------------------ */ |
| 18 | + |
| 19 | +#define __MAP_END__(...) |
| 20 | +#define __MAP_OUT__ |
| 21 | +#define __MAP_COMMA__ , |
| 22 | + |
| 23 | +#define __mapGetEnd2() 0, __MAP_END__ |
| 24 | +#define __mapGetEnd1(...) __mapGetEnd2 |
| 25 | +#define __mapGetEnd(...) __mapGetEnd1 |
| 26 | + |
| 27 | +#define __mapNext0(test, next, ...) next __MAP_OUT__ |
| 28 | +#define __mapNext1(test, next) __mapNext0(test, next, 0) |
| 29 | +#define __mapNext(test, next) __mapNext1(__mapGetEnd test, next) |
| 30 | + |
| 31 | +#define __map0(f, x, peek, ...) \ |
| 32 | + f(x) __mapNext(peek, __map1)(f, peek, __VA_ARGS__) |
| 33 | +#define __map1(f, x, peek, ...) \ |
| 34 | + f(x) __mapNext(peek, __map0)(f, peek, __VA_ARGS__) |
| 35 | + |
| 36 | +#define __mapListNext1(test, next) __mapNext0(test, __MAP_COMMA__ next, 0) |
| 37 | +#define __mapListNext(test, next) __mapListNext1(__mapGetEnd test, next) |
| 38 | + |
| 39 | +#define __mapList0(f, x, peek, ...) \ |
| 40 | + f(x) __mapListNext(peek, __mapList1)(f, peek, __VA_ARGS__) |
| 41 | +#define __mapList1(f, x, peek, ...) \ |
| 42 | + f(x) __mapListNext(peek, __mapList0)(f, peek, __VA_ARGS__) |
| 43 | + |
| 44 | +// Applies the function macro `f` to each of the remaining parameters. |
| 45 | +#define map$(f, ...) eval$(__map1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) |
| 46 | + |
| 47 | +// Applies the function macro `f` to each of the remaining parameters and |
| 48 | +// inserts commas between the results. |
| 49 | +#define mapList$(f, ...) \ |
| 50 | + eval$(__mapList1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) |
| 51 | + |
| 52 | +namespace Karm { |
| 53 | + |
| 54 | +using usize = size_t; |
| 55 | +using isize = ptrdiff_t; |
| 56 | + |
| 57 | +using u8 = uint8_t; |
| 58 | +using u16 = uint16_t; |
| 59 | +using u32 = uint32_t; |
| 60 | +using u64 = uint64_t; |
| 61 | +using u128 = __uint128_t; |
| 62 | + |
| 63 | +using i8 = int8_t; |
| 64 | +using i16 = int16_t; |
| 65 | +using i32 = int32_t; |
| 66 | +using i64 = int64_t; |
| 67 | +using i128 = __int128_t; |
| 68 | + |
| 69 | +using f16 = __fp16; |
| 70 | +using f32 = float; |
| 71 | +using f64 = double; |
| 72 | +using f128 = long double; |
| 73 | + |
| 74 | +using Str = std::string_view; |
| 75 | + |
| 76 | +template <auto N> struct StrLit { |
| 77 | + char _buf[N]; |
| 78 | + constexpr StrLit(char const (&buf)[N]) { |
| 79 | + for (usize i = 0; i < N; i++) |
| 80 | + _buf[i] = buf[i]; |
| 81 | + } |
| 82 | + constexpr operator char const *() { return _buf; } |
| 83 | +}; |
| 84 | + |
| 85 | +template <typename T, typename FT, StrLit N, FT T::*P> struct _Field { |
| 86 | + using Type = FT; |
| 87 | + static constexpr char const *NAME = N; |
| 88 | + static constexpr auto PTR = P; |
| 89 | + |
| 90 | + static auto &access(T &t) { return t.*PTR; } |
| 91 | + |
| 92 | + static auto const &access(T const &t) { return t.*PTR; } |
| 93 | +}; |
| 94 | + |
| 95 | +template <typename T, StrLit N> struct _Type { |
| 96 | + using Type = T; |
| 97 | + static constexpr char const *NAME = N; |
| 98 | +}; |
| 99 | + |
| 100 | +template <typename T> struct Reflect; |
| 101 | + |
| 102 | +template <typename T> static constexpr auto nameOf() { |
| 103 | + Str v = __PRETTY_FUNCTION__; |
| 104 | + auto start = v.find('='); |
| 105 | + start += 2; |
| 106 | + return v.substr(start, v.length() - start - 1); |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace Karm |
| 110 | + |
| 111 | +#define __reflectableField(F) \ |
| 112 | + Karm::_Field<Type, decltype(std::declval<Type>().F), #F, &Type::F> |
| 113 | + |
| 114 | +#define __reflectableFields(...) \ |
| 115 | + std::tuple<mapList$(__reflectableField, __VA_ARGS__)> |
| 116 | + |
| 117 | +#define __reflectableType(T, ...) \ |
| 118 | + struct Karm::Reflect<T> { \ |
| 119 | + using Type = T; \ |
| 120 | + static constexpr auto NAME = Karm::nameOf<T>(); \ |
| 121 | + using Fields = __reflectableFields(__VA_ARGS__); \ |
| 122 | + } |
| 123 | + |
| 124 | +#define Reflectable$(T, ...) \ |
| 125 | + template <> __reflectableType(T __VA_OPT__(, ) __VA_ARGS__) |
| 126 | + |
| 127 | +#define ReflectableTemplate$(T, ...) \ |
| 128 | + __reflectableType(T __VA_OPT__(, ) __VA_ARGS__) |
| 129 | + |
| 130 | +template <typename T> struct Vec { |
| 131 | + T x, y, z; |
| 132 | +}; |
| 133 | + |
| 134 | +template <typename T> ReflectableTemplate$(Vec<T>, x, y, z); |
| 135 | + |
| 136 | +int main() { |
| 137 | + std::cout << "'" << Karm::Reflect<Vec<int>>::NAME << "'" << std::endl; |
| 138 | + return 0; |
| 139 | +} |
0 commit comments