diff --git a/libs/common/include/helpers/EnumRange.h b/libs/common/include/helpers/EnumRange.h index 2b66d3f68..b41c5cd49 100644 --- a/libs/common/include/helpers/EnumRange.h +++ b/libs/common/include/helpers/EnumRange.h @@ -6,12 +6,23 @@ #include "MaxEnumValue.h" #include +#include #include // Using BOOST_FORCEINLINE mostly for increased debug performance (doesn't work for MSVC though) #define RTTR_CONSTEXPR_INLINE constexpr BOOST_FORCEINLINE namespace helpers { +template +struct EnumIterTraits +{ + using iterator_category = std::forward_iterator_tag; + using value_type = T; + using difference_type = std::make_signed_t, int>>; + using pointer = T*; + using reference = T&; +}; + /// Can be used in range-based for loops to iterate over each enum value /// Requires: T must be an enum with MaxEnumValue trait specialized /// T must be simple: Start at zero, no gaps @@ -19,7 +30,7 @@ template struct EnumRange { static_assert(std::is_enum::value, "Must be an enum!"); - class iterator + class iterator : public EnumIterTraits { unsigned value; @@ -41,7 +52,7 @@ template struct EnumRangeWithOffset { static_assert(std::is_enum::value, "Must be an enum!"); - class iterator + class iterator : public EnumIterTraits { unsigned value; diff --git a/tests/common/testEnumRange.cpp b/tests/common/testEnumRange.cpp index 86fe94810..1c7d603ae 100644 --- a/tests/common/testEnumRange.cpp +++ b/tests/common/testEnumRange.cpp @@ -4,6 +4,7 @@ #include "helpers/EnumRange.h" #include +#include enum PlainEnum { @@ -91,3 +92,17 @@ BOOST_AUTO_TEST_CASE(OffsetWorks) result.push_back(e); BOOST_TEST(result == expected); } + +BOOST_AUTO_TEST_CASE(CanAssignToRange) +{ + const auto enums = helpers::EnumRange{}; + std::vector enums_vec(enums.begin(), enums.end()); + std::vector expected{IntEnum::First, IntEnum::Second, IntEnum::Third, IntEnum::Forth}; + BOOST_TEST(enums_vec == expected); + + expected.push_back(expected.front()); + expected.erase(expected.begin()); + const auto enums2 = helpers::enumRange(IntEnum::Second); + enums_vec = std::vector(enums2.begin(), enums2.end()); + BOOST_TEST(enums_vec == expected); +} \ No newline at end of file