From a93b7172aab170566a7ef7d6f44657f08859dcca Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 9 Dec 2019 09:41:50 +0100 Subject: [PATCH] util/AllocatedArray: add ConstBuffer copy constructor --- src/util/AllocatedArray.hxx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/util/AllocatedArray.hxx b/src/util/AllocatedArray.hxx index aaa788420..fc6400014 100644 --- a/src/util/AllocatedArray.hxx +++ b/src/util/AllocatedArray.hxx @@ -61,16 +61,17 @@ public: explicit AllocatedArray(size_type _size) noexcept :buffer{new T[_size], _size} {} - explicit AllocatedArray(const AllocatedArray &other) noexcept { - assert(other.size() == 0 || other.buffer.data != nullptr); - - if (other == nullptr) + explicit AllocatedArray(ConstBuffer src) noexcept { + if (src == nullptr) return; - buffer = {new T[other.buffer.size], other.buffer.size}; - std::copy_n(other.buffer.data, buffer.size, buffer.data); + buffer = {new T[src.size], src.size}; + std::copy_n(src.data, src.size, buffer.data); } + explicit AllocatedArray(const AllocatedArray &other) noexcept + :AllocatedArray(other.buffer) {} + AllocatedArray(AllocatedArray &&other) noexcept :buffer(std::exchange(other.buffer, nullptr)) {} @@ -78,6 +79,15 @@ public: delete[] buffer.data; } + AllocatedArray &operator=(ConstBuffer src) noexcept { + assert(size() == 0 || buffer.data != nullptr); + assert(src.size == 0 || src.data != nullptr); + + ResizeDiscard(src.size); + std::copy_n(src.data, src.size, buffer.data); + return *this; + } + AllocatedArray &operator=(const AllocatedArray &other) noexcept { assert(size() == 0 || buffer.data != nullptr); assert(other.size() == 0 || other.buffer.data != nullptr);