From 438997577c31d5f3dbf52be2cbc393433d832b5d Mon Sep 17 00:00:00 2001 From: Marc Emmers Date: Thu, 5 Nov 2020 16:42:13 +0100 Subject: [PATCH] Add functions to create a span from an std::array type --- platform/include/platform/Span.h | 119 ++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 3 deletions(-) diff --git a/platform/include/platform/Span.h b/platform/include/platform/Span.h index 450b7f57a1c..6de65df56b8 100644 --- a/platform/include/platform/Span.h +++ b/platform/include/platform/Span.h @@ -19,6 +19,7 @@ #define MBED_PLATFORM_SPAN_H_ #include +#include #include #include #include @@ -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. * @@ -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 + Span(std::array &array): + _data(array.data()) + { + MBED_STATIC_ASSERT( + (span_detail::is_convertible::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 + Span(const std::array &array): + _data(array.data()) + { + MBED_STATIC_ASSERT( + (span_detail::is_convertible::value), + "OtherElementType(*)[] should be convertible to ElementType (*)[]" + ); + } + /** * Construct a Span object from another Span of the same size. * @@ -652,7 +691,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. * @@ -665,6 +704,44 @@ struct Span { 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 + Span(std::array &array): + _data(array.data()), _size(Count) + { + MBED_STATIC_ASSERT( + (span_detail::is_convertible::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 + Span(const std::array &arr): + _data(arr.data()), _size(Count) + { + MBED_STATIC_ASSERT( + (span_detail::is_convertible::value), + "OtherElementType(*)[] should be convertible to ElementType (*)[]" + ); + } + /** * Construct a Span object from another Span. * @@ -1017,6 +1094,24 @@ Span make_Span(T (&elements)[Size]) return Span(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 +Span make_Span(std::array &array) +{ + return Span(array); +} + /** * Generate a Span from a pointer to a C/C++ array. * @@ -1048,7 +1143,7 @@ Span make_Span(T *elements) * * @note This helper avoids the typing of template parameter when Span is * created 'inline'. - * + * * @relates Span */ template @@ -1074,6 +1169,24 @@ Span make_const_Span(const T (&elements)[Extent]) { return Span(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 +Span make_const_Span(const std::array &array) +{ + return Span(array); +} // *INDENT-ON* /** * Generate a Span to a const content from a pointer to a C/C++ array.