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