Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ability to define min max as an enum value #4

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions include/mgutility/reflection/detail/enum_for_each.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in include/mgutility/reflection/detail/enum_for_each.hpp

View check run for this annotation

Trunk.io / Trunk Check

clang-format

Incorrect formatting, autoformat by running 'trunk fmt'
MIT License

Copyright (c) 2024 mguludag
Expand Down Expand Up @@ -74,7 +74,7 @@
* @brief An iterator for enum values.
*/
struct enum_iter {
using const_iter_type = decltype(enum_range<Enum>::min);
using const_iter_type = int;
using iter_type = detail::remove_const_t<const_iter_type>;
using iterator_category = std::forward_iterator_tag;
using value_type = const detail::enum_pair<Enum>;
Expand Down Expand Up @@ -172,12 +172,12 @@
* @return The size of the enum range.
*/
auto size() -> std::size_t {
return enum_range<Enum>::max - enum_range<Enum>::min;
return static_cast<int>(enum_range<Enum>::max) - static_cast<int>(enum_range<Enum>::min) + 1;
}

private:
enum_iter m_begin{enum_range<Enum>::min}; /**< The beginning iterator. */
enum_iter m_end{enum_range<Enum>::max}; /**< The end iterator. */
enum_iter m_begin{static_cast<int>(enum_range<Enum>::min)}; /**< The beginning iterator. */
enum_iter m_end{static_cast<int>(enum_range<Enum>::max) + 1}; /**< The end iterator. */
};
} // namespace mgutility

Expand Down
2 changes: 1 addition & 1 deletion include/mgutility/reflection/detail/enum_name_impl.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in include/mgutility/reflection/detail/enum_name_impl.hpp

View check run for this annotation

Trunk.io / Trunk Check

clang-format

Incorrect formatting, autoformat by running 'trunk fmt'
MIT License

Copyright (c) 2024 mguludag
Expand Down Expand Up @@ -179,7 +179,7 @@
template <typename Enum, int Min = mgutility::enum_range<Enum>::min,
int Max = mgutility::enum_range<Enum>::max>
MGUTILITY_CNSTXPR inline auto get_enum_array() noexcept
-> std::array<mgutility::string_view, Max - Min + 1> {
-> std::array<mgutility::string_view, Max - Min + 2> {
return get_enum_array<Enum>(detail::make_enum_sequence<Enum, Min, Max>());
}

Expand Down
2 changes: 1 addition & 1 deletion include/mgutility/reflection/detail/meta.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in include/mgutility/reflection/detail/meta.hpp

View check run for this annotation

Trunk.io / Trunk Check

clang-format

Incorrect formatting, autoformat by running 'trunk fmt'
MIT License

Copyright (c) 2024 mguludag
Expand Down Expand Up @@ -145,7 +145,7 @@
* @tparam Max The maximum value in the sequence.
*/
template <typename Enum, int Min, int Max>
using make_enum_sequence = typename enum_sequence_helper<Enum, Min, Max>::type;
using make_enum_sequence = typename enum_sequence_helper<Enum, Min, Max + 1>::type;
} // namespace detail

/**
Expand Down
37 changes: 18 additions & 19 deletions include/mgutility/reflection/enum_name.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*

Check failure on line 1 in include/mgutility/reflection/enum_name.hpp

View check run for this annotation

Trunk.io / Trunk Check

clang-format

Incorrect formatting, autoformat by running 'trunk fmt'
MIT License

Copyright (c) 2023 mguludag
Expand Down Expand Up @@ -27,7 +27,6 @@

#include "detail/enum_name_impl.hpp"


namespace mgutility {

/**
Expand Down Expand Up @@ -56,7 +55,7 @@
template <int Min, int Max, typename Enum>
MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept
-> detail::string_or_view_t<Enum> {
static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
static_assert(Min < Max, "Max must be greater than Min!");
static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
return detail::enum_name_impl<Enum, Min, Max>(enumValue);
}
Expand All @@ -70,11 +69,11 @@
* @param e The enum value.
* @return A string view or string representing the name of the enum value.
*/
template <typename Enum, int Min = enum_range<Enum>::min,
int Max = enum_range<Enum>::max>
template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
int Max = static_cast<int>(enum_range<Enum>::max)>
MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept
-> detail::string_or_view_t<Enum> {
static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
static_assert(Min < Max, "Max must be greater than Min!");
static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
return detail::enum_name_impl<Enum, Min, Max>(enumValue);
}
Expand All @@ -100,12 +99,12 @@
* @param str The string view representing the enum name.
* @return An optional enum value.
*/
template <typename Enum, int Min = enum_range<Enum>::min,
int Max = enum_range<Enum>::max,
template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
int Max = static_cast<int>(enum_range<Enum>::max),
detail::enable_if_t<!detail::has_bit_or<Enum>::value, bool> = true>
MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
-> mgutility::optional<Enum> {
static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
static_assert(Min < Max, "Max must be greater than Min!");
static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
return detail::to_enum_impl<Enum, Min, Max>(str);
}
Expand All @@ -119,12 +118,12 @@
* @param str The string view representing the enum name.
* @return An optional enum bitmask value.
*/
template <typename Enum, int Min = enum_range<Enum>::min,
int Max = enum_range<Enum>::max,
template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
int Max = static_cast<int>(enum_range<Enum>::max),
detail::enable_if_t<detail::has_bit_or<Enum>::value, bool> = true>
MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
-> mgutility::optional<Enum> {
static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
static_assert(Min < Max, "Max must be greater than Min!");
static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
return detail::to_enum_bitmask_impl<Enum, Min, Max>(str);
}
Expand All @@ -138,11 +137,11 @@
* @param value The integer value to cast.
* @return An optional enum value.
*/
template <typename Enum, int Min = enum_range<Enum>::min,
int Max = enum_range<Enum>::max>
template <typename Enum, int Min = static_cast<int>(enum_range<Enum>::min),
int Max = static_cast<int>(enum_range<Enum>::max)>
MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept
-> mgutility::optional<Enum> {
static_assert(Min < Max - 1, "Max must be greater than (Min + 1)!");
static_assert(Min < Max, "Max must be greater than Min!");
static_assert(std::is_enum<Enum>::value, "Type is not an Enum type!");
if (enum_name(static_cast<Enum>(value)).empty()) {
return mgutility::nullopt;
Expand All @@ -153,14 +152,14 @@
namespace operators {
template <typename Enum, mgutility::detail::enable_if_t<
std::is_enum<Enum>::value, bool> = true>
inline constexpr auto operator&(const Enum &lhs, const Enum &rhs) -> Enum {
constexpr auto operator&(const Enum &lhs, const Enum &rhs) -> Enum {
return static_cast<Enum>(mgutility::enum_to_underlying(lhs) &
mgutility::enum_to_underlying(rhs));
}

template <typename Enum, mgutility::detail::enable_if_t<
std::is_enum<Enum>::value, bool> = true>
inline constexpr auto operator|(const Enum &lhs, const Enum &rhs) -> Enum {
constexpr auto operator|(const Enum &lhs, const Enum &rhs) -> Enum {
return static_cast<Enum>(mgutility::enum_to_underlying(lhs) |
mgutility::enum_to_underlying(rhs));
}
Expand All @@ -178,10 +177,10 @@
*/
template <typename Enum, mgutility::detail::enable_if_t<
std::is_enum<Enum>::value, bool> = true>
auto operator<<(std::ostream &os, Enum e) -> std::ostream & {
auto operator<<(std::ostream &outStream, Enum enumVal) -> std::ostream & {
static_assert(std::is_enum<Enum>::value, "Value is not an Enum type!");
os << mgutility::enum_name(e);
return os;
outStream << mgutility::enum_name(enumVal);
return outStream;
}

#if defined(__cpp_lib_format)
Expand Down
Loading