2 // Boost.Pointer Container
4 // Copyright Thorsten Ottosen 2003-2005. Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see http://www.boost.org/libs/ptr_container/
12 #include "test_data.hpp"
13 #include <boost/ptr_container/ptr_vector.hpp>
14 #include <boost/utility.hpp>
19 using namespace boost
;
22 // Forward declare 'allocate_clone()' to be able
23 // use clonability of 'Composite' inline in the class.
24 // This is normally not needed when using .hpp + .cpp files.
27 Composite
* new_clone( const Composite
& );
32 typedef ptr_vector
<Composite
> composite_t
;
33 typedef composite_t::iterator iterator
;
34 typedef composite_t::const_iterator const_iterator
;
35 typedef composite_t::size_type size_type
;
36 composite_t elements_
;
39 // only used internally for 'clone()'
41 Composite( const Composite
& r
) : elements_( r
.elements_
.clone() )
45 // this class is not Copyable nor Assignable
47 void operator=( const Composite
& );
54 // of course detructor is virtual
60 // one way of adding new elements
62 void add( Composite
* c
)
64 elements_
.push_back( c
);
68 // second way of adding new elements
70 void add( Composite
& c
)
72 elements_
.push_back( new_clone( c
) );
75 void remove( iterator where
)
77 elements_
.erase( where
);
81 // recusively count the elements
83 size_type
size() const
86 for( const_iterator i
= elements_
.begin(); i
!= elements_
.end(); ++i
)
88 return 1 /* this */ + res
;
94 for( iterator i
= elements_
.begin(); i
!= elements_
.end(); ++i
)
99 // this class is clonable and this is the callback for 'allocate_clone()'
101 Composite
* clone() const
107 virtual void do_foo()
109 cout
<< "composite base" << "\n";
112 virtual Composite
* do_clone() const
114 return new Composite( *this );
119 // make 'Composite' clonable; note that we do not need to overload
120 // the function in the 'boost' namespace.
122 Composite
* new_clone( const Composite
& c
)
128 class ConcreteComposite1
: public Composite
130 virtual void do_foo()
132 cout
<< "composite 1" << "\n";
135 virtual Composite
* do_clone() const
137 return new ConcreteComposite1();
142 class ConcreteComposite2
: public Composite
144 virtual void do_foo()
146 cout
<< "composite 2" << "\n";
149 virtual Composite
* do_clone() const
151 return new ConcreteComposite2();
155 void test_incomplete()
158 c
.add( new ConcreteComposite1
);
159 c
.add( new ConcreteComposite2
);
160 BOOST_CHECK_EQUAL( c
.size(), 3u );
161 c
.add( new_clone( c
) ); // add c to itself
162 BOOST_CHECK_EQUAL( c
.size(), 6u );
163 c
.add( c
); // add c to itself
164 BOOST_CHECK_EQUAL( c
.size(), 12u );
168 using namespace boost
;
171 #include <boost/test/unit_test.hpp>
172 using boost::unit_test::test_suite
;
174 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
176 test_suite
* test
= BOOST_TEST_SUITE( "Pointer Container Test Suite" );
178 test
->add( BOOST_TEST_CASE( &test_incomplete
) );
184 // todo: remake example with shared_ptr