Utils.hpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Chris Ohk, Youngjoong Kim, SeungHyun Jeon
2 
3 // We are making my contributions/submissions to this project solely in our
4 // personal capacity and are not conveying any rights to any intellectual
5 // property of any third parties.
6 
7 #ifndef ROSETTASTONE_UTILS_HPP
8 #define ROSETTASTONE_UTILS_HPP
9 
10 #include <cstddef>
11 
12 template <typename T>
13 constexpr bool AllCondIsTrue(const T& t)
14 {
15  return t == true;
16 }
17 
18 template <typename T, typename... Others>
19 constexpr bool AllCondIsTrue(const T& t, Others const&... args)
20 {
21  return (t == true) && AllCondIsTrue(args...);
22 }
23 
31 template <typename T>
32 class SizedPtr
33 {
34  public:
35  using value_type = T;
36  using size_type = std::size_t;
37 
38  using reference = T&;
39  using const_reference = const T&;
40 
41  using pointer = T*;
42  using const_pointer = const T*;
43 
44  using iterator = pointer;
46 
48  SizedPtr() : m_size(0), m_ptr(nullptr)
49  {
50  // Do nothing
51  }
52 
55  explicit SizedPtr(std::size_t size) : m_size(size), m_ptr(new T[m_size])
56  {
57  // Do nothing
58  }
59 
63  SizedPtr(T* ptr, std::size_t size) : m_size(size), m_ptr(ptr)
64  {
65  // Do nothing
66  }
67 
70  {
71  reset();
72  }
73 
75  SizedPtr(const SizedPtr& ptr) : m_size(ptr.size()), m_ptr(new T[m_size])
76  {
77  for (size_t i = 0; i < m_size; ++i)
78  {
79  m_ptr[i] = ptr[i];
80  }
81  }
82 
84  SizedPtr(SizedPtr&& ptr) noexcept : m_size(ptr.size()), m_ptr(ptr.get())
85  {
86  ptr.m_ptr = nullptr;
87  ptr.m_size = 0;
88  }
89 
92  {
93  reset(new T[ptr.size()], ptr.size());
94  for (std::size_t i = 0; i < m_size; ++i)
95  {
96  m_ptr[i] = ptr[i];
97  }
98  return *this;
99  }
100 
102  SizedPtr& operator=(SizedPtr&& ptr) noexcept
103  {
104  reset(ptr.get(), ptr.size());
105 
106  ptr.m_ptr = nullptr;
107  ptr.m_size = 0;
108 
109  return *this;
110  }
111 
113  void reset() noexcept
114  {
115  if (m_ptr != nullptr)
116  {
117  delete[] m_ptr;
118  }
119 
120  m_ptr = nullptr;
121  m_size = 0;
122  }
123 
127  void reset(T* other, std::size_t size) noexcept
128  {
129  if (m_ptr != nullptr)
130  {
131  delete[] m_ptr;
132  }
133 
134  m_ptr = other;
135  m_size = size;
136  }
137 
139  operator bool()
140  {
141  return m_ptr != nullptr;
142  }
143 
146  T* get()
147  {
148  return m_ptr;
149  }
150 
153  const T* get() const
154  {
155  return m_ptr;
156  }
157 
160  std::size_t size() const
161  {
162  return m_size;
163  }
164 
168  {
169  return *m_ptr;
170  }
171 
174  const T& operator*() const
175  {
176  return *m_ptr;
177  }
178 
182  T& operator[](std::size_t idx)
183  {
184  return m_ptr[idx];
185  }
186 
190  const T& operator[](std::size_t idx) const
191  {
192  return m_ptr[idx];
193  }
194 
198  bool operator==(const SizedPtr& other) const
199  {
200  if (m_size != other.size())
201  {
202  return false;
203  }
204 
205  for (std::size_t i = 0; i < m_size; ++i)
206  {
207  if (m_ptr[i] != other[i])
208  {
209  return false;
210  }
211  }
212 
213  return true;
214  }
215 
219  bool operator!=(const SizedPtr& other) const
220  {
221  return !operator==(other);
222  }
223 
226  bool operator==(std::nullptr_t) const
227  {
228  return m_ptr == nullptr;
229  }
230 
233  bool operator!=(std::nullptr_t) const
234  {
235  return m_ptr != nullptr;
236  }
237 
241  {
242  return m_ptr;
243  }
244 
248  {
249  return m_ptr;
250  }
251 
255  {
256  return m_ptr;
257  }
258 
262  {
263  return m_ptr + m_size;
264  }
265 
269  {
270  return m_ptr + m_size;
271  }
272 
276  {
277  return m_ptr + m_size;
278  }
279 
280  private:
281  std::size_t m_size;
282  T* m_ptr;
283 };
284 
285 #endif // ROSETTASTONE_UTILS_HPP
T * pointer
Definition: Utils.hpp:41
bool operator!=(std::nullptr_t) const
Definition: Utils.hpp:233
bool operator==(const SizedPtr &other) const
Definition: Utils.hpp:198
~SizedPtr()
Default destructor.
Definition: Utils.hpp:69
SizedPtr class.
Definition: Utils.hpp:32
constexpr bool AllCondIsTrue(const T &t)
Definition: Utils.hpp:13
iterator begin()
Definition: Utils.hpp:240
void reset() noexcept
Resets buffer.
Definition: Utils.hpp:113
T & reference
Definition: Utils.hpp:38
std::size_t size_type
Definition: Utils.hpp:36
std::size_t size() const
Definition: Utils.hpp:160
const_iterator cend() const
Definition: Utils.hpp:275
SizedPtr(SizedPtr &&ptr) noexcept
Move constructor.
Definition: Utils.hpp:84
T & operator[](std::size_t idx)
Definition: Utils.hpp:182
pointer iterator
Definition: Utils.hpp:44
const_iterator end() const
Definition: Utils.hpp:268
SizedPtr(std::size_t size)
Definition: Utils.hpp:55
const_iterator begin() const
Definition: Utils.hpp:247
void reset(T *other, std::size_t size) noexcept
Definition: Utils.hpp:127
const_iterator cbegin() const
Definition: Utils.hpp:254
const T * const_pointer
Definition: Utils.hpp:42
iterator end()
Definition: Utils.hpp:261
const T & operator*() const
Definition: Utils.hpp:174
SizedPtr(T *ptr, std::size_t size)
Definition: Utils.hpp:63
T & operator*()
Definition: Utils.hpp:167
SizedPtr()
Default constructor.
Definition: Utils.hpp:48
SizedPtr(const SizedPtr &ptr)
Copy constructor, deep copying buffer.
Definition: Utils.hpp:75
bool operator!=(const SizedPtr &other) const
Definition: Utils.hpp:219
const T & operator[](std::size_t idx) const
Definition: Utils.hpp:190
const T & const_reference
Definition: Utils.hpp:39
const_pointer const_iterator
Definition: Utils.hpp:45
bool operator==(std::nullptr_t) const
Definition: Utils.hpp:226
T value_type
Definition: Utils.hpp:35
SizedPtr & operator=(const SizedPtr &ptr)
Copy assignment operator, deep copying buffer.
Definition: Utils.hpp:91
SizedPtr & operator=(SizedPtr &&ptr) noexcept
Move assignment operator.
Definition: Utils.hpp:102