diff --git a/src/core/enum_type.hpp b/src/core/enum_type.hpp index 533f734825..753037309c 100644 --- a/src/core/enum_type.hpp +++ b/src/core/enum_type.hpp @@ -14,21 +14,54 @@ template constexpr std::underlying_type_t to_underlying(enum_type e) { return static_cast>(e); } +/** Trait to enable prefix/postfix incrementing operators. */ +template +struct is_enum_incrementable { + static constexpr bool value = false; +}; + +template +constexpr bool is_enum_incrementable_v = is_enum_incrementable::value; + +/** Prefix increment. */ +template , bool> = true> +inline constexpr enum_type &operator ++(enum_type &e) +{ + e = static_cast(to_underlying(e) + 1); + return e; +} + +/** Postfix increment, uses prefix increment. */ +template , bool> = true> +inline constexpr enum_type operator ++(enum_type &e, int) +{ + enum_type e_org = e; + ++e; + return e_org; +} + +/** Prefix decrement. */ +template , bool> = true> +inline constexpr enum_type &operator --(enum_type &e) +{ + e = static_cast(to_underlying(e) - 1); + return e; +} + +/** Postfix decrement, uses prefix decrement. */ +template , bool> = true> +inline constexpr enum_type operator --(enum_type &e, int) +{ + enum_type e_org = e; + --e; + return e_org; +} + /** Some enums need to have allowed incrementing (i.e. StationClassID) */ #define DECLARE_POSTFIX_INCREMENT(enum_type) \ - inline constexpr enum_type operator ++(enum_type& e, int) \ - { \ - enum_type e_org = e; \ - e = static_cast(to_underlying(e) + 1); \ - return e_org; \ - } \ - inline constexpr enum_type operator --(enum_type& e, int) \ - { \ - enum_type e_org = e; \ - e = static_cast(to_underlying(e) - 1); \ - return e_org; \ - } - + template <> struct is_enum_incrementable { \ + static const bool value = true; \ + }; /** Operators to allow to work with enum as with type safe bit set in C++ */