Gamedev Framework (gf)  0.14.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 <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 
60  constexpr ArrayRef(std::nullptr_t)
61  : m_data(nullptr)
62  , m_size(0)
63  {
64  }
65 
72  constexpr ArrayRef(const T *data, std::size_t size)
73  : m_data(data)
74  , m_size(size)
75  {
76  }
77 
83  ArrayRef(const std::vector<T>& values)
84  : m_data(values.data())
85  , m_size(values.size())
86  {
87  }
88 
94  template<std::size_t N>
95  ArrayRef(const std::array<T,N>& values)
96  : m_data(values.data())
97  , m_size(values.size())
98  {
99  }
100 
108  template<std::size_t N>
109  constexpr ArrayRef(const T (&data)[N])
110  : m_data(data)
111  , m_size(N)
112  {
113  }
114 
120  ArrayRef(std::initializer_list<T> values)
121  : m_data(values.size() == 0 ? nullptr : values.begin())
122  , m_size(values.size())
123  {
124  }
125 
131  constexpr const T *getData() const noexcept {
132  return m_data;
133  }
134 
140  constexpr std::size_t getSize() const noexcept {
141  return m_size;
142  }
143 
149  constexpr bool isEmpty() const noexcept {
150  return m_size == 0;
151  }
152 
160  constexpr const T *begin() const noexcept {
161  return m_data;
162  }
163 
171  constexpr const T *end() const noexcept {
172  return m_data + m_size;
173  }
174 
183  constexpr const T& operator[](std::size_t index) const {
184  return m_data[index];
185  }
186 
187  private:
188  const T *m_data;
189  std::size_t m_size;
190  };
191 
192 
201  template<typename T>
202  constexpr
203  ArrayRef<T> array(const T *data, std::size_t size) {
204  return ArrayRef<T>(data, size);
205  }
206 
207 
208 #ifndef DOXYGEN_SHOULD_SKIP_THIS
209 }
210 #endif
211 }
212 
213 #endif // GF_ARRAY_REF_H
constexpr const T * getData() const noexcept
Get a pointer to the elements.
Definition: ArrayRef.h:131
constexpr ArrayRef(std::nullptr_t)
Null constructor.
Definition: ArrayRef.h:60
ArrayRef(const std::array< T, N > &values)
Constructor from a std::array
Definition: ArrayRef.h:95
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:183
ArrayRef(std::initializer_list< T > values)
Constructor from an initializer list.
Definition: ArrayRef.h:120
constexpr const T * end() const noexcept
Get an iterator past the last element.
Definition: ArrayRef.h:171
The namespace for gf classes.
Definition: Action.h:35
constexpr std::size_t getSize() const noexcept
Get the number of elements.
Definition: ArrayRef.h:140
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:83
constexpr const T * begin() const noexcept
Get an iterator to the first element.
Definition: ArrayRef.h:160
constexpr bool isEmpty() const noexcept
Check if the array is empty.
Definition: ArrayRef.h:149
constexpr ArrayRef(const T(&data)[N])
Constructor from a static array.
Definition: ArrayRef.h:109
constexpr ArrayRef< T > array(const T *data, std::size_t size)
Create a constant reference to an array.
Definition: ArrayRef.h:203
constexpr ArrayRef(const T *data, std::size_t size)
Constructor from a pointer and a size.
Definition: ArrayRef.h:72