Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
ArrayRef.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_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 
32  /**
33  * @ingroup core
34  * @brief A constant reference to an array and its size
35  *
36  * This class stores a pointer to some data and its size. It can be built
37  * from various inputs: `std::vector`, static array, pointer and size.
38  *
39  * @sa gf::BufferRef, gf::StringRef
40  */
41  template<typename T>
42  class ArrayRef {
43  public:
44  /**
45  * @brief Default constructor
46  *
47  * Data is nullptr and size is 0.
48  */
49  constexpr ArrayRef()
50  : m_data(nullptr)
51  , m_size(0)
52  {
53 
54  }
55 
56  /**
57  * @brief Constructor from a pointer and a size
58  *
59  * @param data A pointer to the elements in the array
60  * @param size The number of elements in the array
61  */
62  constexpr ArrayRef(const T *data, std::size_t size)
63  : m_data(data)
64  , m_size(size)
65  {
66 
67  }
68 
69  /**
70  * @brief Constructor from a `std::vector`
71  *
72  * @param values The vector of elements
73  */
74  ArrayRef(const std::vector<T>& values)
75  : m_data(values.data())
76  , m_size(values.size())
77  {
78 
79  }
80 
81  /**
82  * @brief Constructor from a `std::array`
83  *
84  * @param values The array of elements
85  */
86  template<std::size_t N>
87  ArrayRef(const std::array<T,N>& values)
88  : m_data(values.data())
89  , m_size(values.size())
90  {
91 
92  }
93 
94  /**
95  * @brief Constructor from a static array
96  *
97  * The size is computed at compile-time.
98  *
99  * @param data The static array
100  */
101  template<std::size_t N>
102  constexpr ArrayRef(const T (&data)[N])
103  : m_data(data)
104  , m_size(N)
105  {
106 
107  }
108 
109  /**
110  * @brief Constructor from an initializer list
111  *
112  * @param values The list of elements
113  */
114  ArrayRef(std::initializer_list<T> values)
115  : m_data(values.size() == 0 ? nullptr : values.begin())
116  , m_size(values.size())
117  {
118 
119  }
120 
121  /**
122  * @brief Get a pointer to the elements
123  *
124  * @returns A pointer to the first element in the array
125  */
126  constexpr const T *getData() const noexcept {
127  return m_data;
128  }
129 
130  /**
131  * @brief Get the number of elements
132  *
133  * @returns The number of elements in the array
134  */
135  constexpr std::size_t getSize() const noexcept {
136  return m_size;
137  }
138 
139  /**
140  * @brief Get an iterator to the first element
141  *
142  * @returns A pointer to the first element
143  *
144  * @sa end()
145  */
146  constexpr const T *begin() const noexcept {
147  return m_data;
148  }
149 
150  /**
151  * @brief Get an iterator past the last element
152  *
153  * @returns A pointer past the last element
154  *
155  * @sa begin()
156  */
157  constexpr const T *end() const noexcept {
158  return m_data + m_size;
159  }
160 
161  private:
162  const T *m_data;
163  std::size_t m_size;
164  };
165 
166 
167 #ifndef DOXYGEN_SHOULD_SKIP_THIS
168 }
169 #endif
170 }
171 
172 #endif // GF_ARRAY_REF_H
constexpr const T * getData() const noexcept
Get a pointer to the elements.
Definition: ArrayRef.h:126
ArrayRef(const std::array< T, N > &values)
Constructor from a std::array
Definition: ArrayRef.h:87
constexpr ArrayRef()
Default constructor.
Definition: ArrayRef.h:49
ArrayRef(std::initializer_list< T > values)
Constructor from an initializer list.
Definition: ArrayRef.h:114
constexpr const T * end() const noexcept
Get an iterator past the last element.
Definition: ArrayRef.h:157
The namespace for gf classes.
Definition: Action.h:34
constexpr std::size_t getSize() const noexcept
Get the number of elements.
Definition: ArrayRef.h:135
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:74
constexpr const T * begin() const noexcept
Get an iterator to the first element.
Definition: ArrayRef.h:146
constexpr ArrayRef(const T(&data)[N])
Constructor from a static array.
Definition: ArrayRef.h:102
constexpr ArrayRef(const T *data, std::size_t size)
Constructor from a pointer and a size.
Definition: ArrayRef.h:62