Skip to content

Commit

Permalink
Change to ETL_STATIC_ASSERT. Add static assert for first and last fun…
Browse files Browse the repository at this point in the history
…ctions
  • Loading branch information
mike919192 committed Feb 15, 2024
1 parent 5275598 commit f35ebfa
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions include/etl/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ SOFTWARE.
#include "memory.h"
#include "array.h"
#include "byte.h"
#include "static_assert.h"

#include "private/dynamic_extent.h"

Expand Down Expand Up @@ -295,6 +296,9 @@ namespace etl
template <size_t COUNT>
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> first() const ETL_NOEXCEPT
{
//if extent is static, check that original span contains at least COUNT elements
ETL_STATIC_ASSERT((extent != etl::dynamic_extent) ? COUNT <= extent : true, "Original span does not contain COUNT elements");

return etl::span<element_type, COUNT>(pbegin, pbegin + COUNT);
}

Expand All @@ -312,6 +316,9 @@ namespace etl
template <size_t COUNT>
ETL_NODISCARD ETL_CONSTEXPR etl::span<element_type, COUNT> last() const ETL_NOEXCEPT
{
//if extent is static, check that original span contains at least COUNT elements
ETL_STATIC_ASSERT((extent != etl::dynamic_extent) ? COUNT <= extent : true, "Original span does not contain COUNT elements");

return etl::span<element_type, COUNT>(pbegin + Extent - COUNT, (pbegin + Extent));
}

Expand All @@ -332,10 +339,10 @@ namespace etl
etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET> subspan() const ETL_NOEXCEPT
{
//if extent is static, check that OFFSET is within the original span
static_assert((extent != etl::dynamic_extent) ? OFFSET <= extent : true);
ETL_STATIC_ASSERT((extent != etl::dynamic_extent) ? OFFSET <= extent : true, "OFFSET is not within the original span");

//if count is also static, check that OFFSET + COUNT is within the original span
static_assert((extent != etl::dynamic_extent) && (COUNT != etl::dynamic_extent) ? COUNT <= (extent - OFFSET) : true);
ETL_STATIC_ASSERT((extent != etl::dynamic_extent) && (COUNT != etl::dynamic_extent) ? COUNT <= (extent - OFFSET) : true, "OFFSET + COUNT is not within the original span");

return (COUNT == etl::dynamic_extent) ? etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET>(pbegin + OFFSET, (pbegin + Extent))
: etl::span<element_type, COUNT != etl::dynamic_extent ? COUNT : Extent - OFFSET>(pbegin + OFFSET, pbegin + OFFSET + COUNT);
Expand Down

0 comments on commit f35ebfa

Please sign in to comment.