-
Notifications
You must be signed in to change notification settings - Fork 3k
Add functions to create a Span from an std::array #13868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,7 @@ | |
| #define MBED_PLATFORM_SPAN_H_ | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <iterator> | ||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
|
|
@@ -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<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), | ||
| "OtherElementType(*)[] should be convertible to ElementType (*)[]" | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a Span object from another Span of the same size. | ||
| * | ||
|
|
@@ -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. | ||
| * | ||
|
|
@@ -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( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| (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. | ||
| * | ||
|
|
@@ -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. | ||
| * | ||
|
|
@@ -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> | ||
|
|
@@ -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. | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
stdtype traits stuff, no need for thespan_detailany more.