Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
BufferRef.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_BUFFER_REF_H
22 #define GF_BUFFER_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 
43  template<typename T>
44  class BufferRef {
45  public:
51  constexpr BufferRef()
52  : m_data(nullptr)
53  , m_size(0)
54  {
55 
56  }
57 
63  constexpr BufferRef(std::nullptr_t)
64  : m_data(nullptr)
65  , m_size(0)
66  {
67 
68  }
69 
76  constexpr BufferRef(T *data, std::size_t size)
77  : m_data(data)
78  , m_size(size)
79  {
80 
81  }
82 
88  BufferRef(std::vector<T>& values)
89  : m_data(values.data())
90  , m_size(values.size())
91  {
92 
93  }
94 
100  template<std::size_t N>
101  BufferRef(std::array<T,N>& values)
102  : m_data(values.data())
103  , m_size(values.size())
104  {
105 
106  }
107 
115  template<std::size_t N>
116  constexpr BufferRef(T (&data)[N])
117  : m_data(data)
118  , m_size(N)
119  {
120 
121  }
122 
128  T *getData() noexcept {
129  return m_data;
130  }
131 
137  constexpr std::size_t getSize() const noexcept {
138  return m_size;
139  }
140 
146  constexpr bool isEmpty() const noexcept {
147  return m_size == 0;
148  }
149 
156  constexpr BufferRef<T> sub(std::size_t index) {
157  assert(index <= m_size);
158  return BufferRef<T>(m_data + index, m_size - index);
159  }
160 
168  T *begin() noexcept {
169  return m_data;
170  }
171 
179  T *end() noexcept {
180  return m_data + m_size;
181  }
182 
191  T& operator[](std::size_t index) {
192  return m_data[index];
193  }
194 
195  private:
196  T *m_data;
197  std::size_t m_size;
198  };
199 
200 
209  template<typename T>
210  constexpr
211  BufferRef<T> buffer(T *data, std::size_t size) {
212  return BufferRef<T>(data, size);
213  }
214 
215 
216 #ifndef DOXYGEN_SHOULD_SKIP_THIS
217 }
218 #endif
219 }
220 
221 #endif // GF_BUFFER_REF_H
T & operator[](std::size_t index)
Get an element at a given index.
Definition: BufferRef.h:191
BufferRef(std::array< T, N > &values)
Constructor from a std::array
Definition: BufferRef.h:101
T * end() noexcept
Get an iterator past the last element.
Definition: BufferRef.h:179
constexpr BufferRef< T > sub(std::size_t index)
Create a buffer starting at a specified index.
Definition: BufferRef.h:156
constexpr BufferRef(T(&data)[N])
Constructor from a static array.
Definition: BufferRef.h:116
constexpr std::size_t getSize() const noexcept
Get the number of elements.
Definition: BufferRef.h:137
constexpr BufferRef< T > buffer(T *data, std::size_t size)
Create a reference to a buffer.
Definition: BufferRef.h:211
T * begin() noexcept
Get an iterator to the first element.
Definition: BufferRef.h:168
A reference to a modifiable buffer and its size.
Definition: BufferRef.h:44
constexpr bool isEmpty() const noexcept
Check if the buffer is empty.
Definition: BufferRef.h:146
constexpr BufferRef(std::nullptr_t)
Null constructor.
Definition: BufferRef.h:63
The namespace for gf classes.
Definition: Action.h:35
constexpr BufferRef(T *data, std::size_t size)
Constructor from a pointer and a size.
Definition: BufferRef.h:76
constexpr BufferRef()
Default constructor.
Definition: BufferRef.h:51
T * getData() noexcept
Get a pointer to the elements.
Definition: BufferRef.h:128
BufferRef(std::vector< T > &values)
Constructor from a std::vector
Definition: BufferRef.h:88