You.i Engine
YiRange.h
Go to the documentation of this file.
1 #ifndef YI_RANGE_H
2 #define YI_RANGE_H
3 
4 #include "YiError.h"
5 #include "YiPreprocessorMacros.h"
6 #include "YiTypeTraits.h"
7 
8 #include <glm/gtc/epsilon.hpp>
9 
22 template<typename Type>
23 class CYIRange
24 {
25 public:
30  m_start(0),
31  m_end(0)
32  {
33  static_assert((YiIsIntegral<Type>::value || YiIsFloatingPoint<Type>::value) && !YiIsSameType<Type, bool>::value, "Type must be a primitive data type, and not bool!");
34  }
35 
40  CYIRange(Type lhs, Type rhs) :
41  m_start(std::min(lhs, rhs)),
42  m_end(std::max(lhs, rhs))
43  {
44  }
45 
49  CYIRange(const CYIRange<Type> &other) :
50  m_start(other.m_start),
51  m_end(other.m_end)
52  {
53  }
54 
55  //==============================================================================
59  Type GetStart() const
60  {
61  return m_start;
62  }
63 
67  Type GetEnd() const
68  {
69  return m_end;
70  }
71 
75  Type GetLength() const
76  {
77  return m_end - m_start;
78  }
79 
80 #ifdef DOXY
81 
84  bool IsEmpty() const;
85 #else
86  template<typename T = Type>
87  typename std::enable_if<!std::is_floating_point<T>::value, bool>::type IsEmpty() const
88  {
89  return m_start == m_end;
90  }
91 
92  template<typename T = Type>
93  typename std::enable_if<std::is_floating_point<T>::value, bool>::type IsEmpty() const
94  {
95  return glm::epsilonEqual(m_start, m_end, glm::epsilon<Type>());
96  }
97 #endif
98 
102  bool IsNotEmpty() const
103  {
104  return !IsEmpty();
105  }
106 
107  //==============================================================================
111  static CYIRange CreateEmptyRange(Type m_start)
112  {
113  return CYIRange<Type>(m_start, m_start);
114  }
115 
119  static CYIRange CreateWithStartAndLength(Type startValue, Type length)
120  {
121  return CYIRange<Type>(startValue, startValue + length);
122  }
123 
124  //==============================================================================
131  void SetStart(Type newStart)
132  {
133  m_start = newStart;
134  m_end = std::max(m_end, newStart);
135  }
136 
143  void SetEnd(Type newEnd)
144  {
145  m_start = std::min(m_start, newEnd);
146  m_end = newEnd;
147  }
148 
154  void SetLength(Type newLength)
155  {
156  m_end = m_start + std::max(Type(), newLength);
157  }
158 
165  CYIRange<Type> WithStart(Type newStart) const
166  {
167  return CYIRange<Type>(newStart, std::max(newStart, m_end));
168  }
169 
176  CYIRange<Type> WithEnd(Type newEnd) const
177  {
178  return CYIRange<Type>(std::min(m_start, newEnd), newEnd);
179  }
180 
186  CYIRange<Type> WithLength(Type newLength) const
187  {
188  return CYIRange<Type>(m_start, m_start + std::max(newLength, Type()));
189  }
190 
194  CYIRange<Type> Expanded(Type amount) const
195  {
196  return CYIRange<Type>(m_start - amount, m_end + amount);
197  }
198 
203  CYIRange<Type> Shifted(Type delta) const
204  {
205  return CYIRange<Type>(m_start + delta, m_end + delta);
206  }
207 
208  //==============================================================================
215  bool Contains(Type position) const
216  {
217  return m_start <= position && position < m_end;
218  }
219 
220  //==============================================================================
225  {
226  m_start = other.m_start;
227  m_end = other.m_end;
228  return *this;
229  }
230 
236  {
237  m_start += delta;
238  m_end += delta;
239  return *this;
240  }
241 
247  {
248  m_start -= delta;
249  m_end -= delta;
250  return *this;
251  }
252 
257  CYIRange<Type> operator+(Type delta) const
258  {
259  return CYIRange<Type>(m_start + delta, m_end + delta);
260  }
261 
266  CYIRange<Type> operator-(Type delta) const
267  {
268  return CYIRange<Type>(m_start - delta, m_end - delta);
269  }
270 
271  bool operator==(const CYIRange<Type> &other) const
272  {
273  return m_start == other.m_start && m_end == other.m_end;
274  }
275 
276  bool operator!=(const CYIRange<Type> &other) const
277  {
278  return m_start != other.m_start || m_end != other.m_end;
279  }
280 
281 private:
282  Type m_start, m_end;
283 };
284 
287 #endif //YI_RANGE_H
CYIRange(const CYIRange< Type > &other)
Definition: YiRange.h:49
CYIRange< Type > operator+=(Type delta)
Definition: YiRange.h:235
CYIRange< Type > Expanded(Type amount) const
Definition: YiRange.h:194
bool operator!=(const CYIRange< Type > &other) const
Definition: YiRange.h:276
void SetLength(Type newLength)
Definition: YiRange.h:154
Type GetStart() const
Definition: YiRange.h:59
CYIRange< Type > operator-=(Type delta)
Definition: YiRange.h:246
CYIRange< Type > WithStart(Type newStart) const
Definition: YiRange.h:165
CYIRange(Type lhs, Type rhs)
Definition: YiRange.h:40
STL namespace.
Definition: YiTypeTraits.h:169
static CYIRange CreateEmptyRange(Type m_start)
Definition: YiRange.h:111
void SetStart(Type newStart)
Definition: YiRange.h:131
void SetEnd(Type newEnd)
Definition: YiRange.h:143
bool IsNotEmpty() const
Definition: YiRange.h:102
Type GetEnd() const
Definition: YiRange.h:67
CYIRange< Type > WithLength(Type newLength) const
Definition: YiRange.h:186
CYIRange()
Definition: YiRange.h:29
CYIRange< Type > Shifted(Type delta) const
Definition: YiRange.h:203
CYIRange< Type > operator-(Type delta) const
Definition: YiRange.h:266
static CYIRange CreateWithStartAndLength(Type startValue, Type length)
Definition: YiRange.h:119
bool IsEmpty() const
CYIRange< Type > operator+(Type delta) const
Definition: YiRange.h:257
bool operator==(const CYIRange< Type > &other) const
Definition: YiRange.h:271
Type GetLength() const
Definition: YiRange.h:75
Definition: YiTypeTraits.h:335
bool Contains(Type position) const
Definition: YiRange.h:215
Definition: YiTypeTraits.h:290
Definition: YiRange.h:23
CYIRange< Type > WithEnd(Type newEnd) const
Definition: YiRange.h:176
CYIRange< Type > & operator=(const CYIRange< Type > &other)
Definition: YiRange.h:224