/* This is an implementation of C++20's std::span http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf */ // Copyright Tristan Brindle 2018. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file ../../LICENSE_1_0.txt or copy at // https://www.boost.org/LICENSE_1_0.txt) // https://github.com/tcbrindle/span/blob/master/include/tcb/span.hpp #pragma once #include #include #include #include #if !defined(NDEBUG) #include #endif namespace tcb { // Establish default contract checking behavior #if !defined(NDEBUG) [[noreturn]] inline void contract_violation(const char* /*unused*/) { std::terminate(); } #define TCB_SPAN_STRINGIFY(cond) #cond #define TCB_SPAN_EXPECT(cond) \ cond ? (void) 0 : contract_violation("Expected " TCB_SPAN_STRINGIFY(cond)) #else #define TCB_SPAN_EXPECT(cond) #endif inline constexpr std::size_t dynamic_extent = SIZE_MAX; template class Span; namespace detail { template struct span_storage { constexpr span_storage() noexcept = default; constexpr span_storage(E* p_ptr, std::size_t /*unused*/) noexcept : ptr(p_ptr) {} E* ptr = nullptr; static constexpr std::size_t size = S; }; template struct span_storage { constexpr span_storage() noexcept = default; constexpr span_storage(E* p_ptr, std::size_t p_size) noexcept : ptr(p_ptr), size(p_size) {} E* ptr = nullptr; std::size_t size = 0; }; using std::data; using std::size; using std::void_t; template using uncvref_t = typename std::remove_cv::type>::type; 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_and_data : std::false_type {}; template struct has_size_and_data())), decltype(detail::data(std::declval()))>> : std::true_type {}; template > struct is_container { static constexpr bool value = !is_span::value && !is_std_array::value && !std::is_array::value && has_size_and_data::value; }; template using remove_pointer_t = typename std::remove_pointer::type; template struct is_container_element_type_compatible : std::false_type {}; template struct is_container_element_type_compatible< T, E, typename std::enable_if< !std::is_same()))>::type, void>::value>::type> : std::is_convertible< remove_pointer_t()))> (*)[], E (*)[]> {}; template struct is_complete : std::false_type {}; template struct is_complete : std::true_type {}; } // namespace detail template class Span { static_assert(std::is_object::value, "A span's ElementType must be an object type (not a " "reference type or void)"); static_assert(detail::is_complete::value, "A span's ElementType must be a complete type (not a forward " "declaration)"); static_assert(!std::is_abstract::value, "A span's ElementType cannot be an abstract class type"); using storage_type = detail::span_storage; public: // constants and types using element_type = ElementType; using value_type = typename std::remove_cv::type; using size_type = std::size_t; using difference_type = std::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 size_type extent = Extent; // [span.cons], span constructors, copy, assignment, and destructor template < std::size_t E = Extent, typename std::enable_if<(E == dynamic_extent || E <= 0), int>::type = 0> constexpr Span() noexcept {} constexpr Span(pointer ptr, size_type count) : storage_(ptr, count) { TCB_SPAN_EXPECT(extent == dynamic_extent || count == extent); } constexpr Span(pointer first_elem, pointer last_elem) : storage_(first_elem, last_elem - first_elem) { TCB_SPAN_EXPECT(extent == dynamic_extent || last_elem - first_elem == static_cast(extent)); } template ::value, int>::type = 0> constexpr Span(element_type (&arr)[N]) noexcept : storage_(arr, N) {} template &, ElementType>::value, int>::type = 0> constexpr Span(std::array& arr) noexcept : storage_(arr.data(), N) {} template &, ElementType>::value, int>::type = 0> constexpr Span(const std::array& arr) noexcept : storage_(arr.data(), N) {} template < typename Container, std::size_t E = Extent, typename std::enable_if< E == dynamic_extent && detail::is_container::value && detail::is_container_element_type_compatible< Container&, ElementType>::value, int>::type = 0> constexpr Span(Container& cont) : storage_(detail::data(cont), detail::size(cont)) {} template < typename Container, std::size_t E = Extent, typename std::enable_if< E == dynamic_extent && detail::is_container::value && detail::is_container_element_type_compatible< const Container&, ElementType>::value, int>::type = 0> constexpr Span(const Container& cont) : storage_(detail::data(cont), detail::size(cont)) {} constexpr Span(const Span& other) noexcept = default; template ::value, int>::type = 0> constexpr Span(const Span& other) noexcept : storage_(other.data(), other.size()) {} ~Span() noexcept = default; constexpr Span& operator=(const Span& other) noexcept = default; // [span.sub], span subviews template constexpr Span first() const { TCB_SPAN_EXPECT(Count <= size()); return {data(), Count}; } template constexpr Span last() const { TCB_SPAN_EXPECT(Count <= size()); return {data() + (size() - Count), Count}; } template using subspan_return_t = Span; template constexpr subspan_return_t subspan() const { TCB_SPAN_EXPECT(Offset <= size() && (Count == dynamic_extent || Offset + Count <= size())); return {data() + Offset, Count != dynamic_extent ? Count : size() - Offset}; } constexpr Span first(size_type count) const { TCB_SPAN_EXPECT(count <= size()); return {data(), count}; } constexpr Span last(size_type count) const { TCB_SPAN_EXPECT(count <= size()); return {data() + (size() - count), count}; } constexpr Span subspan(size_type offset, size_type count = dynamic_extent) const { TCB_SPAN_EXPECT(offset <= size() && (count == dynamic_extent || offset + count <= size())); return {data() + offset, count == dynamic_extent ? size() - offset : count}; } // [span.obs], span observers constexpr size_type size() const noexcept { return storage_.size; } constexpr size_type size_bytes() const noexcept { return size() * sizeof(element_type); } [[nodiscard]] constexpr bool empty() const noexcept { return size() == 0; } // [span.elem], span element access constexpr reference operator[](size_type idx) const { TCB_SPAN_EXPECT(idx < size()); return *(data() + idx); } constexpr reference front() const { TCB_SPAN_EXPECT(!empty()); return *data(); } constexpr reference back() const { TCB_SPAN_EXPECT(!empty()); return *(data() + (size() - 1)); } constexpr pointer data() const noexcept { return storage_.ptr; } // [span.iterators], span iterator support 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: storage_type storage_{}; }; /* Deduction Guides */ template Span(T (&)[N])->Span; template Span(std::array&)->Span; template Span(const std::array&)->Span; template Span(Container&)->Span; template Span(const Container&)->Span; template constexpr Span make_span(Span s) noexcept { return s; } template constexpr Span make_span(T (&arr)[N]) noexcept { return {arr}; } template constexpr Span make_span(std::array& arr) noexcept { return {arr}; } template constexpr Span make_span(const std::array& arr) noexcept { return {arr}; } template constexpr Span make_span(Container& cont) { return {cont}; } template constexpr Span make_span(const Container& cont) { return {cont}; } template Span as_bytes(Span s) noexcept { return {reinterpret_cast(s.data()), s.size_bytes()}; } template < class ElementType, size_t Extent, typename std::enable_if::value, int>::type = 0> Span as_writable_bytes(Span s) noexcept { return {reinterpret_cast(s.data()), s.size_bytes()}; } template constexpr auto get(Span s) -> decltype(s[N]) { return s[N]; } } // namespace tcb namespace std { template class tuple_size> : public integral_constant {}; template class tuple_size>; // not defined template class tuple_element> { public: static_assert(Extent != tcb::dynamic_extent && I < Extent, ""); using type = ElementType; }; } // end namespace std