basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
commit | a58fad2587466536fbbb95a9b054c5f1d25f594c |
parent | f9c399b8c96e67feae74e1358418999a2937f8d0 |
author | Dimitrije Dobrota < mail@dimitrijedobrota.com > |
date | Mon, 5 May 2025 09:21:13 +0200 |
Enum array redability improvement
M | include/based/enum.hpp | | | ++++++++++++++ -------- |
M | include/based/macros.hpp | | | +++ --- |
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/ include/based/enum.hpp b/ include/based/enum.hpp
@@ -36,6 +36,7 @@
#define BASED_DETAIL_ENUM_DEFINE_GET(Qualifier, Type, ...) \
inline const Qualifier::type& Qualifier::type::get(Type idx) \
{ \
/* NOLINTNEXTLINE(*paths-covered*) */ \
switch (idx) { \
BASED_FOREACH_1(Qualifier, BASED_DETAIL_ENUM_DECLARE_CASE, __VA_ARGS__) \
default: \
@@ -49,30 +50,35 @@
BASED_DETAIL_ENUM_DEFINE_NAMES(Qualifier, __VA_ARGS__) \
BASED_DETAIL_ENUM_DEFINE_GET(Qualifier, Type, __VA_ARGS__)
#define BASED_ENUM_DEFINE_ARRAY(Name) \
#define BASED_ENUM_DECLARE_ARRAY(Name) \
template<typename T> \
class array : public std::array<T, Name::type::size> \
{ \
using std::array<T, Name::type::size>::operator[]; \
using base = std::array<T, Name::type::size>; \
using base::operator[]; \
\
public: \
constexpr array() = default; \
constexpr array() noexcept \
: base() \
{ \
} \
\
template<class... Args> \
requires(sizeof...(Args) == Name::type::size) \
constexpr explicit array(Args&&... args) /* NOLINTNEXTLINE(*decay*) */ \
: std::array<T, Name::type::size>({std::forward<Args>(args)...}) \
constexpr explicit array(Args&&... args \
) noexcept /* NOLINTNEXTLINE(*decay*) */ \
: base({std::forward<Args>(args)...}) \
{ \
} \
\
const T& operator[](Name::type val) const \
{ \
return std::array<T, Name::type::size>::operator[](val.value); \
return base::operator[](val.value); \
} \
\
T& operator[](Name::type val) \
{ \
return std::array<T, Name::type::size>::operator[](val.value); \
return base::operator[](val.value); \
} \
};
@@ -93,7 +99,7 @@
static constexpr size_t size = \
BASED_NUMARGS(BASED_LIST_STR(__VA_ARGS__)); \
\
BASED_ENUM_DEFINE_ARRAY(Name) \
BASED_ENUM_DECLARE_ARRAY(Name) \
static const array<const char*> names; \
\
static const type& get(Type idx); \
diff --git a/ include/based/macros.hpp b/ include/based/macros.hpp
@@ -2,7 +2,7 @@
// NOLINTBEGIN(*macro-usage*)
#define BASED_NUMARGS(...) (std::array{__VA_ARGS__}.size())
#define BASED_NUMARGS(...) (std::array {__VA_ARGS__}.size())
// clang-format off
#define BASED_GET_MACRO( \
@@ -55,9 +55,9 @@
#define BASED_FE_15_1(First, WHAT, X, ...) WHAT(First, X, 14) BASED_FE_14_1(First, WHAT, __VA_ARGS__)
// clang-format on
#define BASED_FOREACH_1(First, action, ...) \
#define BASED_FOREACH_1(First, action, ...) \
BASED_GET_MACRO(_0, __VA_ARGS__, BASED_FE_15_1, BASED_FE_14_1, BASED_FE_13_1, BASED_FE_12_1, BASED_FE_11_1, BASED_FE_10_1, BASED_FE_9_1, BASED_FE_8_1, BASED_FE_7_1, BASED_FE_6_1, BASED_FE_5_1, BASED_FE_4_1, BASED_FE_3_1, BASED_FE_2_1, BASED_FE_1_1, BASED_FE_0_1)( \
First, action, __VA_ARGS__ \
First, action, __VA_ARGS__ \
)
// clang-format off