diff options
Diffstat (limited to 'include/fud_vector.hpp')
-rw-r--r-- | include/fud_vector.hpp | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/include/fud_vector.hpp b/include/fud_vector.hpp index 52876fd..53b2625 100644 --- a/include/fud_vector.hpp +++ b/include/fud_vector.hpp @@ -39,6 +39,7 @@ class Vector { public: constexpr Vector() noexcept = default; + constexpr explicit Vector(Allocator& allocator) noexcept : m_allocator{&allocator} {} constexpr Vector(const Vector<T>& rhs) = delete; constexpr Vector(Vector<T>&& rhs) noexcept : m_allocator(rhs.m_allocator), m_data(rhs.m_data), m_length{rhs.m_length}, m_capacity{rhs.m_capacity} @@ -121,12 +122,12 @@ class Vector { Vector<T> output{}; auto status = initializeWithCapacity(output, count, allocator); if (status != FudStatus::Success) { - return status; + return FudError{status}; } - return output; + return Okay<Vector<T>>{std::move(output)}; } - static Result<Vector<T>, FudStatus> initializeWithSize( + static FudStatus initializeWithSize( Vector<T>& output, size_t count, Allocator* allocator = &globalFudAllocator) @@ -145,7 +146,8 @@ class Vector { const auto* ptr = new (output.m_data + index) T(); fudAssert(ptr != nullptr); } - return output; + + return FudStatus::Success; } template <typename Builder> @@ -157,20 +159,20 @@ class Vector { Vector<T> output{}; auto status = initializeWithSizeFallible(output, count, std::forward<Builder>(builder), allocator); if (status != FudStatus::Success) { - return status; + return FudError{status}; } - return output; + return Okay<Vector<T>>{std::move(output)}; } template <typename Builder> - static Result<Vector<T>, FudStatus> initializeWithSizeFallible( + static FudStatus initializeWithSizeFallible( Vector<T>& output, size_t count, Builder&& builder, Allocator* allocator = &globalFudAllocator) { - using BuilderResult = decltype(std::forward<Builder>(builder)()); - static_assert(std::is_same_v<BuilderResult, FudStatus>()); + // using BuilderResult = decltype(std::forward<Builder>(builder)(T{})); + // static_assert(std::is_same_v<BuilderResult, FudStatus>); auto status = Vector::initializeWithCapacity(output, count, allocator); if (status != FudStatus::Success) { @@ -180,13 +182,13 @@ class Vector { output.m_length = count; for (size_t index = 0; index < count; ++index) { - auto builderResult{std::forward<Builder>(builder)(output.m_data[index])}; - if (builderResult.isError()) { - return builderResult.takeError(); + auto builderStatus{std::forward<Builder>(builder)(output.m_data[index])}; + if (builderStatus != FudStatus::Success) { + return builderStatus; } } - return output; + return FudStatus::Success; } static Result<Vector<T>, FudStatus> from(const Vector<T>& rhs, Option<Allocator*> allocatorOption = NullOpt) @@ -356,7 +358,7 @@ class Vector { auto status = FudStatus::Success; if (m_capacity > 0) { - status = m_allocator->deallocate(reinterpret_cast<std::byte*>(m_data), m_capacity); + m_allocator->deallocate(reinterpret_cast<std::byte*>(m_data), m_capacity); } m_data = dataPtr; @@ -672,10 +674,7 @@ class Vector { auto status = clear(); if (m_data != nullptr && m_allocator != nullptr) { - auto deallocStatus = m_allocator->deallocate(reinterpret_cast<std::byte*>(m_data), m_capacity); - if (status == FudStatus::Success) { - status = deallocStatus; - } + m_allocator->deallocate(reinterpret_cast<std::byte*>(m_data), m_capacity); } m_allocator = nullptr; |