Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
ArrayRef.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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_ARRAY_REF_H
22 #define GF_ARRAY_REF_H
23 
24 #include <array>
25 #include <vector>
26 
27 namespace gf {
28 #ifndef DOXYGEN_SHOULD_SKIP_THIS
29 inline namespace v1 {
30 #endif
31 
41  template<typename T>
42  class ArrayRef {
43  public:
49  constexpr ArrayRef()
50  : m_data(nullptr)
51  , m_size(0)
52  {
53 
54  }
55 
61  constexpr ArrayRef(std::nullptr_t)
62  : m_data(nullptr)
63  , m_size(0)
64  {
65 
66  }
67 
74  constexpr ArrayRef(const T *data, std::size_t size)
75  : m_data(data)
76  , m_size(size)
77  {
78 
79  }
80 
86  ArrayRef(const std::vector<T>& values)
87  : m_data(values.data())
88  , m_size(values.size())
89  {
90 
91  }
92 
98  template<std::size_t N>
99  ArrayRef(const std::array<T,N>& values)
100  : m_data(values.data())
101  , m_size(values.size())
102  {
103 
104  }
105 
113  template<std::size_t N>
114  constexpr ArrayRef(const T (&data)[N])
115  : m_data(data)
116  , m_size(N)
117  {
118 
119  }
120 
126  ArrayRef(std::initializer_list<T> values)
127  : m_data(values.size() == 0 ? nullptr : values.begin())
128  , m_size(values.size())
129  {
130 
131  }
132 
138  constexpr const T *getData() const noexcept {
139  return m_data;
140  }
141 
147  constexpr std::size_t getSize() const noexcept {
148  return m_size;
149  }
150 
156  constexpr bool isEmpty() const noexcept {
157  return m_size == 0;
158  }
159 
167  constexpr const T *begin() const noexcept {
168  return m_data;
169  }
170 
178  constexpr const T *end() const noexcept {
179  return m_data + m_size;
180  }
181 
190  constexpr const T& operator[](std::size_t index) const {
191  return m_data[index];
192  }
193 
194  private:
195  const T *m_data;
196  std::size_t m_size;
197  };
198 
199 
200 #ifndef DOXYGEN_SHOULD_SKIP_THIS
201 }
202 #endif
203 }
204 
205 #endif // GF_ARRAY_REF_H
constexpr const T * getData() const noexcept
Get a pointer to the elements.
Definition: ArrayRef.h:138
constexpr ArrayRef(std::nullptr_t)
Null constructor.
Definition: ArrayRef.h:61
ArrayRef(const std::array< T, N > &values)
Constructor from a std::array
Definition: ArrayRef.h:99
constexpr ArrayRef()
Default constructor.
Definition: ArrayRef.h:49
constexpr const T & operator[](std::size_t index) const
Get an element at a given index.
Definition: ArrayRef.h:190
ArrayRef(std::initializer_list< T > values)
Constructor from an initializer list.
Definition: ArrayRef.h:126
constexpr const T * end() const noexcept
Get an iterator past the last element.
Definition: ArrayRef.h:178
The namespace for gf classes.
Definition: Action.h:35
constexpr std::size_t getSize() const noexcept
Get the number of elements.
Definition: ArrayRef.h:147
A constant reference to an array and its size.
Definition: ArrayRef.h:42
ArrayRef(const std::vector< T > &values)
Constructor from a std::vector
Definition: ArrayRef.h:86
constexpr const T * begin() const noexcept
Get an iterator to the first element.
Definition: ArrayRef.h:167
constexpr bool isEmpty() const noexcept
Check if the array is empty.
Definition: ArrayRef.h:156
constexpr ArrayRef(const T(&data)[N])
Constructor from a static array.
Definition: ArrayRef.h:114
constexpr ArrayRef(const T *data, std::size_t size)
Constructor from a pointer and a size.
Definition: ArrayRef.h:74