OpenTTD Source 20250205-master-gfd85ab1e2c
enum_type.hpp File Reference

Type (helpers) for enums. More...

Go to the source code of this file.

Data Structures

struct  is_enum_incrementable< enum_type >
 Trait to enable prefix/postfix incrementing operators. More...
 
class  EnumBitSet< Tenum, Tstorage >
 Enum-as-bit-set wrapper. More...
 

Macros

#define DECLARE_INCREMENT_DECREMENT_OPERATORS(enum_type)
 For some enums it is useful to have pre/post increment/decrement operators.
 
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
 Operators to allow to work with enum as with type safe bit set in C++.
 
#define DECLARE_ENUM_AS_ADDABLE(EnumType)
 Operator that allows this enumeration to be added to any other enumeration.
 

Functions

template<typename enum_type >
constexpr std::underlying_type_t< enum_type > to_underlying (enum_type e)
 Implementation of std::to_underlying (from C++23)
 
template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type & operator++ (enum_type &e)
 Prefix increment.
 
template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type operator++ (enum_type &e, int)
 Postfix increment, uses prefix increment.
 
template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type & operator-- (enum_type &e)
 Prefix decrement.
 
template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type operator-- (enum_type &e, int)
 Postfix decrement, uses prefix decrement.
 
template<typename T , class = typename std::enable_if_t<std::is_enum_v<T>>>
debug_inline constexpr bool HasFlag (const T x, const T y)
 Checks if a value in a bitset enum is set.
 
template<typename T , class = typename std::enable_if_t<std::is_enum_v<T>>>
debug_inline constexpr void ToggleFlag (T &x, const T y)
 Toggle a value in a bitset enum.
 

Variables

template<typename enum_type >
constexpr bool is_enum_incrementable_v = is_enum_incrementable<enum_type>::value
 

Detailed Description

Type (helpers) for enums.

Definition in file enum_type.hpp.

Macro Definition Documentation

◆ DECLARE_ENUM_AS_ADDABLE

#define DECLARE_ENUM_AS_ADDABLE (   EnumType)
Value:
template <typename OtherEnumType, typename = typename std::enable_if<std::is_enum_v<OtherEnumType>, OtherEnumType>::type> \
constexpr OtherEnumType operator + (OtherEnumType m1, EnumType m2) { \
return static_cast<OtherEnumType>(to_underlying(m1) + to_underlying(m2)); \
}
constexpr std::underlying_type_t< enum_type > to_underlying(enum_type e)
Implementation of std::to_underlying (from C++23)
Definition enum_type.hpp:15

Operator that allows this enumeration to be added to any other enumeration.

Definition at line 78 of file enum_type.hpp.

◆ DECLARE_ENUM_AS_BIT_SET

#define DECLARE_ENUM_AS_BIT_SET (   enum_type)
Value:
inline constexpr enum_type operator | (enum_type m1, enum_type m2) { return static_cast<enum_type>(to_underlying(m1) | to_underlying(m2)); } \
inline constexpr enum_type operator & (enum_type m1, enum_type m2) { return static_cast<enum_type>(to_underlying(m1) & to_underlying(m2)); } \
inline constexpr enum_type operator ^ (enum_type m1, enum_type m2) { return static_cast<enum_type>(to_underlying(m1) ^ to_underlying(m2)); } \
inline constexpr enum_type& operator |= (enum_type& m1, enum_type m2) { m1 = m1 | m2; return m1; } \
inline constexpr enum_type& operator &= (enum_type& m1, enum_type m2) { m1 = m1 & m2; return m1; } \
inline constexpr enum_type& operator ^= (enum_type& m1, enum_type m2) { m1 = m1 ^ m2; return m1; } \
inline constexpr enum_type operator ~(enum_type m) { return static_cast<enum_type>(~to_underlying(m)); }

Operators to allow to work with enum as with type safe bit set in C++.

Definition at line 68 of file enum_type.hpp.

◆ DECLARE_INCREMENT_DECREMENT_OPERATORS

#define DECLARE_INCREMENT_DECREMENT_OPERATORS (   enum_type)
Value:
template <> struct is_enum_incrementable<enum_type> { \
static const bool value = true; \
};
Trait to enable prefix/postfix incrementing operators.
Definition enum_type.hpp:19

For some enums it is useful to have pre/post increment/decrement operators.

Definition at line 61 of file enum_type.hpp.

Function Documentation

◆ HasFlag()

template<typename T , class = typename std::enable_if_t<std::is_enum_v<T>>>
debug_inline constexpr bool HasFlag ( const T  x,
const T  y 
)
constexpr

Checks if a value in a bitset enum is set.

Parameters
xThe value to check.
yThe flag to check.
Returns
True iff the flag is set.

Definition at line 91 of file enum_type.hpp.

Referenced by DrawLayoutLine(), SanitizeSingleStringHelper(), and ToggleFlag().

◆ operator++() [1/2]

template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type & operator++ ( enum_type &  e)
inlineconstexpr

Prefix increment.

Definition at line 28 of file enum_type.hpp.

References to_underlying().

◆ operator++() [2/2]

template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type operator++ ( enum_type &  e,
int   
)
inlineconstexpr

Postfix increment, uses prefix increment.

Definition at line 36 of file enum_type.hpp.

◆ operator--() [1/2]

template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type & operator-- ( enum_type &  e)
inlineconstexpr

Prefix decrement.

Definition at line 45 of file enum_type.hpp.

References to_underlying().

◆ operator--() [2/2]

template<typename enum_type , std::enable_if_t< is_enum_incrementable_v< enum_type >, bool > = true>
constexpr enum_type operator-- ( enum_type &  e,
int   
)
inlineconstexpr

Postfix decrement, uses prefix decrement.

Definition at line 53 of file enum_type.hpp.

◆ to_underlying()

◆ ToggleFlag()

template<typename T , class = typename std::enable_if_t<std::is_enum_v<T>>>
debug_inline constexpr void ToggleFlag ( T &  x,
const T  y 
)
constexpr

Toggle a value in a bitset enum.

Parameters
xThe value to change.
yThe flag to toggle.

Definition at line 102 of file enum_type.hpp.

References HasFlag().

Variable Documentation

◆ is_enum_incrementable_v

template<typename enum_type >
constexpr bool is_enum_incrementable_v = is_enum_incrementable<enum_type>::value
constexpr

Definition at line 24 of file enum_type.hpp.