1 // Boost.Geometry Index
3 // R-tree nodes based on Boost.Variant, storing dynamic-size containers
5 // Copyright (c) 2011-2014 Adam Wulkiewicz, Lodz, Poland.
7 // Use, modification and distribution is subject to the Boost Software License,
8 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP
12 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP
14 #include <boost/core/pointer_traits.hpp>
16 namespace boost { namespace geometry { namespace index {
18 namespace detail { namespace rtree {
20 // nodes default types
22 template <typename Value, typename Parameters, typename Box, typename Allocators, typename Tag>
23 struct variant_internal_node
25 typedef boost::container::vector
27 rtree::ptr_pair<Box, typename Allocators::node_pointer>,
28 typename Allocators::node_allocator_type::template rebind
30 rtree::ptr_pair<Box, typename Allocators::node_pointer>
34 template <typename Al>
35 inline variant_internal_node(Al const& al)
39 elements_type elements;
42 template <typename Value, typename Parameters, typename Box, typename Allocators, typename Tag>
45 typedef boost::container::vector
48 typename Allocators::node_allocator_type::template rebind
54 template <typename Al>
55 inline variant_leaf(Al const& al)
59 elements_type elements;
64 template <typename Value, typename Parameters, typename Box, typename Allocators>
65 struct node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
67 typedef boost::variant<
68 variant_leaf<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>,
69 variant_internal_node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
73 template <typename Value, typename Parameters, typename Box, typename Allocators>
74 struct internal_node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
76 typedef variant_internal_node<Value, Parameters, Box, Allocators, node_variant_dynamic_tag> type;
79 template <typename Value, typename Parameters, typename Box, typename Allocators>
80 struct leaf<Value, Parameters, Box, Allocators, node_variant_dynamic_tag>
82 typedef variant_leaf<Value, Parameters, Box, Allocators, node_variant_dynamic_tag> type;
87 template <typename Value, typename Parameters, typename Box, typename Allocators, bool IsVisitableConst>
88 struct visitor<Value, Parameters, Box, Allocators, node_variant_dynamic_tag, IsVisitableConst>
90 typedef static_visitor<> type;
95 template <typename Allocator, typename Value, typename Parameters, typename Box>
96 class allocators<Allocator, Value, Parameters, Box, node_variant_dynamic_tag>
97 : public Allocator::template rebind<
99 Value, Parameters, Box,
100 allocators<Allocator, Value, Parameters, Box, node_variant_dynamic_tag>,
101 node_variant_dynamic_tag
105 typedef typename Allocator::template rebind<
107 >::other value_allocator_type;
110 typedef Allocator allocator_type;
112 typedef Value value_type;
113 typedef typename value_allocator_type::reference reference;
114 typedef typename value_allocator_type::const_reference const_reference;
115 typedef typename value_allocator_type::size_type size_type;
116 typedef typename value_allocator_type::difference_type difference_type;
117 typedef typename value_allocator_type::pointer pointer;
118 typedef typename value_allocator_type::const_pointer const_pointer;
120 typedef typename Allocator::template rebind<
121 typename node<Value, Parameters, Box, allocators, node_variant_dynamic_tag>::type
122 >::other::pointer node_pointer;
124 typedef typename Allocator::template rebind<
125 typename node<Value, Parameters, Box, allocators, node_variant_dynamic_tag>::type
126 >::other node_allocator_type;
129 : node_allocator_type()
132 template <typename Alloc>
133 inline explicit allocators(Alloc const& alloc)
134 : node_allocator_type(alloc)
137 inline allocators(BOOST_FWD_REF(allocators) a)
138 : node_allocator_type(boost::move(a.node_allocator()))
141 inline allocators & operator=(BOOST_FWD_REF(allocators) a)
143 node_allocator() = boost::move(a.node_allocator());
147 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
148 inline allocators & operator=(allocators const& a)
150 node_allocator() = a.node_allocator();
155 void swap(allocators & a)
157 boost::swap(node_allocator(), a.node_allocator());
160 bool operator==(allocators const& a) const { return node_allocator() == a.node_allocator(); }
161 template <typename Alloc>
162 bool operator==(Alloc const& a) const { return node_allocator() == node_allocator_type(a); }
164 Allocator allocator() const { return Allocator(node_allocator()); }
166 node_allocator_type & node_allocator() { return *this; }
167 node_allocator_type const& node_allocator() const { return *this; }
170 // create_node_variant
172 template <typename VariantPtr, typename Node>
173 struct create_variant_node
175 template <typename AllocNode>
176 static inline VariantPtr apply(AllocNode & alloc_node)
178 typedef boost::container::allocator_traits<AllocNode> Al;
179 typedef typename Al::pointer P;
181 P p = Al::allocate(alloc_node, 1);
184 throw_runtime_error("boost::geometry::index::rtree node creation failed");
186 scoped_deallocator<AllocNode> deallocator(p, alloc_node);
188 Al::construct(alloc_node, boost::pointer_traits<P>::to_address(p), Node(alloc_node)); // implicit cast to Variant
190 deallocator.release();
195 // destroy_node_variant
197 template <typename Node>
198 struct destroy_variant_node
200 template <typename AllocNode, typename VariantPtr>
201 static inline void apply(AllocNode & alloc_node, VariantPtr n)
203 typedef boost::container::allocator_traits<AllocNode> Al;
205 Al::destroy(alloc_node, boost::addressof(*n));
206 Al::deallocate(alloc_node, n, 1);
212 template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
215 variant_internal_node<Value, Parameters, Box, Allocators, Tag>
218 static inline typename Allocators::node_pointer
219 apply(Allocators & allocators)
221 return create_variant_node<
222 typename Allocators::node_pointer,
223 variant_internal_node<Value, Parameters, Box, Allocators, Tag>
224 >::apply(allocators.node_allocator());
228 template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
231 variant_leaf<Value, Parameters, Box, Allocators, Tag>
234 static inline typename Allocators::node_pointer
235 apply(Allocators & allocators)
237 return create_variant_node<
238 typename Allocators::node_pointer,
239 variant_leaf<Value, Parameters, Box, Allocators, Tag>
240 >::apply(allocators.node_allocator());
246 template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
249 variant_internal_node<Value, Parameters, Box, Allocators, Tag>
252 static inline void apply(Allocators & allocators, typename Allocators::node_pointer n)
254 destroy_variant_node<
255 variant_internal_node<Value, Parameters, Box, Allocators, Tag>
256 >::apply(allocators.node_allocator(), n);
260 template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
263 variant_leaf<Value, Parameters, Box, Allocators, Tag>
266 static inline void apply(Allocators & allocators, typename Allocators::node_pointer n)
268 destroy_variant_node<
269 variant_leaf<Value, Parameters, Box, Allocators, Tag>
270 >::apply(allocators.node_allocator(), n);
274 }} // namespace detail::rtree
276 }}} // namespace boost::geometry::index
278 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_NODE_VARIANT_DYNAMIC_HPP