Brian Silverman | 355f11d | 2018-08-04 23:57:00 -0700 | [diff] [blame^] | 1 | // Boost shared_ptr_example.cpp --------------------------------------------// |
| 2 | |
| 3 | // Copyright Beman Dawes 2001. Distributed under the Boost |
| 4 | // Software License, Version 1.0. (See accompanying file |
| 5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | |
| 7 | |
| 8 | // See http://www.boost.org/libs/smart_ptr for documentation. |
| 9 | |
| 10 | // Revision History |
| 11 | // 21 May 01 Initial complete version (Beman Dawes) |
| 12 | |
| 13 | // The original code for this example appeared in the shared_ptr documentation. |
| 14 | // Ray Gallimore pointed out that foo_set was missing a Compare template |
| 15 | // argument, so would not work as intended. At that point the code was |
| 16 | // turned into an actual .cpp file so it could be compiled and tested. |
| 17 | |
| 18 | #include <vector> |
| 19 | #include <set> |
| 20 | #include <iostream> |
| 21 | #include <algorithm> |
| 22 | #include <boost/shared_ptr.hpp> |
| 23 | |
| 24 | // The application will produce a series of |
| 25 | // objects of type Foo which later must be |
| 26 | // accessed both by occurrence (std::vector) |
| 27 | // and by ordering relationship (std::set). |
| 28 | |
| 29 | struct Foo |
| 30 | { |
| 31 | Foo( int _x ) : x(_x) {} |
| 32 | ~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; } |
| 33 | int x; |
| 34 | /* ... */ |
| 35 | }; |
| 36 | |
| 37 | typedef boost::shared_ptr<Foo> FooPtr; |
| 38 | |
| 39 | struct FooPtrOps |
| 40 | { |
| 41 | bool operator()( const FooPtr & a, const FooPtr & b ) |
| 42 | { return a->x > b->x; } |
| 43 | void operator()( const FooPtr & a ) |
| 44 | { std::cout << a->x << "\n"; } |
| 45 | }; |
| 46 | |
| 47 | int main() |
| 48 | { |
| 49 | std::vector<FooPtr> foo_vector; |
| 50 | std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset! |
| 51 | |
| 52 | FooPtr foo_ptr( new Foo( 2 ) ); |
| 53 | foo_vector.push_back( foo_ptr ); |
| 54 | foo_set.insert( foo_ptr ); |
| 55 | |
| 56 | foo_ptr.reset( new Foo( 1 ) ); |
| 57 | foo_vector.push_back( foo_ptr ); |
| 58 | foo_set.insert( foo_ptr ); |
| 59 | |
| 60 | foo_ptr.reset( new Foo( 3 ) ); |
| 61 | foo_vector.push_back( foo_ptr ); |
| 62 | foo_set.insert( foo_ptr ); |
| 63 | |
| 64 | foo_ptr.reset ( new Foo( 2 ) ); |
| 65 | foo_vector.push_back( foo_ptr ); |
| 66 | foo_set.insert( foo_ptr ); |
| 67 | |
| 68 | std::cout << "foo_vector:\n"; |
| 69 | std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() ); |
| 70 | |
| 71 | std::cout << "\nfoo_set:\n"; |
| 72 | std::for_each( foo_set.begin(), foo_set.end(), FooPtrOps() ); |
| 73 | std::cout << "\n"; |
| 74 | |
| 75 | // Expected output: |
| 76 | // |
| 77 | // foo_vector: |
| 78 | // 2 |
| 79 | // 1 |
| 80 | // 3 |
| 81 | // 2 |
| 82 | // |
| 83 | // foo_set: |
| 84 | // 3 |
| 85 | // 2 |
| 86 | // 1 |
| 87 | // |
| 88 | // Destructing a Foo with x=2 |
| 89 | // Destructing a Foo with x=1 |
| 90 | // Destructing a Foo with x=3 |
| 91 | // Destructing a Foo with x=2 |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |