/* Copyright 2016-2018 Joaquin M Lopez Munoz. * 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) * * See http://www.boost.org/libs/poly_collection for library home page. */ #ifndef BOOST_POLY_COLLECTION_DETAIL_SEGMENT_HPP #define BOOST_POLY_COLLECTION_DETAIL_SEGMENT_HPP #if defined(_MSC_VER) #pragma once #endif #include #include #include #include namespace boost{ namespace poly_collection{ namespace detail{ /* segment encapsulates implementations of * Model::segment_backend virtual interface under a value-semantics type for * use by poly_collection. The techique is described by Sean Parent at slides * 157-205 of * https://github.com/sean-parent/sean-parent.github.com/wiki/ * presentations/2013-09-11-cpp-seasoning/cpp-seasoning.pdf * with one twist: when the type of the implementation can be known at compile * time, a downcast is done and non-virtual member functions (named with a nv_ * prefix) are used: this increases the performance of some operations. */ template class segment { public: using value_type=typename Model::value_type; using allocator_type=Allocator; /* needed for uses-allocator construction */ using base_iterator=typename Model::base_iterator; using const_base_iterator=typename Model::const_base_iterator; using base_sentinel=typename Model::base_sentinel; using const_base_sentinel=typename Model::const_base_sentinel; template using iterator=typename Model::template iterator; template using const_iterator=typename Model::template const_iterator; template static segment make(const allocator_type& al) { return segment_backend_implementation::make(al); } /* clones the implementation of x with no elements */ static segment make_from_prototype(const segment& x,const allocator_type& al) { return {from_prototype{},x,al}; } segment(const segment& x): pimpl{x.impl().copy()}{set_sentinel();} segment(segment&& x)=default; segment(const segment& x,const allocator_type& al): pimpl{x.impl().copy(al)}{set_sentinel();} /* TODO: try ptr-level move before impl().move() */ segment(segment&& x,const allocator_type& al): pimpl{x.impl().move(al)}{set_sentinel();} segment& operator=(const segment& x) { pimpl=allocator_traits::propagate_on_container_copy_assignment::value? x.impl().copy():x.impl().copy(impl().get_allocator()); set_sentinel(); return *this; } segment& operator=(segment&& x) { pimpl=x.impl().move( allocator_traits::propagate_on_container_move_assignment::value? x.impl().get_allocator():impl().get_allocator()); set_sentinel(); return *this; } friend bool operator==(const segment& x,const segment& y) { if(typeid(*(x.pimpl))!=typeid(*(y.pimpl)))return false; else return x.impl().equal(y.impl()); } friend bool operator!=(const segment& x,const segment& y){return !(x==y);} base_iterator begin()const noexcept{return impl().begin();} template base_iterator begin()const noexcept{return impl().nv_begin();} base_iterator end()const noexcept{return impl().end();} template base_iterator end()const noexcept{return impl().nv_end();} base_sentinel sentinel()const noexcept{return snt;} bool empty()const noexcept{return impl().empty();} template bool empty()const noexcept{return impl().nv_empty();} std::size_t size()const noexcept{return impl().size();} template std::size_t size()const noexcept{return impl().nv_size();} std::size_t max_size()const noexcept{return impl().max_size();} template std::size_t max_size()const noexcept {return impl().nv_max_size();} void reserve(std::size_t n){filter(impl().reserve(n));} template void reserve(std::size_t n){filter(impl().nv_reserve(n));} std::size_t capacity()const noexcept{return impl().capacity();} template std::size_t capacity()const noexcept {return impl().nv_capacity();} void shrink_to_fit(){filter(impl().shrink_to_fit());} template void shrink_to_fit(){filter(impl().nv_shrink_to_fit());} template base_iterator emplace(Iterator it,Args&&... args) { return filter(impl().nv_emplace(it,std::forward(args)...)); } template base_iterator emplace_back(Args&&... args) { return filter(impl().nv_emplace_back(std::forward(args)...)); } template base_iterator push_back(const T& x) { return filter(impl().push_back(subaddress(x))); } template< typename T, typename std::enable_if< !std::is_lvalue_reference::value&&!std::is_const::value >::type* =nullptr > base_iterator push_back(T&& x) { return filter(impl().push_back_move(subaddress(x))); } template base_iterator push_back_terminal(U&& x) { return filter( impl::type>().nv_push_back(std::forward(x))); } template base_iterator insert(const_base_iterator it,const T& x) { return filter(impl().insert(it,subaddress(x))); } template base_iterator insert(const_iterator it,const T& x) { return filter( impl().nv_insert(it,*static_cast(subaddress(x)))); } template< typename T, typename std::enable_if< !std::is_lvalue_reference::value&&!std::is_const::value >::type* =nullptr > base_iterator insert(const_base_iterator it,T&& x) { return filter(impl().insert_move(it,subaddress(x))); } template< typename U,typename T, typename std::enable_if< !std::is_lvalue_reference::value&&!std::is_const::value >::type* =nullptr > base_iterator insert(const_iterator it,T&& x) { return filter( impl().nv_insert(it,std::move(*static_cast(subaddress(x))))); } template base_iterator insert(InputIterator first,InputIterator last) { return filter( impl::value_type>(). nv_insert(first,last)); } template base_iterator insert( const_base_iterator it,InputIterator first,InputIterator last) { return insert( const_iterator< typename std::iterator_traits::value_type>(it), first,last); } template base_iterator insert( const_iterator it,InputIterator first,InputIterator last) { return filter(impl().nv_insert(it,first,last)); } base_iterator erase(const_base_iterator it) { return filter(impl().erase(it)); } template base_iterator erase(const_iterator it) { return filter(impl().nv_erase(it)); } base_iterator erase(const_base_iterator f,const_base_iterator l) { return filter(impl().erase(f,l)); } template base_iterator erase(const_iterator f,const_iterator l) { return filter(impl().nv_erase(f,l)); } template base_iterator erase_till_end(Iterator f) { return filter(impl().erase_till_end(f)); } template base_iterator erase_from_begin(Iterator l) { return filter(impl().erase_from_begin(l)); } void clear()noexcept{filter(impl().clear());} template void clear()noexcept{filter(impl().nv_clear());} private: using allocator_traits=std::allocator_traits; using segment_backend=typename Model::template segment_backend; template using segment_backend_implementation=typename Model:: template segment_backend_implementation; using segment_backend_unique_ptr= typename segment_backend::segment_backend_unique_ptr; using range=typename segment_backend::range; struct from_prototype{}; segment(segment_backend_unique_ptr&& pimpl): pimpl{std::move(pimpl)}{set_sentinel();} segment(from_prototype,const segment& x,const allocator_type& al): pimpl{x.impl().empty_copy(al)}{set_sentinel();} segment_backend& impl()noexcept{return *pimpl;} const segment_backend& impl()const noexcept{return *pimpl;} template segment_backend_implementation& impl()noexcept { return static_cast&>(impl()); } template const segment_backend_implementation& impl()const noexcept { return static_cast&>(impl()); } template static void* subaddress(T& x){return Model::subaddress(x);} template static const void* subaddress(const T& x){return Model::subaddress(x);} void set_sentinel(){filter(impl().end());} void filter(base_sentinel x){snt=x;} base_iterator filter(const range& x){snt=x.second;return x.first;} segment_backend_unique_ptr pimpl; base_sentinel snt; }; } /* namespace poly_collection::detail */ } /* namespace poly_collection */ } /* namespace boost */ #endif