Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54471 - in sandbox/monotonic: boost/heterogenous libs/monotonic/test/clones
From: christian.schladetsch_at_[hidden]
Date: 2009-06-28 16:36:35


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

Log:
added more examples, foreach
Text files modified:
   sandbox/monotonic/boost/heterogenous/vector.hpp | 27 +++++++++++++++++++++++++++
   sandbox/monotonic/libs/monotonic/test/clones/tests.cpp | 23 ++++++++++++++++++++++-
   2 files changed, 49 insertions(+), 1 deletions(-)

Modified: sandbox/monotonic/boost/heterogenous/vector.hpp
==============================================================================
--- sandbox/monotonic/boost/heterogenous/vector.hpp (original)
+++ sandbox/monotonic/boost/heterogenous/vector.hpp 2009-06-28 16:36:34 EDT (Sun, 28 Jun 2009)
@@ -7,6 +7,7 @@
 #define BOOST_HETEROGENOUS_VECTOR_HPP
 
 #include <boost/ptr_container/ptr_vector.hpp>
+#include <boost/foreach.hpp>
 
 #include <boost/heterogenous/detail/prefix.hpp>
 #include <boost/heterogenous/base.hpp>
@@ -112,6 +113,32 @@
                         {
                                 return impl.get_allocator();
                         }
+
+ template <class Ty, class Fun>
+ Fun foreach(Fun fun)
+ {
+ BOOST_FOREACH(common_base &b, *this)
+ {
+ if (Ty *ptr = dynamic_cast<Ty *>(&b))
+ {
+ fun(*ptr);
+ }
+ }
+ return fun;
+ }
+
+ template <class Ty, class Fun>
+ Fun foreach(Fun fun) const
+ {
+ BOOST_FOREACH(const common_base &base, *this)
+ {
+ if (Ty *ptr = dynamic_cast<Ty *>(&base))
+ {
+ fun(*ptr);
+ }
+ }
+ return fun;
+ }
                 private:
                         template <class U>
                         U *allocate_type()

Modified: sandbox/monotonic/libs/monotonic/test/clones/tests.cpp
==============================================================================
--- sandbox/monotonic/libs/monotonic/test/clones/tests.cpp (original)
+++ sandbox/monotonic/libs/monotonic/test/clones/tests.cpp 2009-06-28 16:36:34 EDT (Sun, 28 Jun 2009)
@@ -7,8 +7,10 @@
 // sandbox at https://svn.boost.org/svn/boost/sandbox/monotonic/
 
 #include <string>
+#include <iostream>
 #include <boost/heterogenous/vector.hpp>
 #include <boost/monotonic/allocator.hpp>
+#include <boost/bind.hpp>
 
 using namespace std;
 using namespace boost;
@@ -37,6 +39,11 @@
 
         derived3() { }
         explicit derived3(float f, int n, std::string const &s) : real(f), num(n), str(s) { }
+
+ void print() const
+ {
+ cout << "derived3: " << real << ", " << num << ", " << str << endl;
+ }
 };
 
 // naive way of allowing reuse in derived types: factor out the implementation
@@ -59,20 +66,34 @@
         typedef heterogenous::vector<monotonic::allocator<int> > vec;
 
         {
+ // a 'heterogenous' container of objects of any type that derives from common_base
                 vec bases;
- bases.push_back<derived>(42);
+
+ // type of thing to insert must be passed explicitly, and must derive from common_base.
+ // arguments to push_back are passed directly to ctor
+ bases.push_back<derived>(42);
                 bases.push_back<derived2>("foo");
                 bases.push_back<derived3>(3.14f, -123, "spam");
 
+ // perform functor on each contained object of the given type
+ bases.foreach<derived3>(boost::bind(&derived3::print, _1));
+
                 BOOST_ASSERT(bases.size() == 3);
+
+ // does a deep copy, preserving concrete types
                 vec copy = bases;
+
                 BOOST_ASSERT(copy.size() == 3);
 
+ // each object in the container can be retrieved generically as a common_base
                 common_base &generic0 = copy[0];
                 common_base &generic1 = copy[1];
                 common_base &generic2 = copy[2];
 
+ // get a reference; will throw bad_cast on type mismatch
                 derived &p1 = copy.ref_at<derived>(0);
+
+ // get a pointer; returns null on type mismatch
                 derived2 *p2 = copy.ptr_at<derived2>(1);
                 derived3 *p3 = copy.ptr_at<derived3>(2);
                 


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