Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54531 - in sandbox/monotonic: boost/heterogenous boost/monotonic libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-30 05:51:58


Author: cschladetsch
Date: 2009-06-30 05:51:57 EDT (Tue, 30 Jun 2009)
New Revision: 54531
URL: http://svn.boost.org/trac/boost/changeset/54531

Log:
added create, release free functions to allocator.hpp

Text files modified:
   sandbox/monotonic/boost/heterogenous/allocator.hpp | 19 +++++++++++++++++++
   sandbox/monotonic/boost/heterogenous/cloneable.hpp | 7 +++----
   sandbox/monotonic/boost/monotonic/local.hpp | 7 +++++++
   sandbox/monotonic/libs/monotonic/test/clones/tests.cpp | 19 ++++++++++---------
   4 files changed, 39 insertions(+), 13 deletions(-)

Modified: sandbox/monotonic/boost/heterogenous/allocator.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/allocator.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/allocator.hpp 2009-06-30 05:51:57 EDT (Tue, 30 Jun 2009)
@@ -7,6 +7,7 @@
 #define BOOST_HETEROGENOUS_ALLOCATOR_HPP
 
 #include <boost/heterogenous/detail/prefix.hpp>
+#include <boost/heterogenous/detail/allocation.hpp>
 
 namespace boost
 {
@@ -42,6 +43,24 @@
                         }
                 };
 
+ template <class T, class Alloc>
+ T *create(Alloc &alloc)
+ {
+ typename Alloc::template rebind<T>::other al(alloc);
+ T *ptr = al.allocate(1);
+ al.construct(ptr);
+ return ptr;
+ }
+
+ template <class T, class Alloc>
+ void release(T *ptr, Alloc &alloc)
+ {
+ typename Alloc::template rebind<T>::other al(alloc);
+ al.destroy(ptr);
+ al.deallocate(ptr, 1);
+ return ptr;
+ }
+
         } // namespace heterogenous
 
 } // namespace boost

Modified: sandbox/monotonic/boost/heterogenous/cloneable.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/cloneable.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/cloneable.hpp 2009-06-30 05:51:57 EDT (Tue, 30 Jun 2009)
@@ -24,12 +24,11 @@
                         typedef abstract_cloneable<Base> abstract_base_type;
                         typedef cloneable<Derived, Base/*, AbstractBase*/> this_type;
 
- //private:
- static size_t alignment; ///< required alignment for allocation
+ static const size_t alignment; ///< required alignment for allocation
                         mutable derived_type *self_ptr; ///< pointer to derived object in this
 
                 public:
- cloneable() : self_ptr(0) { }
+ cloneable() { self_ptr = static_cast<Derived *>(this); }
 
                         virtual this_type *allocate(abstract_allocator &alloc) const
                         {
@@ -66,7 +65,7 @@
 
                 /// ensure correct alignment when allocating derived instances
                 template <class Derived, class Base/*, class AbstractBase*/>
- size_t cloneable<Derived, Base/*, AbstractBase*/>::alignment = aligned_storage<sizeof(Derived)>::alignment;
+ const size_t cloneable<Derived, Base/*, AbstractBase*/>::alignment = aligned_storage<sizeof(Derived)>::alignment;
 
         } // namespace heterogenous
 

Modified: sandbox/monotonic/boost/monotonic/local.hpp
==============================================================================
--- sandbox/monotonic/boost/monotonic/local.hpp (original)
+++ sandbox/monotonic/boost/monotonic/local.hpp 2009-06-30 05:51:57 EDT (Tue, 30 Jun 2009)
@@ -41,6 +41,13 @@
                         {
                                 reset();
                         }
+
+ template <class T>
+ static monotonic::allocator<T,Region,Access> make_allocator()
+ {
+ return monotonic::allocator<T,Region,Access>();
+ }
+
                         static typename StaticStorage::StorageType &get_storage()
                         {
                                 return StaticStorage::get_storage();

Modified: sandbox/monotonic/libs/monotonic/test/clones/tests.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/clones/tests.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/clones/tests.cpp 2009-06-30 05:51:57 EDT (Tue, 30 Jun 2009)
@@ -11,6 +11,7 @@
 
 #include <string>
 #include <iostream>
+#include <boost/monotonic/local.hpp>
 #include <boost/heterogenous/vector.hpp>
 #include <boost/heterogenous/map.hpp>
 #include <boost/heterogenous/adaptor.hpp>
@@ -70,6 +71,7 @@
                 Q0(int n = 0) : num(n) { }
         };
 
+ /// derive from Q1, which is also cloneable<>
         struct Q1 : Q0, cloneable<Q1>
         {
                 string s;
@@ -85,26 +87,25 @@
 BOOST_AUTO_TEST_CASE(test_clones)
 {
         using namespace mi_test;
- monotonic::allocator<int, my_region> alloc;
+
+ monotonic::local<my_region> local;
+ monotonic::allocator<int,my_region> alloc = local.make_allocator<int>();
 
- Q0 *q0 = new Q0;
+ Q0 *q0 = create<Q0>(alloc);
         BOOST_ASSERT(typeid(*q0) == typeid(Q0));
+
         Q0 *q0_c = dynamic_cast<Q0 *>(q0->clone(alloc));
         BOOST_ASSERT(typeid(*q0_c) == typeid(Q0));
 
- Q1 *q1 = new Q1();
+ Q1 *q1 = create<Q1>(alloc);
         BOOST_ASSERT(typeid(*q1) == typeid(Q1));
 
         Q0 *q1_c0 = dynamic_cast<Q0 *>(q1->clone_as<Q0>(alloc));
         BOOST_ASSERT(typeid(*q1_c0) == typeid(Q0));
 
- Q1 *q1_c = dynamic_cast<Q1 *>(q1->clone_as<Q1>(alloc));
- BOOST_ASSERT(typeid(*q1_c) == typeid(Q1));
-
- delete q0;
- delete q1;
+ Q1 *q1_c1 = dynamic_cast<Q1 *>(q1->clone_as<Q1>(alloc));
+ BOOST_ASSERT(typeid(*q1_c1) == typeid(Q1));
 
- monotonic::static_storage<my_region>::release();
 }
 
 BOOST_AUTO_TEST_CASE(test_multiple_inheritance)


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