Gamedev Framework (gf)  0.4.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 Constructor from a pointer and a size
59  *
60  * @param data A pointer to a buffer
61  * @param size The number of elements in the buffer
62  */
63  constexpr BufferRef(T *data, std::size_t size)
64  : m_data(data)
65  , m_size(size)
66  {
67 
68  }
69 
70  /**
71  * @brief Constructor from a `std::vector`
72  *
73  * @param values The vector of elements
74  */
75  BufferRef(std::vector<T>& values)
76  : m_data(values.data())
77  , m_size(values.size())
78  {
79 
80  }
81 
82  /**
83  * @brief Constructor from a `std::array`
84  *
85  * @param values The array of elements
86  */
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 
95  /**
96  * @brief Constructor from a static array
97  *
98  * The size is computed at compile-time.
99  *
100  * @param data The static array
101  */
102  template<std::size_t N>
103  constexpr BufferRef(T (&data)[N])
104  : m_data(data)
105  , m_size(N)
106  {
107 
108  }
109 
110  /**
111  * @brief Get a pointer to the elements
112  *
113  * @returns A pointer to the first element in the array
114  */
115  T *getData() noexcept {
116  return m_data;
117  }
118 
119  /**
120  * @brief Get the number of elements
121  *
122  * @returns The number of elements in the array
123  */
124  constexpr std::size_t getSize() const noexcept {
125  return m_size;
126  }
127 
128  /**
129  * @brief Get an iterator to the first element
130  *
131  * @returns A pointer to the first element
132  *
133  * @sa end()
134  */
135  T *begin() noexcept {
136  return m_data;
137  }
138 
139  /**
140  * @brief Get an iterator past the last element
141  *
142  * @returns A pointer past the last element
143  *
144  * @sa begin()
145  */
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