OpenTTD Source 20241224-master-gf74b0cf984
enum_type.hpp File Reference

Type (helpers) for enums. More...

Go to the source code of this file.

Macros

#define DECLARE_POSTFIX_INCREMENT(enum_type)
 Some enums need to have allowed incrementing (i.e.
 
#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 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.
 

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 45 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 35 of file enum_type.hpp.

◆ DECLARE_POSTFIX_INCREMENT

#define DECLARE_POSTFIX_INCREMENT (   enum_type)
Value:
inline constexpr enum_type operator ++(enum_type& e, int) \
{ \
enum_type e_org = e; \
e = static_cast<enum_type>(to_underlying(e) + 1); \
return e_org; \
} \
inline constexpr enum_type operator --(enum_type& e, int) \
{ \
enum_type e_org = e; \
e = static_cast<enum_type>(to_underlying(e) - 1); \
return e_org; \
}

Some enums need to have allowed incrementing (i.e.

StationClassID)

Definition at line 18 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

◆ to_underlying()

template<typename enum_type >
constexpr std::underlying_type_t< enum_type > to_underlying ( enum_type  e)
constexpr

Implementation of std::to_underlying (from C++23)

Definition at line 15 of file enum_type.hpp.

Referenced by SetAnimatedTileState(), SetWaterTileType(), and TestVehicleBuildProbability().