Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r74664 - in sandbox-branches/geometry/index: boost/geometry/extensions/index/rtree boost/geometry/extensions/index/rtree/node tests
From: adam.wulkiewicz_at_[hidden]
Date: 2011-10-03 05:24:19


Author: awulkiew
Date: 2011-10-03 05:24:17 EDT (Mon, 03 Oct 2011)
New Revision: 74664
URL: http://svn.boost.org/trac/boost/changeset/74664

Log:
exceptions handled
Text files modified:
   sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default.hpp | 104 +++++++++++++++++++++++++++------------
   sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_static_variant.hpp | 15 -----
   sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_variant.hpp | 104 +++++++++++++++++++++++++++------------
   sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/rtree.hpp | 81 +++++++++++++++++++++---------
   sandbox-branches/geometry/index/tests/main.cpp | 2
   5 files changed, 201 insertions(+), 105 deletions(-)

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default.hpp 2011-10-03 05:24:17 EDT (Mon, 03 Oct 2011)
@@ -210,7 +210,7 @@
 // allocators
 
 template <typename Value, typename Parameters, typename Box, typename Tag, typename Allocator>
-struct allocators
+struct allocators_poly
 {
     typedef Allocator allocator_type;
     typedef typename allocator_type::size_type size_type;
@@ -223,7 +223,7 @@
         typename leaf<Value, Parameters, Box, Tag>::type
>::other leaf_allocator_type;
 
- inline explicit allocators(Allocator alloc)
+ inline explicit allocators_poly(Allocator alloc)
         : allocator(alloc)
         , internal_node_allocator(allocator)
         , leaf_allocator(allocator)
@@ -234,6 +234,55 @@
     leaf_allocator_type leaf_allocator;
 };
 
+// allocators
+
+template <typename Value, typename Parameters, typename Box, typename Tag, typename Allocator>
+struct allocators
+{
+ typedef allocators_poly<Value, Parameters, Box, Tag, Allocator> type;
+};
+
+// create_node_poly
+
+template <typename Allocator, typename Node>
+struct create_node_poly
+{
+ template <typename RetNode>
+ static inline RetNode * apply(Allocator & allocator)
+ {
+ Node * p = allocator.allocate(1);
+
+ if ( 0 == p )
+ throw std::bad_alloc();
+
+ try
+ {
+ allocator.construct(p, Node());
+ }
+ catch(...)
+ {
+ allocator.deallocate(p, 1);
+ throw;
+ }
+
+ return p;
+ }
+};
+
+// destroy_node_poly
+
+template <typename Allocator, typename Node>
+struct destroy_node_poly
+{
+ template <typename BaseNode>
+ static inline void apply(Allocator & allocator, BaseNode * n)
+ {
+ Node * p = rtree::get<Node>(n);
+ allocator.destroy(p);
+ allocator.deallocate(p, 1);
+ }
+};
+
 // create_node
 
 template <typename Allocators, typename Node>
@@ -253,15 +302,12 @@
 {
     static inline typename node<Value, Parameters, Box, Tag>::type * apply(Allocators & allocators)
     {
- //return new internal_node_poly<Value, Parameters, Box, Tag>();
- internal_node_poly<Value, Parameters, Box, Tag> * p
- = allocators.internal_node_allocator.allocate(1);
-
- allocators.internal_node_allocator.construct(
- p,
- internal_node_poly<Value, Parameters, Box, Tag>());
-
- return p;
+ return create_node_poly<
+ typename Allocators::internal_node_allocator_type,
+ internal_node_poly<Value, Parameters, Box, Tag>
+ >::template apply<
+ typename node<Value, Parameters, Box, Tag>::type
+ >(allocators.internal_node_allocator);
     }
 };
 
@@ -273,16 +319,12 @@
 {
     static inline typename node<Value, Parameters, Box, Tag>::type * apply(Allocators & allocators)
     {
- //return new leaf_poly<Value, Parameters, Box, Tag>();
-
- leaf_poly<Value, Parameters, Box, Tag> * p
- = allocators.leaf_allocator.allocate(1);
-
- allocators.leaf_allocator.construct(
- p,
- leaf_poly<Value, Parameters, Box, Tag>());
-
- return p;
+ return create_node_poly<
+ typename Allocators::leaf_allocator_type,
+ leaf_poly<Value, Parameters, Box, Tag>
+ >::template apply<
+ typename node<Value, Parameters, Box, Tag>::type
+ >(allocators.leaf_allocator);
     }
 };
 
@@ -305,12 +347,10 @@
 {
     static inline void apply(Allocators & allocators, typename node<Value, Parameters, Box, Tag>::type * n)
     {
- //delete n;
-
- internal_node_poly<Value, Parameters, Box, Tag> * p
- = rtree::get< internal_node_poly<Value, Parameters, Box, Tag> >(n);
- allocators.internal_node_allocator.destroy(p);
- allocators.internal_node_allocator.deallocate(p, 1);
+ destroy_node_poly<
+ typename Allocators::internal_node_allocator_type,
+ internal_node_poly<Value, Parameters, Box, Tag>
+ >::apply(allocators.internal_node_allocator, n);
     }
 };
 
@@ -322,12 +362,10 @@
 {
     static inline void apply(Allocators & allocators, typename node<Value, Parameters, Box, Tag>::type * n)
     {
- //delete n;
-
- leaf_poly<Value, Parameters, Box, Tag> * p
- = rtree::get< leaf_poly<Value, Parameters, Box, Tag> >(n);
- allocators.leaf_allocator.destroy(p);
- allocators.leaf_allocator.deallocate(p, 1);
+ destroy_node_poly<
+ typename Allocators::leaf_allocator_type,
+ leaf_poly<Value, Parameters, Box, Tag>
+ >::apply(allocators.leaf_allocator, n);
     }
 };
 

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_static_variant.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_static_variant.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_static_variant.hpp 2011-10-03 05:24:17 EDT (Mon, 03 Oct 2011)
@@ -76,20 +76,7 @@
 template <typename Value, typename Parameters, typename Box, typename Allocator>
 struct allocators<Value, Parameters, Box, node_default_static_variant_tag, Allocator>
 {
- typedef Allocator allocator_type;
- typedef typename allocator_type::size_type size_type;
-
- typedef typename allocator_type::template rebind<
- typename node<Value, Parameters, Box, node_default_static_variant_tag>::type
- >::other node_allocator_type;
-
- inline explicit allocators(Allocator alloc)
- : allocator(alloc)
- , node_allocator(allocator)
- {}
-
- allocator_type allocator;
- node_allocator_type node_allocator;
+ typedef allocators_variant<Value, Parameters, Box, node_default_static_variant_tag, Allocator> type;
 };
 
 }} // namespace detail::rtree

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_variant.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_variant.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/node/node_default_variant.hpp 2011-10-03 05:24:17 EDT (Mon, 03 Oct 2011)
@@ -149,8 +149,8 @@
 
 // allocators
 
-template <typename Value, typename Parameters, typename Box, typename Allocator>
-struct allocators<Value, Parameters, Box, node_default_variant_tag, Allocator>
+template <typename Value, typename Parameters, typename Box, typename Tag, typename Allocator>
+struct allocators_variant
 {
     typedef Allocator allocator_type;
     typedef typename allocator_type::size_type size_type;
@@ -159,7 +159,7 @@
         typename node<Value, Parameters, Box, node_default_variant_tag>::type
>::other node_allocator_type;
 
- inline explicit allocators(Allocator alloc)
+ inline explicit allocators_variant(Allocator alloc)
         : allocator(alloc)
         , node_allocator(allocator)
     {}
@@ -168,6 +168,54 @@
     node_allocator_type node_allocator;
 };
 
+// allocators
+
+template <typename Value, typename Parameters, typename Box, typename Allocator>
+struct allocators<Value, Parameters, Box, node_default_variant_tag, Allocator>
+{
+ typedef allocators_variant<Value, Parameters, Box, node_default_variant_tag, Allocator> type;
+};
+
+// create_node_variant
+
+template <typename Allocator, typename Node>
+struct create_node_variant
+{
+ template <typename RetNode>
+ static inline RetNode * apply(Allocator & allocator)
+ {
+ RetNode * p = allocator.allocate(1);
+
+ if ( 0 == p )
+ throw std::bad_alloc();
+
+ try
+ {
+ allocator.construct(p, Node());
+ }
+ catch(...)
+ {
+ allocator.deallocate(p, 1);
+ throw;
+ }
+
+ return p;
+ }
+};
+
+// destroy_node_variant
+
+template <typename Allocator, typename Node>
+struct destroy_node_variant
+{
+ template <typename BaseNode>
+ static inline void apply(Allocator & allocator, BaseNode * n)
+ {
+ allocator.destroy(n);
+ allocator.deallocate(n, 1);
+ }
+};
+
 // create_node
 
 template <typename Allocators, typename Value, typename Parameters, typename Box, typename Tag>
@@ -179,17 +227,12 @@
     static inline typename node<Value, Parameters, Box, Tag>::type *
     apply(Allocators & allocators)
     {
- /*return new typename node<Value, Parameters, Box, Tag>::type(
- internal_node_variant<Value, Parameters, Box, Tag>() );*/
-
- typename node<Value, Parameters, Box, Tag>::type * p
- = allocators.node_allocator.allocate(1);
-
- allocators.node_allocator.construct(
- p,
- internal_node_variant<Value, Parameters, Box, Tag>());
-
- return p;
+ return create_node_variant<
+ typename Allocators::node_allocator_type,
+ internal_node_variant<Value, Parameters, Box, Tag>
+ >::template apply<
+ typename node<Value, Parameters, Box, Tag>::type
+ >(allocators.node_allocator);
     }
 };
 
@@ -201,17 +244,12 @@
 {
     static inline typename node<Value, Parameters, Box, Tag>::type * apply(Allocators & allocators)
     {
- /*return new typename node<Value, Parameters, Box, Tag>::type(
- leaf_variant<Value, Parameters, Box, Tag>() );*/
-
- typename node<Value, Parameters, Box, Tag>::type * p
- = allocators.node_allocator.allocate(1);
-
- allocators.node_allocator.construct(
- p,
- leaf_variant<Value, Parameters, Box, Tag>());
-
- return p;
+ return create_node_variant<
+ typename Allocators::node_allocator_type,
+ leaf_variant<Value, Parameters, Box, Tag>
+ >::template apply<
+ typename node<Value, Parameters, Box, Tag>::type
+ >(allocators.node_allocator);
     }
 };
 
@@ -225,10 +263,10 @@
 {
     static inline void apply(Allocators & allocators, typename node<Value, Parameters, Box, Tag>::type * n)
     {
- //delete n;
-
- allocators.node_allocator.destroy(n);
- allocators.node_allocator.deallocate(n, 1);
+ destroy_node_variant<
+ typename Allocators::node_allocator_type,
+ internal_node_variant<Value, Parameters, Box, Tag>
+ >::apply(allocators.node_allocator, n);
     }
 };
 
@@ -240,10 +278,10 @@
 {
     static inline void apply(Allocators & allocators, typename node<Value, Parameters, Box, Tag>::type * n)
     {
- //delete n;
-
- allocators.node_allocator.destroy(n);
- allocators.node_allocator.deallocate(n, 1);
+ destroy_node_variant<
+ typename Allocators::node_allocator_type,
+ leaf_variant<Value, Parameters, Box, Tag>
+ >::apply(allocators.node_allocator, n);
     }
 };
 

Modified: sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/rtree.hpp
==============================================================================
--- sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/rtree.hpp (original)
+++ sandbox-branches/geometry/index/boost/geometry/extensions/index/rtree/rtree.hpp 2011-10-03 05:24:17 EDT (Mon, 03 Oct 2011)
@@ -84,7 +84,7 @@
     typedef typename detail::rtree::leaf<value_type, typename options_type::parameters_type, box_type, node_tag>::type leaf;
 
     typedef Allocator allocator_type;
- typedef detail::rtree::allocators<value_type, typename options_type::parameters_type, box_type, node_tag, allocator_type> allocators_type;
+ typedef typename detail::rtree::allocators<value_type, typename options_type::parameters_type, box_type, node_tag, allocator_type>::type allocators_type;
     typedef typename allocators_type::size_type size_type;
 
     inline explicit rtree(translator_type const& translator = translator_type(), Allocator allocator = std::allocator<value_type>())
@@ -116,7 +116,15 @@
     inline rtree(rtree const& src)
         : m_allocators(src.m_allocators)
     {
- copy(src, *this);
+ try
+ {
+ copy(src, *this);
+ }
+ catch(...)
+ {
+ destroy(*this);
+ throw;
+ }
     }
 
     inline rtree & operator=(rtree const& src)
@@ -127,7 +135,16 @@
         destroy(*this);
         
         m_allocators = src.m_allocators;
- copy(src, *this);
+
+ try
+ {
+ copy(src, *this);
+ }
+ catch(...)
+ {
+ destroy(*this);
+ throw;
+ }
 
         return *this;
     }
@@ -136,19 +153,27 @@
     {
         BOOST_GEOMETRY_INDEX_ASSERT(index::is_valid(m_translator(value)), "Indexable is invalid");
 
- detail::rtree::visitors::insert<
- value_type,
- value_type,
- options_type,
- translator_type,
- box_type,
- allocators_type,
- typename options_type::insert_tag
- > insert_v(m_root, m_leafs_level, value, m_translator, m_allocators);
+ try
+ {
+ detail::rtree::visitors::insert<
+ value_type,
+ value_type,
+ options_type,
+ translator_type,
+ box_type,
+ allocators_type,
+ typename options_type::insert_tag
+ > insert_v(m_root, m_leafs_level, value, m_translator, m_allocators);
 
- detail::rtree::apply_visitor(insert_v, *m_root);
+ detail::rtree::apply_visitor(insert_v, *m_root);
 
- ++m_values_count;
+ ++m_values_count;
+ }
+ catch(...)
+ {
+ destroy(*this);
+ throw;
+ }
     }
 
     template <typename Iterator>
@@ -162,19 +187,27 @@
     {
         // TODO: awulkiew - assert for correct value (indexable) ?
 
- BOOST_GEOMETRY_INDEX_ASSERT(0 < m_values_count, "can't remove, there is no elements in the rtree");
+ BOOST_GEOMETRY_INDEX_ASSERT(0 < m_values_count, "can't remove, there are no elements in the rtree");
 
- detail::rtree::visitors::remove<
- value_type,
- options_type,
- translator_type,
- box_type,
- allocators_type
- > remove_v(m_root, m_leafs_level, value, m_translator, m_allocators);
+ try
+ {
+ detail::rtree::visitors::remove<
+ value_type,
+ options_type,
+ translator_type,
+ box_type,
+ allocators_type
+ > remove_v(m_root, m_leafs_level, value, m_translator, m_allocators);
 
- detail::rtree::apply_visitor(remove_v, *m_root);
+ detail::rtree::apply_visitor(remove_v, *m_root);
 
- --m_values_count;
+ --m_values_count;
+ }
+ catch(...)
+ {
+ destroy(*this);
+ throw;
+ }
     }
 
     template <typename Iterator>

Modified: sandbox-branches/geometry/index/tests/main.cpp
==============================================================================
--- sandbox-branches/geometry/index/tests/main.cpp (original)
+++ sandbox-branches/geometry/index/tests/main.cpp 2011-10-03 05:24:17 EDT (Mon, 03 Oct 2011)
@@ -27,7 +27,7 @@
 
 BOOST_AUTO_TEST_CASE( last_test_case )
 {
-
+
 #ifdef _MSC_VER
     std::cin.get();
 #endif


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk