Gamedev Framework (gf)  0.11.0
A C++14 framework for 2D games
BufferRef.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 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 
42  template<typename T>
43  class BufferRef {
44  public:
50  constexpr BufferRef()
51  : m_data(nullptr)
52  , m_size(0)
53  {
54 
55  }
56 
62  constexpr BufferRef(std::nullptr_t)
63  : m_data(nullptr)
64  , m_size(0)
65  {
66 
67  }
68 
75  constexpr BufferRef(T *data, std::size_t size)
76  : m_data(data)
77  , m_size(size)
78  {
79 
80  }
81 
87  BufferRef(std::vector<T>& values)
88  : m_data(values.data())
89  , m_size(values.size())
90  {
91 
92  }
93 
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 
114  template<std::size_t N>
115  constexpr BufferRef(T (&data)[N])
116  : m_data(data)
117  , m_size(N)
118  {
119 
120  }
121 
127  T *getData() noexcept {
128  return m_data;
129  }
130 
136  constexpr std::size_t getSize() const noexcept {
137  return m_size;
138  }
139 
145  constexpr bool isEmpty() const noexcept {
146  return m_size == 0;
147  }
148 
156  T *begin() noexcept {
157  return m_data;
158  }
159 
167  T *end() noexcept {
168  return m_data + m_size;
169  }
170 
179  T& operator[](std::size_t index) {
180  return m_data[index];
181  }
182 
183  private:
184  T *m_data;
185  std::size_t m_size;
186  };
187 
188 
189 #ifndef DOXYGEN_SHOULD_SKIP_THIS
190 }
191 #endif
192 }
193 
194 #endif // GF_BUFFER_REF_H
T & operator[](std::size_t index)
Get an element at a given index.
Definition: BufferRef.h:179
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:167
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:156
A reference to a modifiable buffer and its size.
Definition: BufferRef.h:43
constexpr bool isEmpty() const noexcept
Check if the buffer is empty.
Definition: BufferRef.h:145
constexpr BufferRef(std::nullptr_t)
Null constructor.
Definition: BufferRef.h:62
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: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