1 // Boost.Geometry Index
2 //
3 // R-tree deep copying visitor implementation
4 //
5 // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
6 //
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)
10 
11 #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COPY_HPP
12 #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COPY_HPP
13 
14 namespace boost { namespace geometry { namespace index {
15 
16 namespace detail { namespace rtree { namespace visitors {
17 
18 template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
19 class copy
20     : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, false>::type
21 {
22 public:
23     typedef typename rtree::node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type node;
24     typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
25     typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
26 
27     typedef rtree::subtree_destroyer<Value, Options, Translator, Box, Allocators> subtree_destroyer;
28     typedef typename Allocators::node_pointer node_pointer;
29 
copy(Allocators & allocators)30     explicit inline copy(Allocators & allocators)
31         : result(0)
32         , m_allocators(allocators)
33     {}
34 
operator ()(internal_node & n)35     inline void operator()(internal_node & n)
36     {
37         node_pointer raw_new_node = rtree::create_node<Allocators, internal_node>::apply(m_allocators);      // MAY THROW, STRONG (N: alloc)
38         subtree_destroyer new_node(raw_new_node, m_allocators);
39 
40         typedef typename rtree::elements_type<internal_node>::type elements_type;
41         elements_type & elements = rtree::elements(n);
42 
43         elements_type & elements_dst = rtree::elements(rtree::get<internal_node>(*new_node));
44 
45         for (typename elements_type::iterator it = elements.begin();
46             it != elements.end(); ++it)
47         {
48             rtree::apply_visitor(*this, *it->second);                                                   // MAY THROW (V, E: alloc, copy, N: alloc)
49 
50             // for exception safety
51             subtree_destroyer auto_result(result, m_allocators);
52 
53             elements_dst.push_back( rtree::make_ptr_pair(it->first, result) );                          // MAY THROW, STRONG (E: alloc, copy)
54 
55             auto_result.release();
56         }
57 
58         result = new_node.get();
59         new_node.release();
60     }
61 
operator ()(leaf & l)62     inline void operator()(leaf & l)
63     {
64         node_pointer raw_new_node = rtree::create_node<Allocators, leaf>::apply(m_allocators);                // MAY THROW, STRONG (N: alloc)
65         subtree_destroyer new_node(raw_new_node, m_allocators);
66 
67         typedef typename rtree::elements_type<leaf>::type elements_type;
68         elements_type & elements = rtree::elements(l);
69 
70         elements_type & elements_dst = rtree::elements(rtree::get<leaf>(*new_node));
71 
72         for (typename elements_type::iterator it = elements.begin();
73             it != elements.end(); ++it)
74         {
75             elements_dst.push_back(*it);                                                                // MAY THROW, STRONG (V: alloc, copy)
76         }
77 
78         result = new_node.get();
79         new_node.release();
80     }
81 
82     node_pointer result;
83 
84 private:
85     Allocators & m_allocators;
86 };
87 
88 }}} // namespace detail::rtree::visitors
89 
90 }}} // namespace boost::geometry::index
91 
92 #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_COPY_HPP
93