Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54428 - in sandbox/monotonic: boost libs/monotonic/test libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-28 01:36:59


Author: cschladetsch
Date: 2009-06-28 01:36:58 EDT (Sun, 28 Jun 2009)
New Revision: 54428
URL: http://svn.boost.org/trac/boost/changeset/54428

Log:
added more tests

Added:
   sandbox/monotonic/boost/cloneable.hpp (contents, props changed)
Text files modified:
   sandbox/monotonic/libs/monotonic/test/clones/main.cpp | 159 +--------------------------------------
   sandbox/monotonic/libs/monotonic/test/monotonic.vcproj | 4 +
   2 files changed, 11 insertions(+), 152 deletions(-)

Added: sandbox/monotonic/boost/cloneable.hpp
==============================================================================
--- (empty file)
+++ sandbox/monotonic/boost/cloneable.hpp 2009-06-28 01:36:58 EDT (Sun, 28 Jun 2009)
@@ -0,0 +1,111 @@
+// Copyright (C) 2009 Christian Schladetsch
+//
+// Distributed under the Boost Software License, Version 1.0. (See accompanying
+// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
+
+#ifndef BOOST_CLONEABLE_HPP
+#define BOOST_CLONEABLE_HPP
+
+#include <boost/abstract_allocator.hpp>
+#include <boost/aligned_storage.hpp>
+
+namespace boost
+{
+ namespace cloneable
+ {
+ /// common base for all base types for hierachies
+ struct common_base
+ {
+ virtual ~common_base() { }
+
+ virtual common_base *allocate(abstract_allocator &alloc) const = 0;
+ virtual common_base *create(abstract_allocator &alloc) const = 0;
+ virtual common_base *copy_construct(const common_base &original, abstract_allocator &alloc) const = 0;
+ };
+
+ /// base of the given derived type
+ template <class Derived>
+ struct base : common_base
+ {
+ typedef Derived derived_type;
+ typedef base<derived_type> this_type;
+
+ private:
+ static size_t alignment;
+ mutable derived_type *self_ptr;
+
+ derived_type *&self(derived_type *ptr) const
+ {
+ return ptr->this_type::self_ptr;
+ }
+
+ public:
+ base() : self_ptr(0) { }
+
+ virtual base<Derived> *allocate(boost::abstract_allocator &alloc) const
+ {
+ boost::abstract_allocator::pointer bytes = alloc.allocate_bytes(sizeof(derived_type), alignment);
+ Derived *ptr = reinterpret_cast<Derived *>(bytes);
+ self(ptr) = ptr;
+ return ptr;
+ }
+
+ virtual base<Derived> *create(boost::abstract_allocator &alloc) const
+ {
+ base<Derived> *ptr = allocate(alloc);
+ new (ptr->self_ptr) Derived();
+ return ptr;
+ }
+
+ virtual base<Derived> *copy_construct(const common_base &original, boost::abstract_allocator &alloc) const
+ {
+ base<Derived> *ptr = allocate(alloc);
+ new (ptr->self_ptr) Derived(static_cast<const Derived &>(original));
+ return ptr;
+ }
+ };
+
+ template <class Derived>
+ size_t base<Derived>::alignment = aligned_storage<sizeof(Derived)>::alignment;
+
+ /// a cloning allocator
+ struct allocator
+ {
+ template< class U >
+ static U* allocate_clone( const U& r )
+ {
+ throw;
+ }
+
+ template< class U >
+ static void deallocate_clone( const U* clone )
+ {
+ //throw;
+ }
+
+ // idea: pass allocator to the clone_allocator.
+ // allocator rebind could be done in the ptr_container.
+ // calling this must be disabled at compile-time for types that are not boost::is_convertible<cloneable::base<U> *, U*>
+ template< class U, class Alloc >
+ static U* allocate_clone( const U& r, Alloc &alloc )
+ {
+ U *ptr = r.copy_construct(r, alloc);
+ return ptr;
+ }
+
+ // this is not even needed?
+ template< class U, class Alloc >
+ static U* deallocate_clone( const U* r, Alloc &alloc )
+ {
+ typename Alloc::template rebind<U>::other my_alloc(alloc);
+ my_alloc.deallocate(const_cast<U *>(r));
+ }
+ };
+
+ } // namespace cloneable
+
+} // namespace boost
+
+#endif // BOOST_CLONEABLE_HPP
+
+//EOF

Modified: sandbox/monotonic/libs/monotonic/test/clones/main.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/clones/main.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/clones/main.cpp 2009-06-28 01:36:58 EDT (Sun, 28 Jun 2009)
@@ -6,178 +6,32 @@
 // documentation at https://svn.boost.org/svn/boost/sandbox/monotonic/libs/monotonic/doc/index.html
 // sandbox at https://svn.boost.org/svn/boost/sandbox/monotonic/
 
-
-
 //
 // test cases for ptr_container and clone_allocator issues
 //
 
-#include <iostream>
-#include <boost/ptr_container/ptr_vector.hpp>
+#include <string>
 #include <boost/abstract_allocator.hpp>
+#include <boost/cloneable.hpp>
 #include <boost/monotonic/allocator.hpp>
+#include <boost/ptr_container/ptr_vector.hpp>
 
 using namespace std;
 using namespace boost;
 
-namespace boost
-{
- //namespace ptr_container {
-
- namespace cloneable
- {
- struct allocator;
-
- struct dyn_alloc_base
- {
-
- };
-
- template <class Derived>
- struct dyn_alloc : dyn_alloc_base
- {
- template <class Base, class Alloc>
- static void *alloc(Base *ptr, Alloc &a)
- {
- return new Derived();
- }
- };
-
- // common base for all base types for hierachies
- struct base_base
- {
- virtual ~base_base() { }
-
- // this can't work because it doesnt have an allocator :/
- virtual base_base *clone() const { return 0; }
-
- virtual base_base *allocate(boost::abstract_allocator &alloc) const = 0;
- virtual base_base *create(boost::abstract_allocator &alloc) const = 0;
- virtual base_base *copy_construct(const base_base &original, boost::abstract_allocator &alloc) const = 0;
-
- };
-
- template <class Derived>
- struct base : base_base
- {
- typedef Derived derived_type;
- typedef base<derived_type> this_type;
-
- private:
- static size_t alignment;
- mutable derived_type *self_ptr;
-
- derived_type *&self(derived_type *ptr) const
- {
- return ptr->this_type::self_ptr;
- }
-
- public:
- base() : self_ptr(0) { }
-
- virtual base<Derived> *allocate(boost::abstract_allocator &alloc) const
- {
- boost::abstract_allocator::pointer bytes = alloc.allocate_bytes(sizeof(derived_type), alignment);
- Derived *ptr = reinterpret_cast<Derived *>(bytes);
- self(ptr) = ptr;
- return ptr;
- }
-
- virtual base<Derived> *create(boost::abstract_allocator &alloc) const
- {
- base<Derived> *ptr = allocate(alloc);
- //new (ptr) this_type();
- new (ptr->self_ptr) Derived();
- return ptr;
- }
-
- virtual base<Derived> *copy_construct(const base_base &original, boost::abstract_allocator &alloc) const
- {
- base<Derived> *ptr = allocate(alloc);
- //new (ptr) this_type();
- new (ptr->self_ptr) Derived(static_cast<const Derived &>(original));
- return ptr;
- }
- };
- template <class D>
- size_t base<D>::alignment = boost::aligned_storage<sizeof(D)>::alignment;
-
- struct allocator
- {
- template< class U >
- static U* allocate_clone( const U& r )
- {
- // this is not allocated using the parent containers custom allocator
- return r.clone();
- }
-
- template< class U >
- static void deallocate_clone( const U* clone )
- {
- // this is not de-allocated using the parent containers custom allocator
- //! ??clone->deallocate();??
- //! delete clone; // this is not correct either; also calls dtor
-
- // this is not correct either, but seems the best we can do...?
- std::allocator<char> alloc;
- U *ptr = const_cast<U *>(clone);
-// alloc.deallocate(reinterpret_cast<char *>(ptr), 1);
- }
-
- // idea: pass allocator to the clone_allocator.
- // allocator rebind could be done in the ptr_container.
- // calling this must be disabled at compile-time for types that are not boost::is_convertible<cloneable::base<U> *, U*>
- template< class U, class Alloc >
- static U* allocate_clone( const U& r, Alloc &alloc )
- {
- U *ptr = r.copy_construct(r, alloc);
- return ptr;
- }
-
- // idea: this is not even needed?
- // allocator rebind could be done in the ptr_container.
- template< class U, class Alloc >
- static U* deallocate_clone( const U* r, Alloc &alloc )
- {
- typename Alloc::template rebind<U>::other my_alloc(alloc);
- my_alloc.deallocate(const_cast<U *>(r));
- }
- };
-
- } // namespace cloneable
-
-} // namespace boost
-
-// client code...
-
 struct derived : cloneable::base<derived>
 {
         int num;
-
         derived() : num(0) { }
         explicit derived(int n) : num(n) { }
-
- // this can't work
- cloneable::base_base *clone() const
- {
- // this is really no good as it doesnt use the correct allocator
- return new derived(num);
- }
 };
 
 struct derived2 : cloneable::base<derived2>
 {
- string str;
+ std::string str;
 
         derived2() { }
- explicit derived2(string const &n) : str(n) { }
-
- // this can't work
- cloneable::base_base *clone() const
- {
- // this is really no good as it doesnt use the correct allocator
- return new derived2(str);
- }
+ explicit derived2(std::string const &n) : str(n) { }
 };
 
 /*
@@ -194,7 +48,7 @@
 
 int main()
 {
- typedef ptr_vector<cloneable::base_base, cloneable::allocator, monotonic::allocator<int> > vec;
+ typedef ptr_vector<cloneable::common_base, cloneable::allocator, monotonic::allocator<int> > vec;
 
         {
                 vec bases;
@@ -244,6 +98,7 @@
                 derived2 *p2 = dynamic_cast<derived2 *>(&copy[1]);
                 BOOST_ASSERT(p0);
                 BOOST_ASSERT(p2);
+ BOOST_ASSERT(p2->str == "foo");
 
         }
         monotonic::static_storage<>::release();

Modified: sandbox/monotonic/libs/monotonic/test/monotonic.vcproj
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/monotonic.vcproj (original)
+++ sandbox/monotonic/libs/monotonic/test/monotonic.vcproj 2009-06-28 01:36:58 EDT (Sun, 28 Jun 2009)
@@ -255,6 +255,10 @@
                                 RelativePath="..\..\..\boost\abstract_allocator.hpp"
>
                         </File>
+ <File
+ RelativePath="..\..\..\boost\cloneable.hpp"
+ >
+ </File>
                         <Filter
                                 Name="monotonic"
>


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