summaryrefslogtreecommitdiffstats
path: root/raul/Array.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-09-16 21:14:28 +0200
committerDavid Robillard <d@drobilla.net>2018-09-16 21:14:28 +0200
commit2c7828c9c420f3369d09e038371863c095e53c61 (patch)
tree2c3b4b658030d05f9f661ae57b84895836c2cf1a /raul/Array.hpp
parentd6744d73957bb23a57a5d2c1f207eb239d1a6d0d (diff)
downloadraul-2c7828c9c420f3369d09e038371863c095e53c61.tar.gz
raul-2c7828c9c420f3369d09e038371863c095e53c61.tar.bz2
raul-2c7828c9c420f3369d09e038371863c095e53c61.zip
Modernize Array
Diffstat (limited to 'raul/Array.hpp')
-rw-r--r--raul/Array.hpp52
1 files changed, 29 insertions, 23 deletions
diff --git a/raul/Array.hpp b/raul/Array.hpp
index 5f1d4e6..2c338e4 100644
--- a/raul/Array.hpp
+++ b/raul/Array.hpp
@@ -36,39 +36,51 @@ class Array : public Maid::Disposable
public:
explicit Array(size_t size = 0)
: _size(size)
- , _elems(NULL)
+ , _elems(size ? new T[size] : nullptr)
{
- if (size > 0) {
- _elems = new T[size];
- }
}
Array(size_t size, T initial_value)
: _size(size)
- , _elems(NULL)
+ , _elems(size ? new T[size] : nullptr)
{
if (size > 0) {
- _elems = new T[size];
for (size_t i = 0; i < size; ++i) {
_elems[i] = initial_value;
}
}
}
- explicit Array(const Array<T>& copy)
- : _size(copy._size)
+ Array(const Array<T>& array)
+ : _size(array._size)
+ , _elems(_size ? new T[_size] : nullptr)
{
- _elems = new T[_size];
for (size_t i = 0; i < _size; ++i) {
- _elems[i] = copy._elems[i];
+ _elems[i] = array._elems[i];
}
}
+ ~Array() override = default;
+
+ Array<T>& operator=(const Array<T>& array)
+ {
+ _size = array._size;
+ _elems = _size ? new T[_size] : nullptr;
+
+ for (size_t i = 0; i < _size; ++i) {
+ _elems[i] = array._elems[i];
+ }
+ }
+
+ Array(Array<T>&& array) noexcept = default;
+
+ Array<T>& operator=(Array<T>&&) noexcept = default;
+
Array(size_t size, const Array<T>& contents)
: _size(size)
+ , _elems(size ? new T[size] : nullptr)
{
assert(contents.size() >= size);
- _elems = new T[size];
for (size_t i = 0; i < std::min(size, contents.size()); ++i) {
_elems[i] = contents[i];
}
@@ -76,8 +88,8 @@ public:
Array(size_t size, const Array<T>& contents, T initial_value = T())
: _size(size)
+ , _elems(size ? new T[size] : nullptr)
{
- _elems = new T[size];
const size_t end = std::min(size, contents.size());
for (size_t i = 0; i < end; ++i) {
_elems[i] = contents[i];
@@ -87,32 +99,26 @@ public:
}
}
- ~Array() {
- delete[] _elems;
- }
-
virtual void alloc(size_t num_elems) {
- delete[] _elems;
_size = num_elems;
if (num_elems > 0) {
- _elems = new T[num_elems];
+ _elems = std::unique_ptr<T[]>(new T[num_elems]);
} else {
- _elems = NULL;
+ _elems.reset();
}
}
virtual void alloc(size_t num_elems, T initial_value) {
- delete[] _elems;
_size = num_elems;
if (num_elems > 0) {
- _elems = new T[num_elems];
+ _elems = std::unique_ptr<T[]>(new T[num_elems]);
for (size_t i = 0; i < _size; ++i) {
_elems[i] = initial_value;
}
} else {
- _elems = NULL;
+ _elems.reset();
}
}
@@ -130,7 +136,7 @@ public:
private:
size_t _size;
- T* _elems;
+ std::unique_ptr<T[]> _elems;
};
} // namespace Raul