// Copyright (C) 2019 T. Zachary Laine // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) #ifndef BOOST_STL_INTERFACES_CONTAINER_INTERFACE_HPP #define BOOST_STL_INTERFACES_CONTAINER_INTERFACE_HPP #include #include #include #include #include #include namespace boost { namespace stl_interfaces { namespace detail { template struct n_iter : iterator_interface< n_iter, std::random_access_iterator_tag, T> { n_iter() : x_(nullptr), n_(0) {} n_iter(T const & x, SizeType n) : x_(&x), n_(n) {} constexpr std::ptrdiff_t operator-(n_iter other) const noexcept { return std::ptrdiff_t(n_) - std::ptrdiff_t(other.n_); } n_iter & operator+=(std::ptrdiff_t offset) { n_ += offset; return *this; } private: friend access; constexpr T const *& base_reference() noexcept { return x_; } constexpr T const * base_reference() const noexcept { return x_; } T const * x_; SizeType n_; }; template constexpr auto make_n_iter(T const & x, SizeType n) noexcept( noexcept(n_iter(x, n))) { using result_type = n_iter; return result_type(x, SizeType(0)); } template constexpr auto make_n_iter_end(T const & x, SizeType n) noexcept( noexcept(n_iter(x, n))) { return n_iter(x, n); } template std::size_t fake_capacity(Container const & c) { return SIZE_MAX; } template< typename Container, typename Enable = decltype( std::size_t() = std::declval().capacity())> std::size_t fake_capacity(Container const & c) { return c.capacity(); } }}} namespace boost { namespace stl_interfaces { inline namespace v1 { /** A CRTP template that one may derive from to make it easier to define container types. The template parameter `D` for `sequence_container_interface` may be an incomplete type. Before any member of the resulting specialization of `sequence_container_interface` other than special member functions is referenced, `D` shall be complete; shall model `std::derived_from>`, `std::semiregular`, and `std::forward_range`; and shall contain all the nested types required in Table 72: Container requirements and, for those whose iterator nested type models `std::bidirectinal_iterator`, those in Table 73: Reversible container requirements. For an object `d` of type `D`, a call to `std::ranges::begin(d)` sxhall not mutate any data members of `d`, and `d`'s destructor shall end the lifetimes of the objects in `[std::ranges::begin(d), std::ranges::end(d))`. */ template< typename Derived, element_layout Contiguity = element_layout::discontiguous #ifndef BOOST_STL_INTERFACES_DOXYGEN , typename E = std::enable_if_t< std::is_class::value && std::is_same>::value> #endif > struct sequence_container_interface; namespace v1_dtl { template using in_iter = std::is_convertible< typename std::iterator_traits::iterator_category, std::input_iterator_tag>; template struct clear_impl { static constexpr void call(D & d) noexcept {} }; template struct clear_impl().clear())>> { static constexpr void call(D & d) noexcept { d.clear(); } }; template void derived_container(sequence_container_interface const &); } template< typename Derived, element_layout Contiguity #ifndef BOOST_STL_INTERFACES_DOXYGEN , typename E #endif > struct sequence_container_interface { #ifndef BOOST_STL_INTERFACES_DOXYGEN private: constexpr Derived & derived() noexcept { return static_cast(*this); } constexpr const Derived & derived() const noexcept { return static_cast(*this); } constexpr Derived & mutable_derived() const noexcept { return const_cast(static_cast(*this)); } #endif public: template constexpr auto empty() noexcept( noexcept(std::declval().begin() == std::declval().end())) -> decltype( std::declval().begin() == std::declval().end()) { return derived().begin() == derived().end(); } template constexpr auto empty() const noexcept(noexcept( std::declval().begin() == std::declval().end())) -> decltype( std::declval().begin() == std::declval().end()) { return derived().begin() == derived().end(); } template< typename D = Derived, element_layout C = Contiguity, typename Enable = std::enable_if_t> constexpr auto data() noexcept(noexcept(std::declval().begin())) -> decltype(std::addressof(*std::declval().begin())) { return std::addressof(*derived().begin()); } template< typename D = Derived, element_layout C = Contiguity, typename Enable = std::enable_if_t> constexpr auto data() const noexcept(noexcept(std::declval().begin())) -> decltype(std::addressof(*std::declval().begin())) { return std::addressof(*derived().begin()); } template constexpr auto size() #if !BOOST_CLANG noexcept(noexcept( std::declval().end() - std::declval().begin())) #endif -> decltype(typename D::size_type( std::declval().end() - std::declval().begin())) { return derived().end() - derived().begin(); } template constexpr auto size() const noexcept(noexcept( std::declval().end() - std::declval().begin())) -> decltype(typename D::size_type( #if !BOOST_CLANG std::declval().end() - std::declval().begin() #endif )) { return derived().end() - derived().begin(); } template constexpr auto front() noexcept(noexcept(*std::declval().begin())) -> decltype(*std::declval().begin()) { return *derived().begin(); } template constexpr auto front() const noexcept(noexcept(*std::declval().begin())) -> decltype(*std::declval().begin()) { return *derived().begin(); } template constexpr auto push_front(typename D::value_type const & x) noexcept( noexcept(std::declval().emplace_front(x))) -> decltype((void)std::declval().emplace_front(x)) { derived().emplace_front(x); } template constexpr auto push_front(typename D::value_type && x) noexcept( noexcept(std::declval().emplace_front(std::move(x)))) -> decltype((void)std::declval().emplace_front(std::move(x))) { derived().emplace_front(std::move(x)); } template constexpr auto pop_front() noexcept -> decltype( std::declval().emplace_front( std::declval()), (void)std::declval().erase(std::declval().begin())) { derived().erase(derived().begin()); } template< typename D = Derived, typename Enable = std::enable_if_t< v1_dtl::decrementable_sentinel::value && v1_dtl::common_range::value>> constexpr auto back() noexcept(noexcept(*std::prev(std::declval().end()))) -> decltype(*std::prev(std::declval().end())) { return *std::prev(derived().end()); } template< typename D = Derived, typename Enable = std::enable_if_t< v1_dtl::decrementable_sentinel::value && v1_dtl::common_range::value>> constexpr auto back() const noexcept(noexcept(*std::prev(std::declval().end()))) -> decltype(*std::prev(std::declval().end())) { return *std::prev(derived().end()); } template constexpr auto push_back(typename D::value_type const & x) noexcept( noexcept(std::declval().emplace_back(x))) -> decltype((void)std::declval().emplace_back(x)) { derived().emplace_back(x); } template constexpr auto push_back(typename D::value_type && x) noexcept( noexcept(std::declval().emplace_back(std::move(x)))) -> decltype((void)std::declval().emplace_back(std::move(x))) { derived().emplace_back(std::move(x)); } template constexpr auto pop_back() noexcept -> decltype( std::declval().emplace_back( std::declval()), (void)std::declval().erase( std::prev(std::declval().end()))) { derived().erase(std::prev(derived().end())); } template constexpr auto operator[](typename D::size_type n) noexcept( noexcept(std::declval().begin()[n])) -> decltype(std::declval().begin()[n]) { return derived().begin()[n]; } template constexpr auto operator[](typename D::size_type n) const noexcept(noexcept(std::declval().begin()[n])) -> decltype(std::declval().begin()[n]) { return derived().begin()[n]; } template constexpr auto at(typename D::size_type i) -> decltype(std::declval().size(), std::declval()[i]) { if (derived().size() <= i) { throw std::out_of_range( "Bounds check failed in sequence_container_interface::at()"); } return derived()[i]; } template constexpr auto at(typename D::size_type i) const -> decltype( std::declval().size(), std::declval()[i]) { if (derived().size() <= i) { throw std::out_of_range( "Bounds check failed in sequence_container_interface::at()"); } return derived()[i]; } template constexpr Iter begin() const noexcept(noexcept(std::declval().begin())) { return Iter(mutable_derived().begin()); } template constexpr Iter end() const noexcept(noexcept(std::declval().end())) { return Iter(mutable_derived().end()); } template constexpr auto cbegin() const noexcept(noexcept(std::declval().begin())) -> decltype(std::declval().begin()) { return derived().begin(); } template constexpr auto cend() const noexcept(noexcept(std::declval().end())) -> decltype(std::declval().end()) { return derived().end(); } template< typename D = Derived, typename Enable = std::enable_if_t::value>> constexpr auto rbegin() noexcept(noexcept( stl_interfaces::make_reverse_iterator(std::declval().end()))) { return stl_interfaces::make_reverse_iterator(derived().end()); } template< typename D = Derived, typename Enable = std::enable_if_t::value>> constexpr auto rend() noexcept(noexcept( stl_interfaces::make_reverse_iterator(std::declval().begin()))) { return stl_interfaces::make_reverse_iterator(derived().begin()); } template constexpr auto rbegin() const noexcept(noexcept(std::declval().rbegin())) { return typename D::const_reverse_iterator(mutable_derived().rbegin()); } template constexpr auto rend() const noexcept(noexcept(std::declval().rend())) { return typename D::const_reverse_iterator(mutable_derived().rend()); } template constexpr auto crbegin() const noexcept(noexcept(std::declval().rbegin())) -> decltype(std::declval().rbegin()) { return derived().rbegin(); } template constexpr auto crend() const noexcept(noexcept(std::declval().rend())) -> decltype(std::declval().rend()) { return derived().rend(); } template constexpr auto insert( typename D::const_iterator pos, typename D::value_type const & x) noexcept(noexcept(std::declval().emplace(pos, x))) -> decltype(std::declval().emplace(pos, x)) { return derived().emplace(pos, x); } template constexpr auto insert( typename D::const_iterator pos, typename D::value_type && x) noexcept(noexcept(std::declval() .emplace(pos, std::move(x)))) -> decltype(std::declval().emplace(pos, std::move(x))) { return derived().emplace(pos, std::move(x)); } template constexpr auto insert( typename D::const_iterator pos, typename D::size_type n, typename D::value_type const & x) // If you see an error in this noexcept() expression, that's // because this function is not properly constrained. In other // words, Derived does not have a "range" insert like // insert(position, first, last). If that is the case, this // function should be removed via SFINAE from overload resolution. // However, both the trailing decltype code below and a // std::enable_if in the template parameters do not work. Sorry // about that. See below for details. noexcept(noexcept(std::declval().insert( pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))) // This causes the compiler to infinitely recurse into this function's // declaration, even though the call below does not match the // signature of this function. #if 0 -> decltype(std::declval().insert( pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n))) #endif { return derived().insert( pos, detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } template constexpr auto insert( typename D::const_iterator pos, std::initializer_list il) noexcept(noexcept(std::declval() .insert(pos, il.begin(), il.end()))) -> decltype(std::declval().insert(pos, il.begin(), il.end())) { return derived().insert(pos, il.begin(), il.end()); } template constexpr auto erase(typename D::const_iterator pos) noexcept -> decltype(std::declval().erase(pos, std::next(pos))) { return derived().erase(pos, std::next(pos)); } template< typename InputIterator, typename D = Derived, typename Enable = std::enable_if_t::value>> constexpr auto assign(InputIterator first, InputIterator last) noexcept( noexcept(std::declval().insert( std::declval().begin(), first, last))) -> decltype( std::declval().erase( std::declval().begin(), std::declval().end()), (void)std::declval().insert( std::declval().begin(), first, last)) { auto out = derived().begin(); auto const out_last = derived().end(); for (; out != out_last && first != last; ++first, ++out) { *out = *first; } if (out != out_last) derived().erase(out, out_last); if (first != last) derived().insert(derived().end(), first, last); } template constexpr auto assign( typename D::size_type n, typename D::value_type const & x) noexcept(noexcept(std::declval() .insert( std::declval().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)))) -> decltype( std::declval().size(), std::declval().erase( std::declval().begin(), std::declval().end()), (void)std::declval().insert( std::declval().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n))) { if (detail::fake_capacity(derived()) < n) { Derived temp(n, x); derived().swap(temp); } else { auto const min_size = std::min(n, derived().size()); auto const fill_end = std::fill_n(derived().begin(), min_size, x); if (min_size < (std::ptrdiff_t)derived().size()) { derived().erase(fill_end, derived().end()); } else { n -= min_size; derived().insert( derived().begin(), detail::make_n_iter(x, n), detail::make_n_iter_end(x, n)); } } } template constexpr auto assign(std::initializer_list il) noexcept( noexcept(std::declval().assign(il.begin(), il.end()))) -> decltype((void)std::declval().assign(il.begin(), il.end())) { derived().assign(il.begin(), il.end()); } template constexpr auto operator=(std::initializer_list il) noexcept( noexcept(std::declval().assign(il.begin(), il.end()))) -> decltype( std::declval().assign(il.begin(), il.end()), std::declval()) { derived().assign(il.begin(), il.end()); return *this; } template constexpr auto clear() noexcept -> decltype((void)std::declval().erase( std::declval().begin(), std::declval().end())) { derived().erase(derived().begin(), derived().end()); } }; /** Implementation of free function `swap()` for all containers derived from `sequence_container_interface`. */ template constexpr auto swap( ContainerInterface & lhs, ContainerInterface & rhs) noexcept(noexcept(lhs.swap(rhs))) -> decltype(v1_dtl::derived_container(lhs), lhs.swap(rhs)) { return lhs.swap(rhs); } /** Implementation of `operator==()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator==(ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept( noexcept(lhs.size() == rhs.size()) && noexcept(*lhs.begin() == *rhs.begin())) -> decltype( v1_dtl::derived_container(lhs), lhs.size() == rhs.size(), *lhs.begin() == *rhs.begin(), true) { return lhs.size() == rhs.size() && std::equal(lhs.begin(), lhs.end(), rhs.begin()); } /** Implementation of `operator!=()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator!=( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs == rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs == rhs) { return !(lhs == rhs); } /** Implementation of `operator<()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator<( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(*lhs.begin() < *rhs.begin())) -> decltype( v1_dtl::derived_container(lhs), *lhs.begin() < *rhs.begin(), true) { auto it1 = lhs.begin(); auto const last1 = lhs.end(); auto it2 = rhs.begin(); auto const last2 = rhs.end(); for (; it1 != last1 && it2 != last2; ++it1, ++it2) { if (*it1 < *it2) return true; if (*it2 < *it1) return false; } return it1 == last1 && it2 != last2; } /** Implementation of `operator<=()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator<=( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs < rhs) { return !(rhs < lhs); } /** Implementation of `operator>()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator>( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs < rhs) { return rhs < lhs; } /** Implementation of `operator>=()` for all containers derived from `sequence_container_interface`. */ template constexpr auto operator>=( ContainerInterface const & lhs, ContainerInterface const & rhs) noexcept(noexcept(lhs < rhs)) -> decltype(v1_dtl::derived_container(lhs), lhs < rhs) { return !(lhs < rhs); } }}} #endif