Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
ArrayRef.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 <cassert>
25 #include <array>
26 #include <vector>
27 
28 namespace gf {
29 #ifndef DOXYGEN_SHOULD_SKIP_THIS
30 inline namespace v1 {
31 #endif
32 
42  template<typename T>
43  class ArrayRef {
44  public:
50  constexpr ArrayRef()
51  : m_data(nullptr)
52  , m_size(0)
53  {
54  }
55 
61  constexpr ArrayRef(std::nullptr_t)
62  : m_data(nullptr)
63  , m_size(0)
64  {
65  }
66 
73  constexpr ArrayRef(const T *data, std::size_t size)
74  : m_data(data)
75  , m_size(size)
76  {
77  }
78 
84  ArrayRef(const std::vector<T>& values)
85  : m_data(values.data())
86  , m_size(values.size())
87  {
88  }
89 
95  template<std::size_t N>
96  ArrayRef(const std::array<T,N>& values)
97  : m_data(values.data())
98  , m_size(values.size())
99  {
100  }
101 
109  template<std::size_t N>
110  constexpr ArrayRef(const T (&data)[N])
111  : m_data(data)
112  , m_size(N)
113  {
114  }
115 
121  constexpr const T *getData() const noexcept {
122  return m_data;
123  }
124 
130  constexpr std::size_t getSize() const noexcept {
131  return m_size;
132  }
133 
139  constexpr bool isEmpty() const noexcept {
140  return m_size == 0;
141  }
142 
149  constexpr ArrayRef<T> sub(std::size_t index) {
150  assert(index <= m_size);
151  return ArrayRef<T>(m_data + index, m_size - index);
152  }
153 
161  constexpr const T *begin() const noexcept {
162  return m_data;
163  }
164 
172  constexpr const T *end() const noexcept {
173  return m_data + m_size;
174  }
175 
184  constexpr const T& operator[](std::size_t index) const {
185  return m_data[index];
186  }
187 
188  private:
189  const T *m_data;
190  std::size_t m_size;
191  };
192 
193 
202  template<typename T>
203  constexpr
204  ArrayRef<T> array(const T *data, std::size_t size) {
205  return ArrayRef<T>(data, size);
206  }
207 
208 
209 #ifndef DOXYGEN_SHOULD_SKIP_THIS
210 }
211 #endif
212 }
213 
214 #endif // GF_ARRAY_REF_H
constexpr const T * getData() const noexcept
Get a pointer to the elements.
Definition: ArrayRef.h:121
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:96
constexpr ArrayRef< T > sub(std::size_t index)
Create an array starting at a specified index.
Definition: ArrayRef.h:149
constexpr ArrayRef()
Default constructor.
Definition: ArrayRef.h:50
constexpr const T & operator[](std::size_t index) const
Get an element at a given index.
Definition: ArrayRef.h:184
constexpr const T * end() const noexcept
Get an iterator past the last element.
Definition: ArrayRef.h:172
The namespace for gf classes.
Definition: Action.h:35
constexpr std::size_t getSize() const noexcept
Get the number of elements.
Definition: ArrayRef.h:130
A constant reference to an array and its size.
Definition: ArrayRef.h:43
ArrayRef(const std::vector< T > &values)
Constructor from a std::vector
Definition: ArrayRef.h:84
constexpr const T * begin() const noexcept
Get an iterator to the first element.
Definition: ArrayRef.h:161
constexpr bool isEmpty() const noexcept
Check if the array is empty.
Definition: ArrayRef.h:139
constexpr ArrayRef(const T(&data)[N])
Constructor from a static array.
Definition: ArrayRef.h:110
constexpr ArrayRef< T > array(const T *data, std::size_t size)
Create a constant reference to an array.
Definition: ArrayRef.h:204
constexpr ArrayRef(const T *data, std::size_t size)
Constructor from a pointer and a size.
Definition: ArrayRef.h:73