From 8ce397e8c0a83e49e390de9deb73d588e4931ecf Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Tue, 29 Oct 2024 21:02:25 -0500 Subject: Reworking of Result. --- include/fud_vector.hpp | 66 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 40 insertions(+), 26 deletions(-) (limited to 'include/fud_vector.hpp') diff --git a/include/fud_vector.hpp b/include/fud_vector.hpp index f90819a..2b5de9a 100644 --- a/include/fud_vector.hpp +++ b/include/fud_vector.hpp @@ -257,62 +257,68 @@ class Vector { Result, FudStatus> span() const { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } - return Span{m_data, m_length}; + return RetType::okay(Span{m_data, m_length}); } Result, FudStatus> span() { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } - return Span{m_data, m_length}; + return RetType::okay(Span{m_data, m_length}); } Result, FudStatus> span(size_t count) const { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (count > m_length) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } - return Span{m_data, count}; + return RetType::okay(Span{m_data, count}); } Result, FudStatus> span(size_t count) { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (count > m_length) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } - return Span{m_data, count}; + return RetType::okay(Span{m_data, count}); } Result, FudStatus> span(size_t start, size_t count) const { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (SIZE_MAX - start < m_length || start + count > m_length) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } - return Span{m_data + start, count}; + return RetType::okay(Span{m_data + start, count}); } Result, FudStatus> span(size_t start, size_t count) { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (SIZE_MAX - start < m_length || start + count > m_length) { - return FudStatus::ArgumentInvalid; + return RetType::error(FudStatus::ArgumentInvalid); } - return Span{m_data + start, count}; + return RetType::okay(Span{m_data + start, count}); } FudStatus reserve(size_t count) @@ -342,10 +348,15 @@ class Vector { m_data[index].~T(); } + auto status = FudStatus::Success; + if (m_capacity > 0) { + status = m_allocator->deallocate(m_data, m_capacity); + } + m_data = dataPtr; m_capacity = count; - return FudStatus::Success; + return status; } FudStatus resize(size_t count) @@ -394,24 +405,26 @@ class Vector { Result, FudStatus> get(size_t index) { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (index >= m_length) { - return FudStatus::IndexInvalid; + return RetType::error(FudStatus::IndexInvalid); } - return std::ref(m_data[index]); + return RetType::okay(std::ref(m_data[index])); } Result, FudStatus> ref(size_t index) const { + using RetType = Result, FudStatus>; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (index >= m_length) { - return FudStatus::IndexInvalid; + return RetType::error(FudStatus::IndexInvalid); } - return std::cref(m_data[index]); + return RetType::okay(std::cref(m_data[index])); } constexpr Option front() @@ -527,13 +540,14 @@ class Vector { Result popBack() { + using RetType = Result; if (m_data == nullptr) { - return FudStatus::ObjectInvalid; + return RetType::error(FudStatus::ObjectInvalid); } if (m_length == 0) { - return FudStatus::Empty; + return RetType::error(FudStatus::Empty); } - auto result{std::move(m_data[m_length - 1])}; + auto result{RetType::okay({std::move(m_data[m_length - 1])})}; m_length--; m_data[m_length].~T(); return result; -- cgit v1.2.3