From 79553ab1fa3d707612e80dd03bf95ca65fa0c411 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Tue, 4 Feb 2025 23:11:08 +0100 Subject: refactor file browser --- lib/nes/common/span.hh | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'lib/nes/common/span.hh') diff --git a/lib/nes/common/span.hh b/lib/nes/common/span.hh index 16eb129..6779c37 100644 --- a/lib/nes/common/span.hh +++ b/lib/nes/common/span.hh @@ -1,6 +1,7 @@ #pragma once #include "nes/common/types.hh" +#include "nes/common/debug.hh" namespace nes { @@ -21,6 +22,9 @@ namespace nes { using type = T; }; + + template + using remove_const_type = typename remove_const::type; } // namespace detail /// Implementation of a container similar to std::span in C++20. @@ -54,7 +58,7 @@ namespace nes static_assert(Length == ActualLength || Length == detail::span_dynamic_length); } - span(span::type, Length> const other) + span(span, Length> const other) : span{ other.data_, other.length_, {} } { } @@ -65,12 +69,31 @@ namespace nes auto begin() const -> iterator { return data_; } auto end() const -> iterator { return data_ + length_; } - auto subspan(u32 const first, u32 const length) const -> span { return span{ data_ + first, length }; } - auto subspan(u32 const first) const -> span { return subspan(length_ - first); } + auto subspan(u32 const first, u32 const length) const -> span + { + NES_ASSERT(first + length <= get_length() && "subspan out of bounds"); + return span{ data_ + first, length }; + } + + auto subspan(u32 const first) const -> span + { + NES_ASSERT(first <= get_length() && "subspan out of bounds"); + return subspan(length_ - first); + } + template - auto subspan(u32 const first) const -> span { return span{ data_ + first, OtherLength, {} }; } + auto subspan(u32 const first) const -> span + { + static_assert(OtherLength < Length); + NES_ASSERT(first + OtherLength <= get_length() && "subspan out of bounds"); + return span{ data_ + first, OtherLength, {} }; + } - auto operator[](u32 const index) const -> T& { return data_[index]; } + auto operator[](u32 const index) const -> T& + { + NES_ASSERT(index < get_length() && "index out of bounds"); + return data_[index]; + } private: explicit span(T* data, u32 const length, detail::span_unchecked) -- cgit v1.3.1