Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54440 - in sandbox/monotonic: boost/ptr_container libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-28 02:59:16


Author: cschladetsch
Date: 2009-06-28 02:59:15 EDT (Sun, 28 Jun 2009)
New Revision: 54440
URL: http://svn.boost.org/trac/boost/changeset/54440

Log:
added heterogenous vector

Text files modified:
   sandbox/monotonic/boost/ptr_container/ptr_vector.hpp | 41 -------------
   sandbox/monotonic/libs/monotonic/test/clones/main.cpp | 126 +++++++++++++++++++++++++++++++++++++--
   2 files changed, 117 insertions(+), 50 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:59:15 EDT (Sun, 28 Jun 2009)
@@ -61,47 +61,6 @@
             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);
- }
         };
 
     //////////////////////////////////////////////////////////////////////////////

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:59:15 EDT (Sun, 28 Jun 2009)
@@ -45,13 +45,122 @@
         explicit derived3(float f, int n, std::string const &s) : real(f), num(n), str(s) { }
 };
 
-namespace heterogenous
+namespace boost
 {
- template <class Alloc = std::allocator<char> >
- struct vector : boost::ptr_vector<cloneable::common_base, cloneable::allocator, Alloc>
+ namespace heterogenous
         {
- };
+ template <class Alloc = std::allocator<char> >
+ struct vector
+ {
+ typedef ptr_vector<cloneable::common_base, cloneable::allocator, Alloc> implementation;
+ typedef Alloc allocator_type;
+ typedef typename implementation::value_type value_type;
+ typedef typename implementation::reference reference_type;
+ typedef typename implementation::iterator iterator;
+ typedef typename implementation::const_iterator const_iterator;
+
+ private:
+ implementation impl;
+
+ public:
+
+ size_t size() const
+ {
+ return impl.size();
+ }
+
+ iterator begin()
+ {
+ return impl.begin();
+ }
+ iterator end()
+ {
+ return impl.end();
+ }
+ const_iterator begin() const
+ {
+ return impl.begin();
+ }
+ const_iterator end() const
+ {
+ return impl.end();
+ }
+ reference_type at(size_t n)
+ {
+ return impl.at(n);
+ }
+ reference_type operator[](size_t n)
+ {
+ return impl[n];
+ }
+ template <class Other>
+ bool is_type_at(size_t n) const
+ {
+ return ptr_at<Other>(n) != 0;
+ }
+ template <class Other>
+ Other &ref_at(size_t n)
+ {
+ Other *ptr = ptr_at<Other>(n);
+ if (ptr == 0)
+ throw std::bad_cast();
+ return *ptr;
+ }
+ template <class Other>
+ Other *ptr_at(size_t n)
+ {
+ return dynamic_cast<Other *>(&at(n));
+ }
+ 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);
+ impl.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);
+ impl.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);
+ impl.push_back(ptr);
+ }
+
+ allocator_type get_allocator()
+ {
+ return impl.get_allocator();
+ }
+ private:
+ template <class U>
+ U *allocate_type()
+ {
+ typename allocator_type::template rebind<U>::other alloc(get_allocator());
+ return alloc.allocate(1);
+ }
+ template <class U>
+ U *construct_type()
+ {
+ typename allocator_type::template rebind<U>::other alloc(get_allocator());
+ U *ptr = alloc.allocate(1);
+ alloc.construct(ptr);
+ return ptr;
+ }
 
+ };
+ }
 }
 
 int main()
@@ -72,15 +181,14 @@
                 vec copy = bases;
                 BOOST_ASSERT(copy.size() == 3);
 
- derived *p1 = dynamic_cast<derived *>(&copy[0]);
- derived2 *p2 = dynamic_cast<derived2 *>(&copy[1]);
- derived3 *p3 = dynamic_cast<derived3 *>(&copy[2]);
+ derived &p1 = copy.ref_at<derived>(0);
+ derived2 *p2 = copy.ptr_at<derived2>(1);
+ derived3 *p3 = copy.ptr_at<derived3>(1);
                 
- BOOST_ASSERT(p1);
                 BOOST_ASSERT(p2);
                 BOOST_ASSERT(p3);
                 
- BOOST_ASSERT(p1->num == 42);
+ BOOST_ASSERT(p1.num == 42);
                 BOOST_ASSERT(p2->str == "foo");
                 BOOST_ASSERT(p3->real == 3.14f);
                 BOOST_ASSERT(p3->num == -123);


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