Boost logo

Boost-Commit :

From: phil_at_[hidden]
Date: 2008-06-06 05:54:19


Author: pbouchard
Date: 2008-06-06 05:54:18 EDT (Fri, 06 Jun 2008)
New Revision: 46188
URL: http://svn.boost.org/trac/boost/changeset/46188

Log:
Restored shifted<>::p_ to add postponed construction and added skeleton for a STL compliant allocator.
Text files modified:
   sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp | 135 +++++++++++++++++++++++++++++++++++++--
   sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp | 2
   sandbox/shifted_ptr/boost/shifted_ptr.hpp | 3
   sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp | 4 +
   4 files changed, 134 insertions(+), 10 deletions(-)

Modified: sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp
==============================================================================
--- sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp (original)
+++ sandbox/shifted_ptr/boost/detail/sh_owned_base_nt.hpp 2008-06-06 05:54:18 EDT (Fri, 06 Jun 2008)
@@ -158,7 +158,10 @@
 
 #define CONSTRUCT_OWNED(z, n, text) \
         template <BOOST_PP_REPEAT(n, TEMPLATE_DECL, 0)> \
- text(BOOST_PP_REPEAT(n, ARGUMENT_DECL, 0)) : elem_(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0)) {}
+ text(BOOST_PP_REPEAT(n, ARGUMENT_DECL, 0)) \
+ { \
+ new (element()) data_type(BOOST_PP_REPEAT(n, PARAMETER_DECL, 0)); \
+ }
 
 /**
         Object wrapper.
@@ -169,21 +172,28 @@
     {
         typedef T data_type;
 
- T elem_; // need alignas<long>
+ union
+ {
+ long a_;
+ char p_[sizeof(data_type)];
+ };
         
     public:
         class roofof;
         friend class roofof;
 
- shifted() : elem_() {}
+ shifted()
+ {
+ new (element()) data_type();
+ }
         
         BOOST_PP_REPEAT_FROM_TO(1, 10, CONSTRUCT_OWNED, shifted)
 
 
- data_type * element() { return & elem_; }
+ data_type * element() { return static_cast<data_type *>(static_cast<void *>(& p_[0])); }
 
         virtual ~shifted() { dispose(); }
- virtual void dispose() {}
+ virtual void dispose() { dispose(element(), is_array<data_type>()); }
 
         virtual void * get_deleter( std::type_info const & ti ) { return 0; } // dummy
 
@@ -193,7 +203,7 @@
             shifted<data_type> * p_;
 
         public:
- roofof(data_type * p) : p_(sh::roofof((data_type shifted<data_type>::*)(& shifted<data_type>::elem_), p)) {}
+ roofof(data_type * p) : p_(sh::roofof((data_type shifted<data_type>::*)(& shifted<data_type>::p_), p)) {}
             
             operator shifted<data_type> * () const { return p_; }
         };
@@ -207,6 +217,33 @@
         {
             pool_.deallocate(p, sizeof(shifted));
         }
+
+ private:
+ template <typename U>
+ static void dispose_array(U * p, const false_type &)
+ {
+ typedef typename remove_extent<U>::type element_type;
+
+ for (element_type * i = * p; i != * p + sizeof(data_type) / sizeof(element_type); i ++)
+ dispose(i, is_array<element_type>());
+ }
+
+ template <typename U>
+ static void dispose_array(U * p, const true_type &)
+ {
+ }
+
+ template <typename U>
+ static void dispose(U * p, const false_type &)
+ {
+ p->~U();
+ }
+
+ template <typename U>
+ static void dispose(U * p, const true_type &)
+ {
+ dispose_array(p, has_trivial_destructor<data_type>());
+ }
     };
 
 
@@ -215,7 +252,7 @@
     {
         typedef void data_type;
 
- long elem_;
+ long p_;
 
         shifted();
 
@@ -223,7 +260,7 @@
         class roofof;
         friend class roofof;
 
- data_type * element() { return & elem_; }
+ data_type * element() { return & p_; }
 
         virtual ~shifted() {}
         virtual void dispose() {}
@@ -236,13 +273,93 @@
             shifted<data_type> * p_;
 
         public:
- roofof(data_type * p) : p_(sh::roofof((long shifted<data_type>::*)(& shifted<data_type>::elem_), static_cast<long *>(p))) {}
+ roofof(data_type * p) : p_(sh::roofof((long shifted<data_type>::*)(& shifted<data_type>::p_), static_cast<long *>(p))) {}
             
             operator shifted<data_type> * () const { return p_; }
         };
     };
 
 
+/**
+ STL compliant allocator.
+*/
+
+//! FIXME
+template <typename T>
+ class shifted_allocator
+ {
+ public:
+ typedef size_t size_type;
+ typedef ptrdiff_t difference_type;
+ typedef T * pointer;
+ typedef const T * const_pointer;
+ typedef T & reference;
+ typedef const T & const_reference;
+ typedef T value_type;
+
+ template <typename U>
+ struct rebind
+ {
+ typedef shifted_allocator<U> other;
+ };
+
+ shifted_allocator() throw() {}
+ shifted_allocator(const shifted_allocator &) throw() {}
+ template <typename U>
+ shifted_allocator(const shifted_allocator<U> &) throw() {}
+
+ ~shifted_allocator() throw() {}
+ pointer address(reference x) const { return & x; }
+ const_pointer address(const_reference x) const { return & x; }
+
+ pointer allocate(size_type s, const void * = 0)
+ {
+ shifted<T> * q = shifted<T>::operator new(s * sizeof(T));
+
+ // only T's constructor will be called so take care of the rest
+ return static_cast<shifted<T> *>(new (q) owned_base)->element();
+ }
+
+ void deallocate(pointer p, size_type)
+ {
+ owned_base * q = shifted<T>::roofof(p);
+
+ // T's destructor already called so let's handle ~owned_base()
+ q->owned_base::~owned_base();
+
+ shifted<T>::operator delete(q);
+ }
+
+ //! FIXME
+ size_type max_size() const throw()
+ {
+ return size_t(-1) / sizeof(T);
+ }
+
+ void construct(pointer p, const T & x)
+ {
+ ::new (p) T(x);
+ }
+
+ void destroy(pointer p)
+ {
+ p->~T();
+ }
+ };
+
+template <typename T>
+ inline bool operator == (const shifted_allocator<T> &, const shifted_allocator<T> &)
+ {
+ return true;
+ }
+
+template <typename T>
+ inline bool operator != (const shifted_allocator<T> &, const shifted_allocator<T> &)
+ {
+ return false;
+ }
+
+
 } // namespace sh
 
 } // namespace detail

Modified: sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp
==============================================================================
--- sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp (original)
+++ sandbox/shifted_ptr/boost/detail/shifted_ptr_base.hpp 2008-06-06 05:54:18 EDT (Fri, 06 Jun 2008)
@@ -235,7 +235,7 @@
 
 
         protected:
- detail::sh::owned_base * header() const
+ owned_base * header() const
                 {
                         return (shifted<element_type> *) (typename shifted<element_type>::roofof) static_cast<element_type *>(rootof<is_polymorphic<element_type>::value>::get(po_));
                 }

Modified: sandbox/shifted_ptr/boost/shifted_ptr.hpp
==============================================================================
--- sandbox/shifted_ptr/boost/shifted_ptr.hpp (original)
+++ sandbox/shifted_ptr/boost/shifted_ptr.hpp 2008-06-06 05:54:18 EDT (Fri, 06 Jun 2008)
@@ -293,6 +293,9 @@
 
 using detail::sh::shifted_ptr;
 using detail::sh::shifted;
+using detail::sh::shifted_allocator;
+using detail::sh::operator ==;
+using detail::sh::operator !=;
 
 } // namespace boost
 

Modified: sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp
==============================================================================
--- sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp (original)
+++ sandbox/shifted_ptr/libs/smart_ptr/example/shifted_ptr_test2.cpp 2008-06-06 05:54:18 EDT (Fri, 06 Jun 2008)
@@ -20,6 +20,9 @@
 
 using boost::shifted_ptr;
 using boost::shifted;
+using boost::shifted_allocator;
+using boost::operator ==;
+using boost::operator !=;
 
 struct node {
     node() {
@@ -61,6 +64,7 @@
     ~vector() { --count; }
     vector(const vector& other) : elements(other.elements) { ++count; }
     std::vector<shifted_ptr<vector> > elements;
+ //std::vector<shifted_ptr<vector>, shifted_allocator<vector> > elements; //! FIXME
 };
 
 struct create_type {


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