From 6a27a2a4032e88fa9154ef0f0741edc584f7a701 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Sun, 20 Oct 2024 10:48:19 -0500 Subject: Lots of work. --- include/fud_span.hpp | 75 ++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 20 deletions(-) (limited to 'include/fud_span.hpp') diff --git a/include/fud_span.hpp b/include/fud_span.hpp index cc693f8..5b8497e 100644 --- a/include/fud_span.hpp +++ b/include/fud_span.hpp @@ -18,41 +18,71 @@ #ifndef FUD_SPAN_HPP #define FUD_SPAN_HPP -#include - #include "fud_array.hpp" +#include "fud_result.hpp" +#include "fud_status.hpp" + +#include +#include namespace fud { -template +template struct Span { static_assert(Size > 0); using ValueType = T; - static Span make(Array& array) { - Span output{}; - output.m_data = array.data(); + static Span make(Array& array) + { + Span output{array.data(), Size}; return output; } + static Result make(Array& array, size_t size) + { + if (size > Size) { + return FudStatus::ArgumentInvalid; + } + return Span{array.data(), Size}; + } + template - static Span make(const Array& array) { + static Span make(const Array& array) + { static_assert(std::convertible_to); - Span output{}; - output.m_data = array.data(); - return output; + return Span{array.data(), Size}; } template - static Span make(Array& array) { + static Result make(const Array& array, size_t size) + { static_assert(std::convertible_to); - Span output{}; - output.m_data = array.data(); - return output; + if (size > Size) { + return FudStatus::ArgumentInvalid; + } + return Span{array.data(), Size}; + } + + template + static Span make(Array& array) + { + static_assert(std::convertible_to); + return Span{array.data(), array.size()}; + } + + template + static Result make(Array& array, size_t size) + { + static_assert(std::convertible_to); + if (size > Size) { + return FudStatus::ArgumentInvalid; + } + return Span{array.data(), array.size()}; } template - static Span makeCStringBuffer(Array& array) { + static Span makeCStringBuffer(Array& array) + { static_assert(ArraySize > Size); Span output{}; output.m_data = array.data(); @@ -60,10 +90,15 @@ struct Span { } T* m_data; + const size_t m_size; [[nodiscard]] constexpr size_t size() const { - return Size; + if constexpr (Size < SIZE_MAX) { + return Size; + } else { + return m_size; + } } constexpr T& front() @@ -78,12 +113,12 @@ struct Span { constexpr T& back() { - return m_data[Size - 1]; + return m_data[size() - 1]; } constexpr const T& back() const { - return m_data[Size - 1]; + return m_data[size() - 1]; } constexpr T* data() noexcept @@ -108,12 +143,12 @@ struct Span { constexpr T* end() noexcept { - return m_data + Size; + return m_data + size(); } constexpr const T* end() const noexcept { - return m_data + Size; + return m_data + size(); } constexpr T& operator[](size_t index) -- cgit v1.2.3