Skip to content

Commit

Permalink
karm-base: Removed Reflect
Browse files Browse the repository at this point in the history
Let's wait for C++26 reflection instead.
  • Loading branch information
sleepy-monax committed Aug 5, 2024
1 parent b58b61c commit dad87eb
Show file tree
Hide file tree
Showing 21 changed files with 703 additions and 388 deletions.
2 changes: 1 addition & 1 deletion project.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"cute-engineering/ce-stdcpp": {
"git": "https://github.com/cute-engineering/ce-stdcpp.git",
"tag": "v1.2.0"
"tag": "v1.3.0"
},
"sdl2": {
"description": "A cross-platform development library designed to provide low level access to hardware",
Expand Down
127 changes: 127 additions & 0 deletions src/libs/karm-base/enum.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <karm-base/base.h>
#include <karm-base/string.h>
#include <karm-meta/traits.h>

namespace Karm {
Expand Down Expand Up @@ -165,4 +166,130 @@ constexpr U toUnderlyingType(E value) {
return (U)value;
};

// MARK: Utilities -------------------------------------------------------------

// HACK: Sometime they can be multiple enums in the same scope
// This allow the _LEN to not conflict
#define FOREACH_ENUM_LEN(LEN) \
LEN() \
LEN(0) \
LEN(1) \
LEN(2) \
LEN(3) \
LEN(4) \
LEN(5) \
LEN(6) \
LEN(7)

template <typename E>
concept BoundedEnum =
Meta::Enum<E> and
(requires { E::_MAX; } or
(
#define ITER(LEN) requires { E::_LEN##LEN; } or
FOREACH_ENUM_LEN(ITER)
#undef ITER
false
));

template <Meta::Enum E>
consteval Meta::UnderlyingType<E> enumMin() {
if constexpr (requires { E::_MIN; }) {
return toUnderlyingType(E::_MIN);
}
#define ITER(LEN) \
else if constexpr (requires { E::_LEN##LEN; }) { \
return Meta::UnderlyingType<E>{}; \
}
FOREACH_ENUM_LEN(ITER)
#undef ITER
else {
static_assert(false, "Enum must have _MIN or _LEN");
}
}

template <Meta::Enum E>
consteval Meta::UnderlyingType<E> enumMax() {
if constexpr (requires { E::_MAX; }) {
return toUnderlyingType(E::_MAX);
}
#define ITER(LEN) \
else if constexpr (requires { E::_LEN##LEN; }) { \
return toUnderlyingType(E::_LEN##LEN) - 1; \
}
FOREACH_ENUM_LEN(ITER)
#undef ITER
else {
static_assert(false, "Enum must have _MAX or _LEN");
}
}

template <Meta::Enum E>
consteval Meta::UnderlyingType<E> enumLen() {
#define ITER(LEN) \
if constexpr (requires { E::_LEN##LEN; }) { \
return toUnderlyingType(E::_LEN##LEN); \
} else
FOREACH_ENUM_LEN(ITER)
#undef ITER
return enumMax<E>() - enumMin<E>() + 1;
}

template <typename T>
static constexpr Str nameOf() {
Str v = __PRETTY_FUNCTION__;
auto start = indexOf(v, '=').take();
start += 2;
return sub(v, start, v.len() - 1);
}

template <Meta::Enum E, E V>
static constexpr Str nameOf() {
Str v = __PRETTY_FUNCTION__;
auto start = lastIndexOf(v, ':').take();
start += 1;
return sub(v, start, v.len() - 1);
}

template <Meta::Enum E>
struct _EnumItem {
Str name;
Meta::UnderlyingType<E> value;
};

template <Meta::Enum E, E V>
consteval auto _enumItem() {
return _EnumItem<E>{nameOf<E, V>(), toUnderlyingType(V)};
}

template <Meta::Enum E, Meta::UnderlyingType<E>... Vs>
static constexpr Array<_EnumItem<E>, sizeof...(Vs)> _enumItems(std::integer_sequence<Meta::UnderlyingType<E>, Vs...>) {
return {_enumItem<E, E(Vs + enumMin<E>())>()...};
}

template <Meta::Enum E>
static constexpr auto enumItems() {
return _enumItems<E>(std::make_integer_sequence<Meta::UnderlyingType<E>, enumLen<E>()>{});
}

template <Meta::Enum E>
static constexpr Str nameOf(E v) {
auto items = enumItems<E>();
for (auto &i : items) {
if (i.value == toUnderlyingType(v))
return i.name;
}
return "<unknown>";
}

template <Meta::Enum E>
static constexpr Opt<E> valueOf(Str name) {
auto items = enumItems<E>();
for (auto &i : items) {
if (i.name == name)
return E(i.value);
}
return NONE;
}

} // namespace Karm
205 changes: 0 additions & 205 deletions src/libs/karm-base/reflect.h

This file was deleted.

Loading

0 comments on commit dad87eb

Please sign in to comment.