OpenTTD Source  20241108-master-g80f628063a
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. More...
 
#define DECLARE_ENUM_AS_BIT_SET(enum_type)
 Operators to allow to work with enum as with type safe bit set in C++. More...
 
#define DECLARE_ENUM_AS_ADDABLE(EnumType)
 Operator that allows this enumeration to be added to any other enumeration. More...
 

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>>>
constexpr debug_inline bool HasFlag (const T x, const T y)
 Checks if a value in a bitset enum is set. More...
 

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>>>
constexpr debug_inline 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 58 of file enum_type.hpp.

Referenced by AcceptEnginePreview(), CalcEngineReliability(), CalendarEnginesMonthlyLoop(), CmdSendVehicleToDepot(), GUIEngineListAddChildren(), NewVehicleAvailable(), and SetRailStationTileFlags().