AllocatedArray.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Louis Langholtz https://github.com/louis-langholtz/PlayRho
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  */
20 
21 #ifndef PLAYRHO_COMMON_ALLOCATEDARRAY_HPP
22 #define PLAYRHO_COMMON_ALLOCATEDARRAY_HPP
23 
25 
26 #include <functional>
27 
28 namespace playrho {
29 
31 template <typename T, typename Deleter = std::function<void (void *)> >
33 {
34 public:
35 
37  using size_type = std::size_t;
38 
40  using value_type = T;
41 
44 
47 
49  using const_reference = const value_type&;
50 
52  using pointer = value_type*;
53 
55  using const_pointer = const value_type*;
56 
58  using difference_type = std::ptrdiff_t;
59 
61  using deleter_type = Deleter;
62 
64  using iterator = pointer;
65 
68 
71  m_max_size{max_size}, m_data{data}, m_deleter{deleter}
72  {
73  assert(data);
74  }
75 
77  ~AllocatedArray() noexcept
78  {
79  m_deleter(m_data);
80  m_data = nullptr;
81  }
82 
83  AllocatedArray() = delete;
84  AllocatedArray(const AllocatedArray& copy) = delete;
85 
87  AllocatedArray(AllocatedArray&& other) noexcept:
88  m_max_size{other.m_max_size}, m_size{other.m_size}, m_data{other.m_data}, m_deleter{other.m_deleter}
89  {
90  other.m_size = 0;
91  other.m_data = nullptr;
92  }
93 
95  size_type size() const noexcept { return m_size; }
96 
98  size_type max_size() const noexcept { return m_max_size; }
99 
101  bool empty() const noexcept { return size() == 0; }
102 
104  pointer data() const noexcept { return m_data; }
105 
108  {
109  assert(i < m_size);
110  return m_data[i];
111  }
112 
115  {
116  assert(i < m_size);
117  return m_data[i];
118  }
119 
121  iterator begin() { return iterator{m_data}; }
122 
124  iterator end() { return iterator{m_data + size()}; }
125 
127  const_iterator begin() const { return const_iterator{m_data}; }
128 
130  const_iterator end() const { return const_iterator{m_data + size()}; }
131 
133  const_iterator cbegin() const { return const_iterator{m_data}; }
134 
136  const_iterator cend() const { return const_iterator{m_data + size()}; }
137 
140  reference back() noexcept
141  {
142  assert(m_size > 0);
143  return m_data[m_size - 1];
144  }
145 
148  const_reference back() const noexcept
149  {
150  assert(m_size > 0);
151  return m_data[m_size - 1];
152  }
153 
155  void clear() noexcept
156  {
157  m_size = 0;
158  }
159 
162  {
163  assert(m_size < m_max_size);
164  m_data[m_size] = value;
165  ++m_size;
166  }
167 
169  void pop_back() noexcept
170  {
171  assert(m_size > 0);
172  --m_size;
173  }
174 
175 private:
176 
178  static void noop_deleter(void* /*unused*/) {}
179 
180  size_type m_max_size = 0;
181  size_type m_size = 0;
182  pointer m_data = nullptr;
183  deleter_type m_deleter;
184 };
185 
186 }; // namespace playrho
187 
188 #endif // PLAYRHO_COMMON_ALLOCATEDARRAY_HPP