Skip to content

Commit

Permalink
Merge pull request #4 from mguludag/fixed_string_experiment
Browse files Browse the repository at this point in the history
add ability to define min max as an enum value
  • Loading branch information
mguludag authored Feb 10, 2025
2 parents 54b309c + 99be359 commit c3b69ae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
8 changes: 4 additions & 4 deletions include/mgutility/reflection/detail/enum_for_each.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ template <typename Enum> class enum_for_each {
* @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 @@ template <typename Enum> class enum_for_each {
* @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
Expand Up @@ -179,7 +179,7 @@ get_enum_array(detail::enum_sequence<Enum, Is...> /*unused*/) noexcept
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
Expand Up @@ -145,7 +145,7 @@ struct enum_sequence_helper<Enum, Min, Min, Next...> {
* @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
Expand Up @@ -27,7 +27,6 @@ SOFTWARE.

#include "detail/enum_name_impl.hpp"


namespace mgutility {

/**
Expand Down Expand Up @@ -56,7 +55,7 @@ constexpr auto enum_to_underlying(Enum enumValue) noexcept
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 @@ MGUTILITY_CNSTXPR auto enum_name(Enum enumValue) noexcept
* @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 @@ auto enum_for_each<Enum>::enum_iter::operator*() const -> value_type {
* @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 @@ MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
* @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 @@ MGUTILITY_CNSTXPR auto to_enum(mgutility::string_view str) noexcept
* @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 @@ MGUTILITY_CNSTXPR auto enum_cast(int value) noexcept
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 @@ inline constexpr auto operator|(const Enum &lhs, const Enum &rhs) -> Enum {
*/
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

0 comments on commit c3b69ae

Please sign in to comment.