Added copy and move to Buffer
This commit is contained in:
parent
63beae0b2a
commit
3598d45c26
3 changed files with 121 additions and 45 deletions
|
|
@ -1,8 +1,10 @@
|
|||
#pragma once
|
||||
#include "Globals.h"
|
||||
#include <type_traits>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
template <class Type, unsigned int Alignment = SIMDConfig::defaultAlignment>
|
||||
class Buffer
|
||||
|
|
@ -20,7 +22,9 @@ public:
|
|||
using size_type = size_t;
|
||||
using difference_type = ptrdiff_t;
|
||||
|
||||
constexpr Buffer() { }
|
||||
Buffer()
|
||||
{
|
||||
}
|
||||
Buffer(size_t size)
|
||||
{
|
||||
resize(size);
|
||||
|
|
@ -36,7 +40,9 @@ public:
|
|||
auto tempSize = newSize + 2 * AlignmentMask; // To ensure that we have leeway at the beginning and at the end
|
||||
auto *newData = paddedData != nullptr ? std::realloc(paddedData, tempSize * sizeof(value_type)) : std::malloc(tempSize * sizeof(value_type));
|
||||
if (newData == nullptr)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
largerSize = tempSize;
|
||||
alignedSize = newSize;
|
||||
|
|
@ -47,6 +53,8 @@ public:
|
|||
_alignedEnd = normalEnd + Alignment - endMisalignment;
|
||||
else
|
||||
_alignedEnd = normalEnd;
|
||||
|
||||
DBG("Buffer resized (" << newSize << ") at: " << this);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -64,6 +72,49 @@ public:
|
|||
std::free(paddedData);
|
||||
}
|
||||
|
||||
Buffer(const Buffer<Type> &other)
|
||||
{
|
||||
if (resize(other.size()))
|
||||
{
|
||||
std::memcpy(this->data(), other.data(), other.size() * sizeof(value_type));
|
||||
}
|
||||
}
|
||||
|
||||
Buffer(Buffer<Type> &&other)
|
||||
{
|
||||
largerSize = std::exchange(other.largerSize, 0);
|
||||
alignedSize = std::exchange(other.alignedSize, 0);
|
||||
paddedData = std::exchange(other.paddedData, nullptr);
|
||||
normalData = std::exchange(other.normalData, nullptr);
|
||||
normalEnd = std::exchange(other.normalEnd, nullptr);
|
||||
_alignedEnd = std::exchange(other._alignedEnd, nullptr);
|
||||
}
|
||||
|
||||
Buffer<Type> &operator=(const Buffer<Type> &other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
if (resize(other.size()))
|
||||
std::memcpy(this->data(), other.data(), other.size() * sizeof(value_type));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Buffer<Type> &operator=(Buffer<Type> &&other)
|
||||
{
|
||||
if (this != &other)
|
||||
{
|
||||
std::free(paddedData);
|
||||
largerSize = std::exchange(other.largerSize, 0);
|
||||
alignedSize = std::exchange(other.alignedSize, 0);
|
||||
paddedData = std::exchange(other.paddedData, nullptr);
|
||||
normalData = std::exchange(other.normalData, nullptr);
|
||||
normalEnd = std::exchange(other.normalEnd, nullptr);
|
||||
_alignedEnd = std::exchange(other._alignedEnd, nullptr);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Type &operator[](int idx) { return *(normalData + idx); }
|
||||
constexpr pointer data() const noexcept { return normalData; }
|
||||
constexpr size_type size() const noexcept { return alignedSize; }
|
||||
|
|
@ -71,6 +122,7 @@ public:
|
|||
constexpr iterator begin() noexcept { return data(); }
|
||||
constexpr iterator end() noexcept { return normalEnd; }
|
||||
constexpr pointer alignedEnd() noexcept { return _alignedEnd; }
|
||||
|
||||
private:
|
||||
static constexpr auto AlignmentMask{Alignment - 1};
|
||||
static constexpr auto TypeAlignment{Alignment / sizeof(value_type)};
|
||||
|
|
|
|||
|
|
@ -46,6 +46,8 @@ public:
|
|||
absl::Span<Type> getSpan(Channel channel)
|
||||
{
|
||||
switch (channel) {
|
||||
default:
|
||||
[[fallthrough]];
|
||||
case Channel::left:
|
||||
return absl::MakeSpan(leftBuffer);
|
||||
case Channel::right:
|
||||
|
|
@ -57,6 +59,8 @@ public:
|
|||
{
|
||||
ASSERT(sampleIndex >= 0);
|
||||
switch (channel) {
|
||||
default:
|
||||
[[fallthrough]];
|
||||
case Channel::left:
|
||||
return leftBuffer[sampleIndex];
|
||||
case Channel::right:
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
#include "catch2/catch.hpp"
|
||||
#include "../sources/Buffer.h"
|
||||
#include "catch2/catch.hpp"
|
||||
#include <algorithm>
|
||||
using namespace Catch::literals;
|
||||
|
||||
|
|
@ -53,7 +53,6 @@ TEST_CASE("[Buffer] 10 floats ")
|
|||
REQUIRE(element == 0.0f);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("[Buffer] Resize 10 floats ")
|
||||
{
|
||||
const int baseSize { 10 };
|
||||
|
|
@ -119,3 +118,24 @@ TEST_CASE("[Buffer] Resize 65536 floats ")
|
|||
for (auto i = 0; i < smallSize; ++i)
|
||||
REQUIRE(buffer[i] == 1.0f);
|
||||
}
|
||||
|
||||
TEST_CASE("[Buffer] Copy and move")
|
||||
{
|
||||
const int baseSize { 128 };
|
||||
Buffer<float> buffer(baseSize);
|
||||
Buffer<float> copied { baseSize - 4 };
|
||||
std::fill(buffer.begin(), buffer.end(), 1.0f);
|
||||
std::fill(copied.begin(), copied.end(), 2.0f);
|
||||
copied = buffer;
|
||||
checkBoundaries(copied, baseSize);
|
||||
REQUIRE(std::all_of(copied.begin(), copied.end(), [](auto value) { return value == 1.0f; }));
|
||||
|
||||
Buffer<float> copyConstructed { buffer };
|
||||
checkBoundaries(copyConstructed, baseSize);
|
||||
REQUIRE(std::all_of(copyConstructed.begin(), copyConstructed.end(), [](auto value) { return value == 1.0f; }));
|
||||
|
||||
Buffer<float> moveConstructed { std::move(buffer) };
|
||||
REQUIRE(buffer.empty());
|
||||
checkBoundaries(moveConstructed, baseSize);
|
||||
REQUIRE(std::all_of(moveConstructed.begin(), moveConstructed.end(), [](auto value) { return value == 1.0f; }));
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue