Skip to content

Commit

Permalink
Add etl::make_span() (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandreichweinbmw authored Feb 11, 2025
1 parent 99d7537 commit f02c89a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/etl/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ namespace etl
pointer pbegin;
};

//*************************************************************************
/// Pseudo constructor for constructing from C array without explicitly
/// specifying type and size
//*************************************************************************
template <typename T, size_t Extent>
ETL_CONSTEXPR span<T, Extent> make_span(T (&data)[Extent])
{
return span<T, Extent>(data);
}

//***************************************************************************
/// Span - Dynamic Extent
//***************************************************************************
Expand Down Expand Up @@ -700,6 +710,26 @@ namespace etl
pointer pend;
};

//*************************************************************************
/// Pseudo constructor for constructing from container without explicitly
/// specifying type and size
//*************************************************************************
template <typename T>
ETL_CONSTEXPR span<typename T::value_type, etl::dynamic_extent> make_span(T& data)
{
return span<typename T::value_type, etl::dynamic_extent>(data);
}

//*************************************************************************
/// Pseudo constructor for constructing from const container without
/// explicitly specifying type and size
//*************************************************************************
template <typename T>
ETL_CONSTEXPR span<typename T::value_type const, etl::dynamic_extent> make_span(const T& data)
{
return span<typename T::value_type const, etl::dynamic_extent>(data);
}

template <typename T, size_t Extent>
ETL_CONSTANT size_t span<T, Extent>::extent;

Expand Down
19 changes: 19 additions & 0 deletions test/test_span_dynamic_extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,25 @@ namespace
}
}

//*************************************************************************
TEST(test_make_span_container)
{
{
auto s = etl::make_span(etldata);

CHECK_EQUAL(s.size(), 10);
View view(etldata);
CHECK_TRUE(etl::equal(s, view));
}
{
auto s = etl::make_span(cetldata);

CHECK_EQUAL(s.size(), 10);
View view(etldata);
CHECK_TRUE(etl::equal(s, view));
}
}

#include "etl/private/diagnostic_pop.h"
};
}
19 changes: 19 additions & 0 deletions test/test_span_fixed_extent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,25 @@ namespace
}
}

//*************************************************************************
TEST(test_make_span_c_array)
{
{
auto s = etl::make_span(cdata);

CHECK_EQUAL(s.size(), 10);
View view(etldata);
CHECK_TRUE(etl::equal(s, view));
}
{
auto s = etl::make_span(ccdata);

CHECK_EQUAL(s.size(), 10);
View view(etldata);
CHECK_TRUE(etl::equal(s, view));
}
}

#include "etl/private/diagnostic_pop.h"
};
}

0 comments on commit f02c89a

Please sign in to comment.