diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-20 10:48:19 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-20 10:48:19 -0500 |
commit | 6a27a2a4032e88fa9154ef0f0741edc584f7a701 (patch) | |
tree | 92ca58cbcdd2c1d11b7d69deb0d4925d0f979a3f /include/fud_span.hpp | |
parent | e94db4695e236b42ae1be44b2605075161d5144f (diff) |
Lots of work.
Diffstat (limited to 'include/fud_span.hpp')
-rw-r--r-- | include/fud_span.hpp | 75 |
1 files changed, 55 insertions, 20 deletions
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 <cstdlib> - #include "fud_array.hpp" +#include "fud_result.hpp" +#include "fud_status.hpp" + +#include <cstddef> +#include <cstdint> namespace fud { -template <typename T, size_t Size> +template <typename T, size_t Size = SIZE_MAX> struct Span { static_assert(Size > 0); using ValueType = T; - static Span make(Array<T, Size>& array) { - Span<T, Size> output{}; - output.m_data = array.data(); + static Span make(Array<T, Size>& array) + { + Span<T, Size> output{array.data(), Size}; return output; } + static Result<Span, FudStatus> make(Array<T, Size>& array, size_t size) + { + if (size > Size) { + return FudStatus::ArgumentInvalid; + } + return Span<T, Size>{array.data(), Size}; + } + template <typename U> - static Span make(const Array<U, Size>& array) { + static Span make(const Array<U, Size>& array) + { static_assert(std::convertible_to<U, T>); - Span<T, Size> output{}; - output.m_data = array.data(); - return output; + return Span<T, Size>{array.data(), Size}; } template <typename U> - static Span make(Array<U, Size>& array) { + static Result<Span, FudStatus> make(const Array<U, Size>& array, size_t size) + { static_assert(std::convertible_to<U, T>); - Span<T, Size> output{}; - output.m_data = array.data(); - return output; + if (size > Size) { + return FudStatus::ArgumentInvalid; + } + return Span<T, Size>{array.data(), Size}; + } + + template <typename U> + static Span make(Array<U, Size>& array) + { + static_assert(std::convertible_to<U, T>); + return Span<T, Size>{array.data(), array.size()}; + } + + template <typename U> + static Result<Span, FudStatus> make(Array<U, Size>& array, size_t size) + { + static_assert(std::convertible_to<U, T>); + if (size > Size) { + return FudStatus::ArgumentInvalid; + } + return Span<T, Size>{array.data(), array.size()}; } template <size_t ArraySize> - static Span makeCStringBuffer(Array<T, ArraySize>& array) { + static Span makeCStringBuffer(Array<T, ArraySize>& array) + { static_assert(ArraySize > Size); Span<T, Size> 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) |