Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 116 additions & 3 deletions platform/include/platform/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define MBED_PLATFORM_SPAN_H_

#include <algorithm>
#include <array>
#include <iterator>
#include <stddef.h>
#include <stdint.h>
Expand Down Expand Up @@ -310,7 +311,7 @@ struct Span {
// AStyle ignore, not handling correctly below
// *INDENT-OFF*
/**
* Construct a Span from the reference to an array.
* Construct a Span from the reference to a C/C++ array.
*
* @param elements Reference to the array viewed.
*
Expand All @@ -320,6 +321,44 @@ struct Span {
Span(element_type (&elements)[Extent]):
_data(elements) { }

/**
* Construct a Span from the reference to an std::array.
*
* @param array Reference to the array viewed.
*
* @post a call to size() returns Extent, and data() returns a
* pointer to elements as if by @p array.data().
*/
template<typename OtherElementType>
Span(std::array<OtherElementType, Extent> &array):
_data(array.data())
{
MBED_STATIC_ASSERT(
(span_detail::is_convertible<OtherElementType (*)[1], ElementType (*)[1]>::value),
"OtherElementType(*)[] should be convertible to ElementType (*)[]"
);
}

/**
* Construct a Span from the reference to a const std::array.
*
* @param array Const reference to the array viewed.
*
* @post a call to size() returns Extent, and data() returns a
* pointer to elements as if by @p array.data().
*
* @note ElementType must be const to accept a const std::array.
*/
template<typename OtherElementType>
Span(const std::array<OtherElementType, Extent> &array):
_data(array.data())
{
MBED_STATIC_ASSERT(
(span_detail::is_convertible<OtherElementType (*)[1], ElementType (*)[1]>::value),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can now rely on the std type traits stuff, no need for the span_detail any more.

"OtherElementType(*)[] should be convertible to ElementType (*)[]"
);
}

/**
* Construct a Span object from another Span of the same size.
*
Expand Down Expand Up @@ -652,7 +691,7 @@ struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
// AStyle ignore, not handling correctly below
// *INDENT-OFF*
/**
* Construct a Span from the reference to an array.
* Construct a Span from the reference to a C/C++ array.
*
* @param elements Reference to the array viewed.
*
Expand All @@ -665,6 +704,44 @@ struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
Span(element_type (&elements)[Count]):
_data(elements), _size(Count) { }

/**
* Construct a Span from the reference to a std::array.
*
* @param array Reference to the array viewed.
*
* @post a call to size() returns Count, and data() returns a
* pointer to elements as if by array.data().
*/
template<typename OtherElementType, size_t Count>
Span(std::array<OtherElementType, Count> &array):
_data(array.data()), _size(Count)
{
MBED_STATIC_ASSERT(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been open for ages, not sure what's holding it up: #13085

I'd suggest using static_assert directly in this new code.

(span_detail::is_convertible<OtherElementType (*)[1], ElementType (*)[1]>::value),
"OtherElementType(*)[] should be convertible to ElementType (*)[]"
);
}

/**
* Construct a Span from the reference to a const std::array.
*
* @param array Const reference to the array viewed.
*
* @post a call to size() returns Count, and data() returns a
* pointer to elements as if by array.data().
*
* @note ElementType must be const to accept a const std::array.
*/
template<typename OtherElementType, size_t Count>
Span(const std::array<OtherElementType, Count> &arr):
_data(arr.data()), _size(Count)
{
MBED_STATIC_ASSERT(
(span_detail::is_convertible<OtherElementType (*)[1], ElementType (*)[1]>::value),
"OtherElementType(*)[] should be convertible to ElementType (*)[]"
);
}

/**
* Construct a Span object from another Span.
*
Expand Down Expand Up @@ -1017,6 +1094,24 @@ Span<T, Size> make_Span(T (&elements)[Size])
return Span<T, Size>(elements);
}

/**
* Generate a Span from a reference to an std::array.
*
* @tparam T Type of elements held in elements.
* @tparam Extent Number of items held in elements.
*
* @param array The array viewed.
* @return The Span to elements.
*
* @note This helper avoids the typing of template parameter when Span is
* created 'inline'.
*/
template<typename T, size_t Extent>
Span<T, Extent> make_Span(std::array<T, Extent> &array)
{
return Span<T, Extent>(array);
}

/**
* Generate a Span from a pointer to a C/C++ array.
*
Expand Down Expand Up @@ -1048,7 +1143,7 @@ Span<T, Extent> make_Span(T *elements)
*
* @note This helper avoids the typing of template parameter when Span is
* created 'inline'.
*
*
* @relates Span
*/
template<typename T>
Expand All @@ -1074,6 +1169,24 @@ Span<const T, Extent> make_const_Span(const T (&elements)[Extent])
{
return Span<const T, Extent>(elements);
}

/**
* Generate a Span to a const content from a reference to an std::array.
*
* @tparam T Type of elements held in elements.
* @tparam Extent Number of items held in elements.
*
* @param array The array viewed.
* @return The Span to elements.
*
* @note This helper avoids the typing of template parameter when Span is
* created 'inline'.
*/
template<typename T, size_t Extent>
Span<const T, Extent> make_const_Span(const std::array<T, Extent> &array)
{
return Span<const T, Extent>(array);
}
// *INDENT-ON*
/**
* Generate a Span to a const content from a pointer to a C/C++ array.
Expand Down