]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/ptr_container/test/simple_test.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / ptr_container / test / simple_test.cpp
1 //
2 // Boost.Pointer Container
3 //
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)
8 //
9 // For more information, see http://www.boost.org/libs/ptr_container/
10 //
11
12 #include <boost/ptr_container/ptr_vector.hpp>
13 #include <boost/shared_ptr.hpp>
14 #include <boost/lambda/lambda.hpp>
15 #include <algorithm>
16
17 using namespace std;
18
19 //
20 // A simple polymorphic class
21 //
22 class Poly
23 {
24 int i_;
25 static int cnt_;
26
27 public:
28 Poly() : i_( cnt_++ ) { }
29 virtual ~Poly() { }
30 void foo() { doFoo(); }
31
32 private:
33 virtual void doFoo() { ++i_; }
34
35 public:
36 friend inline bool operator>( const Poly& l, const Poly r )
37 {
38 return l.i_ > r.i_;
39 }
40 };
41
42 int Poly::cnt_ = 0;
43
44 //
45 // Normally we need something like this to compare pointers to objects
46 //
47 template< typename T >
48 struct sptr_greater
49 {
50 bool operator()( const boost::shared_ptr<T>& l, const boost::shared_ptr<T>& r ) const
51 {
52 return *l > *r;
53 }
54 };
55
56 //
57 // one doesn't need to introduce new names or live with long ones
58 //
59 typedef boost::shared_ptr<Poly> PolyPtr;
60
61
62 void simple_test()
63 {
64 enum { size = 2000 };
65 typedef vector<PolyPtr> vector_t;
66 typedef boost::ptr_vector<Poly> ptr_vector_t;
67 vector_t svec;
68 ptr_vector_t pvec;
69
70 for( int i = 0; i < size; ++i )
71 {
72 svec.push_back( PolyPtr( new Poly ) );
73 pvec.push_back( new Poly ); // no extra syntax
74 }
75
76 for( int i = 0; i < size; ++i )
77 {
78 svec[i]->foo();
79 pvec[i].foo(); // automatic indirection
80 svec[i] = PolyPtr( new Poly );
81 pvec.replace( i, new Poly ); // direct pointer assignment not possible, original element is deleted
82 }
83
84 for( vector_t::iterator i = svec.begin(); i != svec.end(); ++i )
85 (*i)->foo();
86
87 for( ptr_vector_t::iterator i = pvec.begin(); i != pvec.end(); ++i )
88 i->foo(); // automatic indirection
89 }
90
91 #include <boost/test/unit_test.hpp>
92 using boost::unit_test::test_suite;
93
94 test_suite* init_unit_test_suite( int argc, char* argv[] )
95 {
96 test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
97
98 test->add( BOOST_TEST_CASE( &simple_test ) );
99
100 return test;
101 }
102
103
104
105
106