Gamedev Framework (gf)  0.5.0
A C++11 framework for 2D games
Range.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_RANGE_H
22 #define GF_RANGE_H
23 
24 #include <cstddef>
25 #include <iterator>
26 
27 #include "Portability.h"
28 
29 namespace gf {
30 #ifndef DOXYGEN_SHOULD_SKIP_THIS
31 inline namespace v1 {
32 #endif
33 
41  template<typename T>
42  struct Range {
43  T lo;
44  T hi;
45 
50  struct Iterator : public std::iterator<std::input_iterator_tag, T> {
51  T index;
52 
58  constexpr Iterator(T iteratorIndex) noexcept
59  : index(iteratorIndex)
60  {
61 
62  }
63 
69  T operator*() noexcept {
70  return index;
71  }
72 
78  Iterator& operator++() noexcept {
79  ++index;
80  return *this;
81  }
82 
89  constexpr bool operator!=(const Iterator& other) const noexcept {
90  return index != other.index;
91  }
92 
99  constexpr bool operator==(const Iterator& other) const noexcept {
100  return index == other.index;
101  }
102  };
103 
110  constexpr bool contains(T value) const noexcept {
111  return lo <= value && value < hi;
112  }
113 
120  constexpr Iterator begin() const noexcept {
121  return Iterator(lo);
122  }
123 
130  constexpr Iterator end() const noexcept {
131  return Iterator(hi);
132  }
133 
139  constexpr T length() const noexcept {
140  return hi - lo;
141  }
142 
149  constexpr bool isEmpty() const noexcept {
150  return lo >= hi;
151  }
152 
159  constexpr bool isValid() const noexcept {
160  return lo <= hi;
161  }
162 
163  };
164 
172 
180 
188 
196 
197 // MSVC does not like extern template
198 #ifndef _MSC_VER
199  extern template struct Range<float>;
200  extern template struct Range<int>;
201  extern template struct Range<unsigned>;
202 #endif
203 
204 #ifndef DOXYGEN_SHOULD_SKIP_THIS
205 }
206 #endif
207 }
208 
209 #endif // GF_RANGE_H
A range iterator.
Definition: Range.h:50
constexpr Iterator(T iteratorIndex) noexcept
Constructor.
Definition: Range.h:58
A half-open range of values.
Definition: Range.h:42
constexpr bool operator!=(const Iterator &other) const noexcept
Inequality operator.
Definition: Range.h:89
T operator*() noexcept
Dereference operator.
Definition: Range.h:69
constexpr bool isEmpty() const noexcept
Check if the range is empty.
Definition: Range.h:149
T lo
The lower endpoint (included)
Definition: Range.h:43
constexpr bool operator==(const Iterator &other) const noexcept
Equality operator.
Definition: Range.h:99
constexpr Iterator end() const noexcept
Get a end iterator.
Definition: Range.h:130
T hi
The higher endpoint (excluded)
Definition: Range.h:44
constexpr bool contains(T value) const noexcept
Check if a value is in a range.
Definition: Range.h:110
constexpr Iterator begin() const noexcept
Get a begin iterator.
Definition: Range.h:120
The namespace for gf classes.
Definition: Action.h:34
T index
The index in the range.
Definition: Range.h:51
constexpr T length() const noexcept
Get the length of the range.
Definition: Range.h:139
Iterator & operator++() noexcept
Increment operator.
Definition: Range.h:78
constexpr bool isValid() const noexcept
Check is the range is valid.
Definition: Range.h:159