// Boost.Geometry Index // // R-tree nodes based on Boost.Variant, storing dynamic-size containers // // Copyright (c) 2011-2018 Adam Wulkiewicz, Lodz, Poland. // // Use, modification and distribution is subject to 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_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP #include namespace boost { namespace geometry { namespace index { namespace detail { namespace rtree { // nodes default types template struct variant_internal_node { typedef rtree::ptr_pair element_type; typedef typename boost::container::allocator_traits < typename Allocators::node_allocator_type >::template rebind_alloc allocator_type; typedef boost::container::vector elements_type; template inline variant_internal_node(Al const& al) : elements(allocator_type(al)) {} elements_type elements; }; template struct variant_leaf { typedef typename boost::container::allocator_traits < typename Allocators::node_allocator_type >::template rebind_alloc allocator_type; typedef boost::container::vector elements_type; template inline variant_leaf(Al const& al) : elements(allocator_type(al)) {} elements_type elements; }; // nodes traits template struct node { typedef boost::variant< variant_leaf, variant_internal_node > type; }; template struct internal_node { typedef variant_internal_node type; }; template struct leaf { typedef variant_leaf type; }; // visitor traits template struct visitor { typedef static_visitor<> type; }; // allocators template struct node_alloc { typedef typename node < Value, Parameters, Box, allocators, Tag >::type node_type; typedef typename boost::container::allocator_traits < Allocator >::template rebind_alloc type; typedef boost::container::allocator_traits traits; }; template class allocators : public detail::rtree::node_alloc < Allocator, Value, Parameters, Box, node_variant_dynamic_tag >::type { typedef detail::rtree::node_alloc < Allocator, Value, Parameters, Box, node_variant_dynamic_tag > node_alloc; public: typedef typename node_alloc::type node_allocator_type; typedef typename node_alloc::traits::pointer node_pointer; private: typedef typename boost::container::allocator_traits < node_allocator_type // node_allocator_type for consistency with variant_leaf >::template rebind_alloc value_allocator_type; typedef boost::container::allocator_traits value_allocator_traits; public: typedef Allocator allocator_type; typedef Value value_type; typedef typename value_allocator_traits::reference reference; typedef typename value_allocator_traits::const_reference const_reference; typedef typename value_allocator_traits::size_type size_type; typedef typename value_allocator_traits::difference_type difference_type; typedef typename value_allocator_traits::pointer pointer; typedef typename value_allocator_traits::const_pointer const_pointer; inline allocators() : node_allocator_type() {} template inline explicit allocators(Alloc const& alloc) : node_allocator_type(alloc) {} inline allocators(BOOST_FWD_REF(allocators) a) : node_allocator_type(boost::move(a.node_allocator())) {} inline allocators & operator=(BOOST_FWD_REF(allocators) a) { node_allocator() = boost::move(a.node_allocator()); return *this; } #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES inline allocators & operator=(allocators const& a) { node_allocator() = a.node_allocator(); return *this; } #endif void swap(allocators & a) { boost::swap(node_allocator(), a.node_allocator()); } bool operator==(allocators const& a) const { return node_allocator() == a.node_allocator(); } template bool operator==(Alloc const& a) const { return node_allocator() == node_allocator_type(a); } Allocator allocator() const { return Allocator(node_allocator()); } node_allocator_type & node_allocator() { return *this; } node_allocator_type const& node_allocator() const { return *this; } }; // create_node_variant template struct create_variant_node { template static inline VariantPtr apply(AllocNode & alloc_node) { typedef boost::container::allocator_traits Al; typedef typename Al::pointer P; P p = Al::allocate(alloc_node, 1); if ( 0 == p ) throw_runtime_error("boost::geometry::index::rtree node creation failed"); scoped_deallocator deallocator(p, alloc_node); Al::construct(alloc_node, boost::to_address(p), Node(alloc_node)); // implicit cast to Variant deallocator.release(); return p; } }; // destroy_node_variant template struct destroy_variant_node { template static inline void apply(AllocNode & alloc_node, VariantPtr n) { typedef boost::container::allocator_traits Al; Al::destroy(alloc_node, boost::addressof(*n)); Al::deallocate(alloc_node, n, 1); } }; // create_node template struct create_node< Allocators, variant_internal_node > { static inline typename Allocators::node_pointer apply(Allocators & allocators) { return create_variant_node< typename Allocators::node_pointer, variant_internal_node >::apply(allocators.node_allocator()); } }; template struct create_node< Allocators, variant_leaf > { static inline typename Allocators::node_pointer apply(Allocators & allocators) { return create_variant_node< typename Allocators::node_pointer, variant_leaf >::apply(allocators.node_allocator()); } }; // destroy_node template struct destroy_node< Allocators, variant_internal_node > { static inline void apply(Allocators & allocators, typename Allocators::node_pointer n) { destroy_variant_node< variant_internal_node >::apply(allocators.node_allocator(), n); } }; template struct destroy_node< Allocators, variant_leaf > { static inline void apply(Allocators & allocators, typename Allocators::node_pointer n) { destroy_variant_node< variant_leaf >::apply(allocators.node_allocator(), n); } }; }} // namespace detail::rtree }}} // namespace boost::geometry::index #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP