TurtleBrains  0.3.5
High quality, portable, C++ framework for rapid 2D game development.
tb_typed_integer.hpp
1 
9 #ifndef TurtleBrains_TypedInteger_hpp
10 #define TurtleBrains_TypedInteger_hpp
11 
12 #include <type_traits>
13 #include <functional>
14 
15 namespace TurtleBrains::Core
16 {
17 
48  template<typename Type, typename std::underlying_type<Type>::type defaultValue = typename std::underlying_type<Type>::type(0)> struct TypedInteger
49  {
50  public:
51  using Integer = typename std::underlying_type<Type>::type;
52 
56  constexpr inline TypedInteger(const Integer value = defaultValue) :
57  mValue(value)
58  {
59  }
60 
65  constexpr inline operator Integer(void) const { return Integer(mValue); }
66 
73  //inline TypedInteger::Integer operator+(int value) const { return mValue + value; }
74  //inline TypedInteger::Integer operator-(int value) const { return mValue - value; }
75 
76  inline constexpr TypedInteger::Integer operator+(TypedInteger::Integer value) const { return mValue + value; }
77  inline TypedInteger::Integer operator+=(TypedInteger::Integer value) { return mValue += value; }
78  inline TypedInteger::Integer operator++(void) { return ++mValue; }
79  inline TypedInteger::Integer operator++(int) { return mValue++; }
80 
81  inline constexpr TypedInteger::Integer operator-(TypedInteger::Integer value) const { return mValue - value; }
82  inline TypedInteger::Integer operator-=(TypedInteger::Integer value) { return mValue -= value; }
83  inline TypedInteger::Integer operator--(void) { return --mValue; }
84  inline TypedInteger::Integer operator--(int) { return mValue--; }
85 
86  private:
87  Integer mValue;
88  };
89 
90 }; /* namespace TurtleBrains::Core */
91 
92 namespace tbCore = TurtleBrains::Core;
93 
94 //--------------------------------------------------------------------------------------------------------------------//
95 
96 // 2025-11-09: This fixes an issue where std::unordered_map<TypedInteger> would complain about the std::hash not being
97 // defined/usable. It does have to be in global space to work.
98 template<typename Type> struct std::hash<TurtleBrains::Core::TypedInteger<Type>>
99 {
100  std::size_t operator()(const TurtleBrains::Core::TypedInteger<Type>& value) const noexcept
101  {
102  return std::hash<typename TurtleBrains::Core::TypedInteger<Type>::Integer>{}(value);
103  }
104 };
105 
106 #endif /* TurtleBrains_TypedInteger_hpp */
Contains core functionality for each component of the API.
Definition: tb_debug_logger.hpp:125
Here is some information about the primary namespace.
Definition: tb_application_dialog.hpp:22
Definition: tb_typed_integer.hpp:49
constexpr TypedInteger(const Integer value=defaultValue)
Definition: tb_typed_integer.hpp:56
constexpr TypedInteger::Integer operator+(TypedInteger::Integer value) const
Definition: tb_typed_integer.hpp:76