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