Gamedev Framework (gf)  0.5.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 
42  template<typename T>
43  class BufferRef {
44  public:
50  constexpr BufferRef()
51  : m_data(nullptr)
52  , m_size(0)
53  {
54 
55  }
56 
63  constexpr BufferRef(T *data, std::size_t size)
64  : m_data(data)
65  , m_size(size)
66  {
67 
68  }
69 
75  BufferRef(std::vector<T>& values)
76  : m_data(values.data())
77  , m_size(values.size())
78  {
79 
80  }
81 
87  template<std::size_t N>
88  BufferRef(std::array<T,N>& values)
89  : m_data(values.data())
90  , m_size(values.size())
91  {
92 
93  }
94 
102  template<std::size_t N>
103  constexpr BufferRef(T (&data)[N])
104  : m_data(data)
105  , m_size(N)
106  {
107 
108  }
109 
115  T *getData() noexcept {
116  return m_data;
117  }
118 
124  constexpr std::size_t getSize() const noexcept {
125  return m_size;
126  }
127 
135  T *begin() noexcept {
136  return m_data;
137  }
138 
146  T *end() noexcept {
147  return m_data + m_size;
148  }
149 
150  private:
151  T *m_data;
152  std::size_t m_size;
153  };
154 
155 
156 #ifndef DOXYGEN_SHOULD_SKIP_THIS
157 }
158 #endif
159 }
160 
161 #endif // GF_BUFFER_REF_H
BufferRef(std::array< T, N > &values)
Constructor from a std::array
Definition: BufferRef.h:88
T * end() noexcept
Get an iterator past the last element.
Definition: BufferRef.h:146
constexpr BufferRef(T(&data)[N])
Constructor from a static array.
Definition: BufferRef.h:103
constexpr std::size_t getSize() const noexcept
Get the number of elements.
Definition: BufferRef.h:124
T * begin() noexcept
Get an iterator to the first element.
Definition: BufferRef.h:135
A reference to a modifiable buffer and its size.
Definition: BufferRef.h:43
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:63
constexpr BufferRef()
Default constructor.
Definition: BufferRef.h:50
T * getData() noexcept
Get a pointer to the elements.
Definition: BufferRef.h:115
BufferRef(std::vector< T > &values)
Constructor from a std::vector
Definition: BufferRef.h:75