/* mbed Microcontroller Library * Copyright (c) 2019 ARM Limited * SPDX-License-Identifier: Apache-2.0 * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef MSTD_SPAN_ #define MSTD_SPAN_ #include #include #include #include "mbed_assert.h" namespace mstd { constexpr size_t dynamic_extent = -1; template class span; namespace detail { template class storage { public: constexpr storage() noexcept = default; constexpr storage(ElementType *ptr, size_t) noexcept : _data(ptr) {} ElementType* _data; static constexpr size_t _size = Extent; }; template class storage { public: constexpr storage() noexcept = default; constexpr storage(ElementType *ptr, size_t size) noexcept : _data(ptr), _size(size) {} ElementType* _data; size_t _size; }; template struct is_span: std::false_type {}; template struct is_span>: std::true_type {}; template struct is_std_array: std::false_type {}; template struct is_std_array>: std::true_type {}; template struct has_size : std::false_type {}; template struct has_size()))>>: std::true_type {}; template struct has_data : std::false_type {}; template struct has_data()))>>: std::true_type {}; template> struct is_container{ static constexpr bool value = not is_span::value && not is_std_array::value && not std::is_array::value && has_size::value && has_data::value; }; template using iterator_t = decltype(mstd::begin(std::declval())); template using range_reference_t = mstd::iter_reference_t>; template struct is_compatible : std::false_type {}; template struct is_compatible())) >, void>::value>>: mstd::is_convertible()))> (*)[], E (*)[]>{}; } // namespace detail template class span { public: using element_type = ElementType; using value_type = typename mstd::remove_cv_t; using index_type = size_t; using difference_type = ptrdiff_t; using pointer = element_type*; using const_pointer = const element_type*; using reference = element_type&; using const_reference = const element_type&; using iterator = pointer; using reverse_iterator = std::reverse_iterator; static constexpr index_type extent = Extent; // Constructors, copy and assignment template = 0> constexpr span() noexcept {} template>(*)[], ElementType(*)[]>::value, int> = 0> constexpr span(It ptr, index_type count) : _storage(ptr, count) { MBED_ASSERT(extent == dynamic_extent || extent == count); } template>(*)[], ElementType(*)[]>::value, int> = 0> constexpr span(It first, It last) : _storage(first, last - first) { MBED_ASSERT(first <= last); MBED_ASSERT(extent == dynamic_extent || extent == last - first); MBED_ASSERT(extent == 0 || nullptr != first); } template constexpr span(type_identity_t (&arr)[N], typename mstd::enable_if_t<(Extent == dynamic_extent || Extent == N) && mstd::is_convertible(*)[], ElementType(*)[]>::value, int> = 0) noexcept: _storage(arr, N) {} template constexpr span(std::array& arr, typename mstd::enable_if_t<(Extent == dynamic_extent || Extent == N) && mstd::is_convertible(*)[], ElementType(*)[]>::value, int> = 0) noexcept: _storage(arr.data(), N) {} template constexpr span(const std::array& arr, typename mstd::enable_if_t<(Extent == dynamic_extent || Extent == N) && mstd::is_convertible(*)[], ElementType(*)[]>::value, int> = 0) noexcept: _storage(arr.data(), N) {} template::value && detail::is_compatible::value, int> = 0> constexpr span(R&& r) : _storage( mstd::data( r ), mstd::size( r )) { MBED_ASSERT(extent == dynamic_extent || extent == mstd::size( r )); } constexpr span(const span& other) noexcept = default; template::value, int> = 0> constexpr span(const span& s) noexcept: _storage(s.data(), s.size()) {} ~span() noexcept = default; constexpr span& operator=(const span& other) noexcept = default; // Subviews template constexpr span first() const { static_assert(Count <= extent); MBED_ASSERT(Count <= size()); return span(data(), Count); } template constexpr span last() const { static_assert(Count <= extent); MBED_ASSERT(Count <= size()); return span(data() + (size() - Count), Count); } template constexpr span subspan() const { static_assert(Offset <= extent && (Count == dynamic_extent || Count <= extent - Offset)); // Only check against Offset == 0 to prevent a warning for subspan<0, N> MBED_ASSERT((Offset == 0 || Offset <= size()) && (Count == dynamic_extent || Count <= size() - Offset)); return {data() + Offset, Count != dynamic_extent ? Count : size() - Offset}; } constexpr span first(index_type count) const { MBED_ASSERT(count <= size()); return span(data(), count); } constexpr span last(index_type count) const { MBED_ASSERT(count <= size()); return span(data() + (size() - count), count); } constexpr span subspan(index_type offset, index_type count = dynamic_extent) const { MBED_ASSERT(offset <= size() && (count == dynamic_extent || count <= size() - offset )); return span(data() + offset, count == dynamic_extent ? size() - offset : count ); } // Observers constexpr index_type size() const noexcept { return _storage._size; } constexpr index_type size_bytes() const noexcept { return size() * sizeof(element_type); } constexpr bool empty() const noexcept { return size() == 0; } // Element access constexpr reference operator[](index_type idx) const { MBED_ASSERT(idx < size()); return *(data() + idx); } constexpr reference front() const { MBED_ASSERT(not empty()); return *data(); } constexpr reference back() const { MBED_ASSERT(not empty()); return *(data() + (size() - 1)); } constexpr pointer data() const noexcept { return _storage._data; } // Iterators constexpr iterator begin() const noexcept { return data(); } constexpr iterator end() const noexcept { return data() + size(); } constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); } constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); } private: detail::storage _storage; }; template span as_bytes(span s) noexcept { return {reinterpret_cast(s.data()), s.size_bytes()}; } template span as_writable_bytes(span s) noexcept { static_assert(not is_const::value); return {reinterpret_cast(s.data()), s.size_bytes()}; } template constexpr span make_span(ElementType (&arr)[Extent]) { return span(arr); } template constexpr span make_span(const ElementType (&arr)[Extent]) { return span(arr); } template constexpr span make_span(std::array& arr) { return span(arr); } template constexpr span make_span(const std::array& arr) { return span(arr); } template constexpr span make_span(R& cont) { return {cont}; } template constexpr span make_span(const R& cont) { return {cont}; } } // namespace mstd #endif // MSTD_SPAN_