|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r54438 - in sandbox/monotonic: boost/ptr_container libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-28 02:39:19
Author: cschladetsch
Date: 2009-06-28 02:39:17 EDT (Sun, 28 Jun 2009)
New Revision: 54438
URL: http://svn.boost.org/trac/boost/changeset/54438
Log:
added push_back overloads
Text files modified:
sandbox/monotonic/boost/ptr_container/ptr_vector.hpp | 44 +++++++++++++++++++++++++++
sandbox/monotonic/libs/monotonic/test/clones/main.cpp | 62 ++++++++++++++-------------------------
2 files changed, 65 insertions(+), 41 deletions(-)
Modified: sandbox/monotonic/boost/ptr_container/ptr_vector.hpp
==============================================================================
--- sandbox/monotonic/boost/ptr_container/ptr_vector.hpp (original)
+++ sandbox/monotonic/boost/ptr_container/ptr_vector.hpp 2009-06-28 02:39:17 EDT (Sun, 28 Jun 2009)
@@ -60,7 +60,49 @@
{
this->base().reserve( n );
}
- };
+
+ template <class U>
+ U *allocate_type()
+ {
+ typename Allocator::template rebind<U>::other alloc(get_allocator());
+ return alloc.allocate(1);
+ }
+ template <class U>
+ U *construct_type()
+ {
+ typename Allocator::template rebind<U>::other alloc(get_allocator());
+ U *ptr = alloc.allocate(1);
+ alloc.construct(ptr);
+ return ptr;
+ }
+ template <class U>
+ void push_back()
+ {
+ U *ptr = construct_type<U>();
+ base().push_back(ptr);
+ }
+ template <class U, class A0>
+ void push_back(A0 a0)
+ {
+ U *ptr = allocate_type<U>();
+ new (ptr) U(a0);
+ base().push_back(ptr);
+ }
+ template <class U, class A0, class A1>
+ void push_back(A0 a0, A1 a1)
+ {
+ U *ptr = allocate_type<U>();
+ new (ptr) U(a0, a1);
+ base().push_back(ptr);
+ }
+ template <class U, class A0, class A1, class A2>
+ void push_back(A0 a0, A1 a1, A2 a2)
+ {
+ U *ptr = allocate_type<U>();
+ new (ptr) U(a0, a1, a2);
+ base().push_back(ptr);
+ }
+ };
//////////////////////////////////////////////////////////////////////////////
// clonability
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 02:39:17 EDT (Sun, 28 Jun 2009)
@@ -34,8 +34,20 @@
explicit derived2(std::string const &n) : str(n) { }
};
+
+struct derived3 : cloneable::base<derived3>
+{
+ float real;
+ int num;
+ std::string str;
+
+ derived3() { }
+ explicit derived3(float f, int n, std::string const &s) : real(f), num(n), str(s) { }
+};
+
int main()
{
+ // there is a problem with static_move_ptr<>
//typedef cloneable::make_cloneable_allocator<std::allocator<int> >::type alloc_type;
typedef cloneable::make_cloneable_allocator<monotonic::allocator<int> >::type alloc_type;
@@ -43,54 +55,24 @@
{
vec bases;
-// BOOST_ASSERT(bases.get_allocator().get_storage() == &monotonic::static_storage<>::get_storage());
-
- //! bases.push_back(new derived(42)); // this doesn't use the custom allocator!
- //! derived *obj = bases.get_allocator().allocate(1); // this has to be recast
-
- // do a dance to get the object into the container...
- typedef vec::allocator_type::template rebind<derived>::other derived_alloc_type;
- derived_alloc_type derived_alloc(bases.get_allocator());
- derived *obj = derived_alloc.allocate(1);
- //! derived_alloc.construct(obj, 42); // can't pass ctor args to a v1 allocator
- new (obj) derived(42); // bypassing allocator::construct :/
-
- // do a dance to get the object into the container...
- typedef vec::allocator_type::template rebind<derived2>::other derived2_alloc_type;
- derived2_alloc_type derived2_alloc(bases.get_allocator());
- derived2 *obj2 = derived2_alloc.allocate(1);
- //! derived_alloc.construct(obj, 42); // can't pass ctor args to a v1 allocator
- new (obj2) derived2("foo"); // bypassing allocator::construct :/
-
- // finally get the correctly allocated objects into the container
- bases.push_back(obj);
- bases.push_back(obj2);
-
-
- // idea: use variadic template arguments for push_back etc:
- // default to use BOOST_PP for C++03
-
- //! bases.push_back<derived>(ctor_args...);
- //! bases.push_back<derived2>(ctor_args...);
- //! ...
- //! bases.push_back<derivedN>(ctor_args...);
-
- // this now works properly; after small changes to:
- // ptr_container/detail/scoped_ptr.hpp
- // ptr_container/detail/reversible_ptr_container.hpp
- // and by introducing boost::abstract_allocator
- //
- // these are all in the monotonic sandbox at https://svn.boost.org/svn/boost/sandbox/monotonic/
+ bases.push_back<derived>(42);
+ bases.push_back<derived2>("foo");
+ bases.push_back<derived3>(3.14f, -123, "spam");
- BOOST_ASSERT(bases.size() == 2);
+ BOOST_ASSERT(bases.size() == 3);
vec copy = bases;
- BOOST_ASSERT(copy.size() == 2);
+ BOOST_ASSERT(copy.size() == 3);
derived *p1 = dynamic_cast<derived *>(©[0]);
derived2 *p2 = dynamic_cast<derived2 *>(©[1]);
+ derived3 *p3 = dynamic_cast<derived3 *>(©[2]);
BOOST_ASSERT(p1);
BOOST_ASSERT(p2);
+ BOOST_ASSERT(p3);
BOOST_ASSERT(p1->num == 42);
BOOST_ASSERT(p2->str == "foo");
+ BOOST_ASSERT(p3->real == 3.14f);
+ BOOST_ASSERT(p3->num == -123);
+ BOOST_ASSERT(p3->str == "spam");
}
monotonic::static_storage<>::release();
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