Gorgon Game Engine
Types.h
Go to the documentation of this file.
1 #include <stdint.h>
3 
4 #pragma once
5 
6 namespace Gorgon {
7 
9  typedef unsigned char Byte;
10 
11 #ifdef GORGON_USE_DOUBLE
12  typedef double Float;
13 #else
14  typedef float Float;
17 #endif
18 
21  template<class T_>
22  class Range {
23  public:
24  Range() = default;
25  Range(T_ start, T_ end) : Start(start), End(end) { }
26 
29  void Normalize() {
30  if(Start > End) {
31  auto t = End;
32  End = Start;
33  Start = t;
34  }
35  }
36 
38  T_ Difference() const {
39  return End - Start;
40  }
41 
42  T_ Start = T_();
43  T_ End = T_();
44  };
45 
46  typedef uint32_t Char;
47 
48  static const Float PI = (Float)3.141592653589793;
49 
51  class Empty {};
52 
55  enum class YesNoAuto {
56  No = 0,
57  Yes = 1,
58  Auto = 2
59  };
60 
63  enum class YesNoUnset {
64  No = 0,
65  Yes = 1,
66  Unset = 2
67  };
68 
70  inline int NumberOfSetBits(uint32_t i) {
71  i = i - ((i >> 1) & 0x55555555);
72  i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
73  return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
74  }
75 
77  inline uint32_t CeilToPowerOf2(uint32_t v) {
78  v--;
79  v |= v >> 1;
80  v |= v >> 2;
81  v |= v >> 4;
82  v |= v >> 8;
83  v |= v >> 16;
84  v++;
85 
86  return v;
87  }
88 
90  template<class T_>
91  void FitInto(T_ &v, T_ min, T_ max) {
92  //replace with std::clamp once C++17 is enforced
93 
94  v = v < min ? min : (v > max ? max : v);
95  }
96 
98  template<class T_>
99  T_ Clamp(T_ v, T_ min, T_ max) {
100  //replace with std::clamp once C++17 is enforced
101 
102  return v < min ? min : (v > max ? max : v);
103  }
104 
107  template<class T_>
108  bool Between(T_ v, T_ min, T_ max) {
109  return v >= min && v < max;
110  }
111 
114  template<class T_>
115  bool Between(Range<T_> range, T_ v) {
116  return v >= range.Start && v < range.End;
117  }
118 
121  template<class T_>
122  bool BetweenInclusive(T_ v, T_ min, T_ max) {
123  return v >= min && v <= max;
124  }
125  template <typename T_>
126  int Sign(T_ val) {
127  return (T_{} < val) - (val < T_{});
128  }
129 
130  template <typename T_>
131  T_ PositiveMod(T_ value, T_ mod) {
132  return ((value%mod)+mod)%mod;
133  }
134 
137 
139  static constexpr AssumeOwnershipTag AssumeOwnership;
140 
142  class Updatable {
143  public:
144  virtual ~Updatable() {}
145 
146  virtual void Update() = 0;
147  };
148 
151  enum class Parity {
152  None = 0,
153  Odd = 1,
154  Even = 2,
155  };
156 
158  enum class ItemPosition {
160  Nowhere = 0,
162  Alone = 1,
164  First = 2,
166  Middle = 3,
168  Last = 4
169  };
170 
171 
172 #ifdef DOXYGEN
173 # define ENUMCLASS enum
174 #else
175 # define ENUMCLASS enum class
176 #endif
177 }
Gorgon::Range::Range
Range(T_ start, T_ end)
Definition: Types.h:25
Gorgon::Updatable::Update
virtual void Update()=0
Gorgon::ItemPosition
ItemPosition
Defines where an item is located in a list.
Definition: Types.h:158
Gorgon::Updatable
Marks an object that can be updated.
Definition: Types.h:142
Gorgon::Range::Start
T_ Start
Definition: Types.h:42
Gorgon::Float
float Float
Represents floating point data type.
Definition: Types.h:16
Gorgon::UI::Odd
@ Odd
In lists denotes the item is in odd position.
Definition: Template.h:227
Gorgon::Range::End
T_ End
Definition: Types.h:43
Gorgon::Range::Range
Range()=default
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Char
uint32_t Char
Definition: Types.h:46
Gorgon::ItemPosition::Last
@ Last
Item is the last item.
Gorgon::Between
bool Between(T_ v, T_ min, T_ max)
Returns if the given number is within the given range including min but excluding max.
Definition: Types.h:108
Gorgon::YesNoAuto::No
@ No
Gorgon::CeilToPowerOf2
uint32_t CeilToPowerOf2(uint32_t v)
Calculate the smalllest power of two larger than this value.
Definition: Types.h:77
Gorgon::Range
This class represents a range of values.
Definition: Types.h:22
Gorgon::Range::Difference
T_ Difference() const
Returns the difference between start and end.
Definition: Types.h:38
Gorgon::Clamp
T_ Clamp(T_ v, T_ min, T_ max)
Performs clamping on the given value and returns the result.
Definition: Types.h:99
Gorgon::Updatable::~Updatable
virtual ~Updatable()
Definition: Types.h:144
Gorgon::FitInto
void FitInto(T_ &v, T_ min, T_ max)
Performs clamping on the given value.
Definition: Types.h:91
Gorgon::Sign
int Sign(T_ val)
Definition: Types.h:126
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::Input::Mouse::Middle
@ Middle
Definition: Mouse.h:35
Gorgon::Empty
A class that has no members and can be used as placeholder.
Definition: Types.h:51
Gorgon::YesNoUnset
YesNoUnset
This enumeration helps with systems that has boolen parameters that can be unset/empty.
Definition: Types.h:63
Gorgon::Range::Normalize
void Normalize()
Normalizes the range ensuring Start is less than or equal to the end.
Definition: Types.h:29
Gorgon::ItemPosition::First
@ First
Item is the first item.
Gorgon::PositiveMod
T_ PositiveMod(T_ value, T_ mod)
Definition: Types.h:131
Gorgon::end
std::vector< T_ >::const_iterator end(enum_type_id< T_ >)
Definition: Enum.h:288
Gorgon::Parity
Parity
Marks the parity as Odd or Even.
Definition: Types.h:151
Gorgon::Parity::None
@ None
Gorgon::BetweenInclusive
bool BetweenInclusive(T_ v, T_ min, T_ max)
Returns if the given number is within the given range including min and max.
Definition: Types.h:122
Gorgon::UI::Alone
@ Alone
In a list this denotes the item alone.
Definition: Template.h:242
Gorgon::YesNoAuto
YesNoAuto
This enumeration helps with systems that has boolen parameters that can be detected automatically,...
Definition: Types.h:55
Gorgon::NumberOfSetBits
int NumberOfSetBits(uint32_t i)
Returns the number of bits that are 1 in a number.
Definition: Types.h:70
Gorgon::YesNoUnset::Unset
@ Unset
Gorgon::AssumeOwnershipTag
Where acceptable, denotes that the object will assume the ownership.
Definition: Types.h:136