Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54035 - in sandbox/monotonic: boost/monotonic libs/monotonic/test
From: christian.schladetsch_at_[hidden]
Date: 2009-06-17 19:53:59


Author: cschladetsch
Date: 2009-06-17 19:53:58 EDT (Wed, 17 Jun 2009)
New Revision: 54035
URL: http://svn.boost.org/trac/boost/changeset/54035

Log:
fixes for monotonic::shared_allocator to provide correct construction of elements that are also monotonic containers

Text files modified:
   sandbox/monotonic/boost/monotonic/allocator.hpp | 96 +++++++++++++++++++++++++++++++--------
   sandbox/monotonic/boost/monotonic/container.hpp | 34 ++++++-------
   sandbox/monotonic/boost/monotonic/shared_allocator.hpp | 46 +++++++++++++++----
   sandbox/monotonic/boost/monotonic/static_storage.hpp | 7 ++
   sandbox/monotonic/libs/monotonic/test/main.cpp | 31 ++++++++----
   5 files changed, 153 insertions(+), 61 deletions(-)

Modified: sandbox/monotonic/boost/monotonic/allocator.hpp
==============================================================================
--- sandbox/monotonic/boost/monotonic/allocator.hpp (original)
+++ sandbox/monotonic/boost/monotonic/allocator.hpp 2009-06-17 19:53:58 EDT (Wed, 17 Jun 2009)
@@ -64,10 +64,27 @@
                                         *ptr = val;
                                 }
                         };
+
+ template <bool is_monotonic_container, class T>
+ struct Creator
+ {
+ static T Create(storage_base &)
+ {
+ return T();
+ }
+ };
+ template <class T>
+ struct Creator<true, T>
+ {
+ static T Create(storage_base &storage)
+ {
+ return T(storage);
+ }
+ };
                 }
 
- template <class T>
- struct allocator
+ template <class T, class Derived>
+ struct allocator_base
                 {
                         BOOST_STATIC_CONSTANT(size_t, alignment = boost::aligned_storage<sizeof(T)>::alignment);
                         typedef size_t size_type;
@@ -77,41 +94,33 @@
                         typedef T &reference;
                         typedef const T &const_reference;
                         typedef T value_type;
- template <class U>
- struct rebind
- {
- typedef allocator<U> other;
- };
+
 
                 private:
                         storage_base *storage;
 
                 public:
- allocator() throw()
+ allocator_base() throw()
                                 : storage(&static_storage)
                         {
                         }
 
- allocator(storage_base &store) throw()
+ allocator_base(storage_base &store) throw()
                                 : storage(&store)
                         {
                         }
 
- allocator(const allocator& alloc) throw()
+ allocator_base(const allocator_base& alloc) throw()
                                 : storage(alloc.get_storage())
                         {
                         }
 
- template <class U>
- allocator(const allocator<U> &alloc) throw()
+ template <class U, class D>
+ allocator_base(const allocator_base<U,D> &alloc) throw()
                                 : storage(alloc.get_storage())
                         {
                         }
 
- ~allocator() throw()
- {
- }
-
                         pointer address(reference x) const
                         {
                                 return &x;
@@ -144,12 +153,12 @@
 
                         void construct(pointer ptr)
                         {
- detail::Construct<detail::is_monotonic<T>::value>::Given(ptr, this);
+ detail::Construct<detail::is_monotonic<T>::value>::Given(ptr, static_cast<Derived *>(this));
                         }
 
                         void construct(pointer ptr, const T& val)
                         {
- detail::Construct<detail::is_monotonic<T>::value>::Given(ptr, val, this);
+ detail::Construct<detail::is_monotonic<T>::value>::Given(ptr, val, static_cast<Derived *>(this));
                         }
 
                         void destroy(pointer ptr)
@@ -177,15 +186,62 @@
                         {
                                 return storage;
                         }
+ friend bool operator==(allocator_base<T,Derived> const &A, allocator_base<T,Derived> const &B)
+ {
+ return A.storage == B.storage;
+ }
+
+ friend bool operator!=(allocator_base<T,Derived> const &A, allocator_base<T,Derived> const &B)
+ {
+ return A.storage != B.storage;
+ }
+ };
+
+ template <class T>
+ struct allocator : allocator_base<T, allocator<T> >
+ {
+ typedef allocator_base<T, allocator<T> > Parent;
+ using typename Parent::size_type;
+ using typename Parent::difference_type;
+ using typename Parent::pointer;
+ using typename Parent::const_pointer;
+ using typename Parent::reference;
+ using typename Parent::const_reference;
+ using typename Parent::value_type;
+
+ template <class U>
+ struct rebind
+ {
+ typedef allocator<U> other;
+ };
+ allocator() throw()
+ : Parent(static_storage)
+ {
+ }
 
+ allocator(storage_base &store) throw()
+ : Parent(store)
+ {
+ }
+
+ allocator(const allocator& alloc) throw()
+ : Parent(alloc)
+ {
+ }
+
+ template <class U>
+ allocator(const allocator<U> &alloc) throw()
+ : Parent(alloc)
+ {
+ }
                         friend bool operator==(allocator<T> const &A, allocator<T> const &B)
                         {
- return A.storage == B.storage;
+ return operator==(static_cast<Parent const &>(A), static_cast<Parent const &>(B));
                         }
 
                         friend bool operator!=(allocator<T> const &A, allocator<T> const &B)
                         {
- return A.storage != B.storage;
+ return operator!=(static_cast<Parent const &>(A), static_cast<Parent const &>(B));
                         }
                 };
         

Modified: sandbox/monotonic/boost/monotonic/container.hpp
==============================================================================
--- sandbox/monotonic/boost/monotonic/container.hpp (original)
+++ sandbox/monotonic/boost/monotonic/container.hpp 2009-06-17 19:53:58 EDT (Wed, 17 Jun 2009)
@@ -45,25 +45,7 @@
                                 }
                         };
 
- // TODO move to detail::Construct in allocator.hpp
- template <bool is_monotonic_container, class T>
- struct Creator
- {
- static T Create(storage_base &)
- {
- return T();
- }
- };
- template <class T>
- struct Creator<true, T>
- {
- static T Create(storage_base &storage)
- {
- return T(storage);
- }
- };
-
- // match against the standard containers
+ // match against the standard containers for allocators
 
                         template <class T, class U>
                         struct is_monotonic<std::list<T, allocator<U> > > : mpl::true_ { };
@@ -76,6 +58,20 @@
 
                         template <class T, class Pred, class U>
                         struct is_monotonic<std::set<T, Pred, allocator<U> > > : mpl::true_ { };
+
+ // match against the standard containers for shared allocators
+
+ template <class T, class U>
+ struct is_monotonic<std::list<T, shared_allocator<U> > > : mpl::true_ { };
+
+ template <class T, class U>
+ struct is_monotonic<std::vector<T, shared_allocator<U> > > : mpl::true_ { };
+
+ template <class K, class T, class Pred, class U>
+ struct is_monotonic<std::map<K, T, Pred, shared_allocator<U> > > : mpl::true_ { };
+
+ template <class T, class Pred, class U>
+ struct is_monotonic<std::set<T, Pred, shared_allocator<U> > > : mpl::true_ { };
                 }
         }
 }

Modified: sandbox/monotonic/boost/monotonic/shared_allocator.hpp
==============================================================================
--- sandbox/monotonic/boost/monotonic/shared_allocator.hpp (original)
+++ sandbox/monotonic/boost/monotonic/shared_allocator.hpp 2009-06-17 19:53:58 EDT (Wed, 17 Jun 2009)
@@ -16,39 +16,65 @@
                 struct shared_allocator;
 
                 template <>
- struct shared_allocator<void> : allocator<void>
+ struct shared_allocator<void>
                 {
+ typedef void* pointer;
+ typedef const void* const_pointer;
+
+ typedef void value_type;
+ template <class U>
+ struct rebind
+ {
+ typedef shared_allocator<U> other;
+ };
                 };
 
                 template <class T>
- struct shared_allocator : allocator<T>
+ struct shared_allocator : allocator_base<T, shared_allocator<T> >
                 {
- typedef allocator<T> Parent;
+ typedef allocator_base<T, shared_allocator<T> > Parent;
+ using typename Parent::size_type;
+ using typename Parent::difference_type;
+ using typename Parent::pointer;
+ using typename Parent::const_pointer;
+ using typename Parent::reference;
+ using typename Parent::const_reference;
+ using typename Parent::value_type;
+
+ template <class U>
+ struct rebind
+ {
+ typedef shared_allocator<U> other;
+ };
 
                         shared_allocator() throw()
- : Parent(&static_shared_storage)
+ : Parent(static_shared_storage)
                         {
                         }
 
                         shared_allocator(shared_storage_base &store) throw()
- : Parent(&store)
+ : Parent(store)
                         {
                         }
 
                         shared_allocator(const shared_allocator& alloc) throw()
- : Parent(alloc.get_storage())
+ : Parent(alloc)
                         {
                         }
 
                         template <class U>
                         shared_allocator(const shared_allocator<U> &alloc) throw()
- : Parent(alloc.get_storage())
+ : Parent(alloc)
                         {
                         }
+ friend bool operator==(shared_allocator<T> const &A, shared_allocator<T> const &B)
+ {
+ return operator==(static_cast<Parent const &>(A), static_cast<Parent const &>(B));
+ }
 
- shared_storage_base *get_storage() const
- {
- return static_cast<shared_storage_base *>(Parent::get_storage());
+ friend bool operator!=(shared_allocator<T> const &A, shared_allocator<T> const &B)
+ {
+ return operator!=(static_cast<Parent const &>(A), static_cast<Parent const &>(B));
                         }
                 };
 

Modified: sandbox/monotonic/boost/monotonic/static_storage.hpp
==============================================================================
--- sandbox/monotonic/boost/monotonic/static_storage.hpp (original)
+++ sandbox/monotonic/boost/monotonic/static_storage.hpp 2009-06-17 19:53:58 EDT (Wed, 17 Jun 2009)
@@ -61,7 +61,12 @@
                 /// TODO: this will be specialised for
                 /// static_storage_base<..., shared_storage>, but to avoid having to link against boost::thread
                 /// it is currently synonymous with unshared storage ATM
- extern static_storage_base<> static_shared_storage;
+
+ // BOOST_MONOTONIC_SHARED_STORAGE_DEFINED is just a hack to avoid having to link with boost::thread
+ // while allowing the possible inclusion of boost/monotonic/shared_storage.hpp for testing
+//#ifndef BOOST_MONOTONIC_SHARED_STORAGE_DEFINED
+// extern static_storage_base<> static_shared_storage;
+//#endif
 
         } // namespace monotonic
 

Modified: sandbox/monotonic/libs/monotonic/test/main.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/main.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/main.cpp 2009-06-17 19:53:58 EDT (Wed, 17 Jun 2009)
@@ -5,11 +5,16 @@
 
 // the sandbox is at https://svn.boost.org/svn/boost/sandbox/monotonic/
 
+//#include <boost/monotonic/shared_storage.hpp>
+//#include <boost/monotonic/shared_allocator.hpp>
+
+#include <boost/monotonic/static_storage.hpp>
+
 #include <boost/monotonic/vector.hpp>
 #include <boost/monotonic/list.hpp>
 #include <boost/monotonic/map.hpp>
 #include <boost/monotonic/set.hpp>
-#include <boost/monotonic/static_storage.hpp>
+
 
 #include <boost/iterator/counting_iterator.hpp>
 
@@ -333,16 +338,20 @@
 void test_mono_map()
 {
         monotonic::storage<> store;
- {
- typedef std::list<int, monotonic::allocator<int> > List;
- typedef std::vector<List, monotonic::allocator<List> > Vector;
- BOOST_STATIC_ASSERT(monotonic::detail::is_monotonic<List>::value);
- BOOST_STATIC_ASSERT(monotonic::detail::is_monotonic<Vector>::value);
- Vector vec(store);
- vec.resize(1);
- BOOST_ASSERT(vec[0].get_allocator().get_storage() == vec.get_allocator().get_storage());
- vec[0].push_back(42);
- }
+ //monotonic::shared_storage<> shared_store;
+ //{
+ // typedef std::list<int, monotonic::shared_allocator<int> > List;
+ // typedef std::vector<List, monotonic::shared_allocator<List> > Vector;
+
+ // BOOST_STATIC_ASSERT(monotonic::detail::is_monotonic<List>::value);
+ // BOOST_STATIC_ASSERT(monotonic::detail::is_monotonic<Vector>::value);
+
+ // Vector vec(shared_store);
+ //
+ // vec.resize(1);
+ // BOOST_ASSERT(vec[0].get_allocator().get_storage() == vec.get_allocator().get_storage());
+ // vec[0].push_back(42);
+ //}
 
         {
                 typedef std::list<int, monotonic::allocator<int> > List;


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