SolidUtils
ConstArray.hpp
Go to the documentation of this file.
1 
30 #ifndef SOLIDUTILS_INCLUDE_CONSTARRAY_HPP
31 #define SOLIDUTILS_INCLUDE_CONSTARRAY_HPP
32 
33 
34 #include "Debug.hpp"
35 #include "Array.hpp"
36 
37 #include <memory>
38 
39 
40 namespace sl
41 {
42 
53 template<typename T>
55 {
56  public:
61  m_size(0),
62  m_data(nullptr),
63  m_isOwner(true)
64  {
65  // do nothing
66  }
67 
75  Array<T> array) :
76  m_size(array.size()), // must come before call to steal()
77  m_data(array.steal()),
78  m_isOwner(true)
79  {
80  // do nothing
81  }
82 
83 
92  T const * const ptr,
93  size_t const size) :
94  m_size(size),
95  m_data(ptr),
96  m_isOwner(false)
97  {
98  ASSERT_TRUE(ptr != nullptr || size == 0);
99  }
100 
108  std::unique_ptr<T[]>&& ptr,
109  size_t const size) :
110  m_size(size),
111  m_data(std::move(ptr)),
112  m_isOwner(true)
113  {
114  // do nothing
115  }
116 
117 
124  ConstArray && rhs) noexcept :
125  m_size(rhs.m_size),
126  m_data(std::move(rhs.m_data)),
127  m_isOwner(rhs.m_isOwner)
128  {
129  rhs.m_size = 0;
130  }
131 
132 
138  ConstArray(
139  ConstArray const & rhs) = delete;
140 
141 
150  ConstArray const & rhs) = delete;
151 
152 
161  ConstArray && lhs)
162  {
163  m_size = lhs.m_size;
164  m_data = std::move(lhs.m_data);
165 
166  lhs.m_size = 0;
167 
168  return *this;
169  }
170 
175  {
176  if (!m_isOwner) {
177  // disown the unique pointer before this object is destroyed.
178  m_data.release();
179  }
180  }
181 
182 
190  T const & operator[](
191  size_t const index) const noexcept
192  {
193  ASSERT_LESS(index, m_size);
194  return m_data[index];
195  }
196 
197 
203  T const * data() const noexcept
204  {
205  return m_data.get();
206  }
207 
208 
214  size_t size() const noexcept
215  {
216  return m_size;
217  }
218 
219 
225  T const * begin() const noexcept
226  {
227  return m_data.get();
228  }
229 
230 
236  T const * end() const noexcept
237  {
238  return m_data.get() + m_size;
239  }
240 
241 
247  T const & front() const noexcept
248  {
249  return (*this)[0];
250  }
251 
257  T const & back() const noexcept
258  {
259  return (*this)[m_size-1];
260  }
261 
265  void clear()
266  {
267  m_size = 0;
268  m_data.reset();
269  }
270 
271 
272  private:
273  size_t m_size;
274  std::unique_ptr<T const []> m_data;
275  bool m_isOwner;
276 
277 };
278 
279 
280 }
281 
282 
283 
284 #endif
ConstArray & operator=(ConstArray const &rhs)=delete
Deleted assignment operator.
Definition: Alloc.hpp:40
T const * data() const noexcept
Get the underlying memory.
Definition: ConstArray.hpp:203
size_t size() const noexcept
Get the size of the underlying memory allocation.
Definition: ConstArray.hpp:214
The ConstArray class provides functionality similar to std::vector, except that it does not construct...
Definition: ConstArray.hpp:54
ConstArray(ConstArray &&rhs) noexcept
Move constructor.
Definition: ConstArray.hpp:123
T const & front() const noexcept
Get the front of the array.
Definition: ConstArray.hpp:247
ConstArray(Array< T > array)
Create a new non-mutable array with a default value for each element.
Definition: ConstArray.hpp:74
ConstArray(T const *const ptr, size_t const size)
Create a new non-owning array. If the pointer is null, then size must 0.
Definition: ConstArray.hpp:91
~ConstArray()
Destructor.
Definition: ConstArray.hpp:174
The Array class provides functionality similar to std::vector, except that it does not construct or d...
Definition: Array.hpp:53
A mutable array structure for storing self-allocated memory.
T const * end() const noexcept
Get the end iterator.
Definition: ConstArray.hpp:236
ConstArray & operator=(ConstArray &&lhs)
Assignment operator (move).
Definition: ConstArray.hpp:160
void clear()
Free the memory associated with this array.
Definition: ConstArray.hpp:265
T const * begin() const noexcept
Get the beginning iterator.
Definition: ConstArray.hpp:225
T const & back() const noexcept
Get the back of the array.
Definition: ConstArray.hpp:257
T const & operator[](size_t const index) const noexcept
Get the element at the given index.
Definition: ConstArray.hpp:190
Debugging functions.
ConstArray()
Create an empty non mutable array.
Definition: ConstArray.hpp:60
ConstArray(std::unique_ptr< T[]> &&ptr, size_t const size)
Create a new constant array.
Definition: ConstArray.hpp:107