OpenTTD Source 20260218-master-g2123fca5ea
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...
struct  is_enum_sequential< enum_type >
 Trait to enable prefix/postfix incrementing operators. More...
struct  EnumBitSetMask< Tstorage, Tenum, Tend_value >
 Helper template structure to get the mask for an EnumBitSet from the end enum value. More...
class  EnumBitSet< Tenum, Tstorage, Tend_value >
 Enum-as-bit-set wrapper. More...
class  EnumClassIndexContainer< Container, Index >
 A sort-of mixin that implements 'at(pos)' and 'operator[](pos)' only for a specific enum class. 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_SEQUENTIAL(enum_type)
 For some enums it is useful to add/sub more than 1.
#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 enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
constexpr enum_type operator+ (enum_type e, int offset)
 Add integer.
template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
constexpr enum_type & operator+= (enum_type &e, int offset)
template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
constexpr enum_type operator- (enum_type e, int offset)
 Subtract integer.
template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
constexpr enum_type & operator-= (enum_type &e, int offset)
template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
constexpr auto operator- (enum_type a, enum_type b)
 Distance of two enums.
template<typename T, class = typename std::enable_if_t<std::is_enum_v<T>>>
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>>>
constexpr void ToggleFlag (T &x, const T y)
 Toggle a value in a bitset enum.

Variables

template<class T>
constexpr bool is_scoped_enum_v = std::conjunction_v<std::is_enum<T>, std::negation<std::is_convertible<T, int>>>
 Implementation of std::is_scoped_enum_v (from C++23).
template<typename enum_type>
constexpr bool is_enum_incrementable_v = is_enum_incrementable<enum_type>::value
template<typename enum_type>
constexpr bool is_enum_sequential_v = is_enum_sequential<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:21
constexpr enum_type operator+(enum_type e, int offset)
Add integer.

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

Definition at line 167 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 157 of file enum_type.hpp.

◆ DECLARE_ENUM_AS_SEQUENTIAL

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

For some enums it is useful to add/sub more than 1.

Definition at line 151 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:28

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

Definition at line 86 of file enum_type.hpp.

Function Documentation

◆ HasFlag()

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

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

Referenced by DrawLayoutLine(), and ToggleFlag().

◆ operator+()

template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
enum_type operator+ ( enum_type e,
int offset )
inlineconstexpr

Add integer.

Parameters
eThe enum to add to.
offsetThe amount to add to the enum.
Returns
The new enum.

Definition at line 107 of file enum_type.hpp.

References to_underlying().

◆ operator++() [1/2]

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

Prefix increment.

Parameters
eThe enum to increment.
Returns
Reference to the incremented enum.

Definition at line 41 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>
enum_type operator++ ( enum_type & e,
int  )
inlineconstexpr

Postfix increment, uses prefix increment.

Parameters
eThe enum to increment.
Returns
Copy of the original value.

Definition at line 53 of file enum_type.hpp.

◆ operator+=()

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

Definition at line 113 of file enum_type.hpp.

◆ operator-() [1/2]

template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
auto operator- ( enum_type a,
enum_type b )
inlineconstexpr

Distance of two enums.

Parameters
aThe first enum.
bThe second enum.
Returns
The value of the first enum minus the value of the second enum.

Definition at line 145 of file enum_type.hpp.

References to_underlying().

◆ operator-() [2/2]

template<typename enum_type, std::enable_if_t< is_enum_sequential_v< enum_type >, bool > = true>
enum_type operator- ( enum_type e,
int offset )
inlineconstexpr

Subtract integer.

Parameters
eThe enum to subtract from.
offsetThe amount to subtract from the enum.
Returns
The new enum.

Definition at line 126 of file enum_type.hpp.

References to_underlying().

◆ operator--() [1/2]

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

Prefix decrement.

Parameters
eThe enum to decrement.
Returns
Reference to the decremented enum.

Definition at line 66 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>
enum_type operator-- ( enum_type & e,
int  )
inlineconstexpr

Postfix decrement, uses prefix decrement.

Parameters
eThe enum to decrement.
Returns
Copy of the original value.

Definition at line 78 of file enum_type.hpp.

◆ operator-=()

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

Definition at line 132 of file enum_type.hpp.

◆ to_underlying()

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

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

Parameters
eThe enum to get the value of.
Returns
The underlying value of the enum.

Definition at line 21 of file enum_type.hpp.

Referenced by AfterLoadGame(), AmbientSoundEffectCallback(), BadgeClassSaveConfigFeature(), CheckforTownRating(), CmdBuildBridge(), CmdBuildDock(), CmdBuildRoad(), CmdBuildTunnel(), CmdDoTownAction(), CmdModifyOrder(), CmdPlantTree(), ConvertTTDBasePrice(), DeserializeNetworkGameInfo(), TownAuthorityWindow::DrawActions(), NetworkContentListWindow::DrawDetails(), DrawOrderString(), DrawRoadBits(), DrawWaterDepot(), DrawWaterLock(), PerformanceRatingDetailWindow::DrawWidget(), TownAuthorityWindow::DrawWidget(), FixOwnerOfRailTrack(), GetBadgeClassConfiguration(), GetBridgeTooLowMessageForStationType(), GetGlobalVariable(), GetIndustryGamePlayProbability(), GetLockPartMinimalBridgeHeight(), GetMaxTreeSpriteSize(), GetNearbyTileInformation(), GetNumberOfIndustries(), GetScaledIndustryGenerationProbability(), GetShipDepotDirection(), GetSmallMapVegetationPixels(), OpenGLBackend::GetSpriteAlignment(), GetStationBridgeableTileInfo(), GetStationTileLayout(), GetTownActionCost(), GetTownRadiusGroup(), AirportTileScopeResolver::GetVariable(), HouseScopeResolver::GetVariable(), IndustriesScopeResolver::GetVariable(), IndustryTileScopeResolver::GetVariable(), ObjectScopeResolver::GetVariable(), RailTypeScopeResolver::GetVariable(), RoadStopScopeResolver::GetVariable(), RoadTypeScopeResolver::GetVariable(), TownScopeResolver::GetVariable(), GenerateLandscapeWindow::GetWidgetString(), GlobalVarChangeInfo(), IndustriesChangeInfo(), IsCommandAllowedWhilePaused(), IsInsideMM(), IsValidCommand(), LoadSpriteTables(), MakeLockTile(), MakePerformanceDetailPanels(), MakeRailDepot(), MakeRailNormal(), MakeRoadCrossing(), MakeRoadDepot(), MakeRoadNormal(), MakeShipDepot(), MakeStation(), MakeTree(), MakeTreeTypeButtons(), ViewportSign::MarkDirty(), MarkViewportDirty(), MoveBuoysToWaypoints(), MoveToNextNewsItem(), MoveToNextTickerItem(), CompanyStationsWindow::OnClick(), CreateScenarioWindow::OnClick(), GenerateLandscapeWindow::OnClick(), OrdersWindow::OnClick(), SpriteAlignerWindow::OnClick(), NetworkClientListWindow::OnClickClientAdmin(), NetworkClientListWindow::OnClickCompanyAdmin(), SpriteAlignerWindow::OnInvalidateData(), BuildRoadToolbarWindow::OnPlaceMouseUp(), OpenGLSprite::OpenGLSprite(), operator+(), operator++(), operator-(), operator-(), operator--(), OrdersWindow::OrderClick_FullLoad(), OrdersWindow::OrderClick_Service(), OrdersWindow::OrderClick_Unload(), OutputContentState(), ScaleByZoom(), ScaleByZoomLower(), ServerNetworkAdminSocketHandler::SendCmdLogging(), ServerNetworkAdminSocketHandler::SendCmdNames(), NetworkGameSocketHandler::SendCommand(), CombinedAuthenticationServerHandler::SendRequest(), ServerNetworkAdminSocketHandler::SendWelcome(), SerializeNetworkGameInfo(), SetAnimatedTileState(), SetClearGroundDensity(), Order::SetConditionComparator(), Order::SetConditionVariable(), SetHasSignals(), Order::SetLoadType(), SetRailGroundType(), SetRoadside(), SetRoadWaypointRoadside(), Order::SetStopLocation(), SetTileType(), SetTreeGroundDensity(), SetTreeGrowth(), Order::SetUnloadType(), CargoSpec::SetupCargoForClimate, SetupScreenshotViewport(), SetWaterClass(), SetWaterTileType(), ShowLastNewsMessage(), ShowNewspaper(), SndPlayScreenCoordFx(), NetworkContentListWindow::StateSorter(), TestVehicleBuildProbability(), TGPGetMaxHeight(), TriggerRoadStopRandomisation(), TriggerStationRandomisation(), UnScaleByZoom(), UnScaleByZoomLower(), UpdateTownRadius(), OrdersWindow::UpdateWidgetSize(), PerformanceRatingDetailWindow::UpdateWidgetSize(), TownAuthorityWindow::UpdateWidgetSize(), ValueStr(), ValueStr(), ValueStr(), and ValueStr().

◆ ToggleFlag()

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

Toggle a value in a bitset enum.

Parameters
xThe value to change.
yThe flag to toggle.

Definition at line 191 of file enum_type.hpp.

References HasFlag().

Variable Documentation

◆ is_enum_incrementable_v

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

Definition at line 33 of file enum_type.hpp.

◆ is_enum_sequential_v

template<typename enum_type>
bool is_enum_sequential_v = is_enum_sequential<enum_type>::value
constexpr

Definition at line 98 of file enum_type.hpp.

◆ is_scoped_enum_v

template<class T>
bool is_scoped_enum_v = std::conjunction_v<std::is_enum<T>, std::negation<std::is_convertible<T, int>>>
constexpr

Implementation of std::is_scoped_enum_v (from C++23).

Definition at line 24 of file enum_type.hpp.