forked from boostorg/smart_ptr
Moved smart_ptr contents to subdirectories, switched to a local test/Jamfile.
[SVN r17042]
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
// Boost scoped_ptr_example implementation file -----------------------------//
|
||||
|
||||
#include "scoped_ptr_example.hpp"
|
||||
#include <iostream>
|
||||
|
||||
class example::implementation
|
||||
{
|
||||
public:
|
||||
~implementation() { std::cout << "destroying implementation\n"; }
|
||||
};
|
||||
|
||||
example::example() : _imp( new implementation ) {}
|
||||
|
||||
void example::do_something() { std::cout << "did something\n"; }
|
||||
|
||||
example::~example() {}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Boost scoped_ptr_example header file ------------------------------------//
|
||||
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/scoped_ptr.hpp>
|
||||
|
||||
// The point of this example is to prove that even though
|
||||
// example::implementation is an incomplete type in translation units using
|
||||
// this header, scoped_ptr< implementation > is still valid because the type
|
||||
// is complete where it counts - in the inplementation translation unit where
|
||||
// destruction is actually instantiated.
|
||||
|
||||
class example : private boost::noncopyable
|
||||
{
|
||||
public:
|
||||
example();
|
||||
~example();
|
||||
void do_something();
|
||||
private:
|
||||
class implementation;
|
||||
boost::scoped_ptr< implementation > _imp; // hide implementation details
|
||||
};
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
// Boost scoped_ptr_example_test main program -------------------------------//
|
||||
|
||||
#include "scoped_ptr_example.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
example my_example;
|
||||
my_example.do_something();
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
// Boost shared_ptr_example.cpp --------------------------------------------//
|
||||
|
||||
// (C) Copyright Beman Dawes 2001. Permission to copy,
|
||||
// use, modify, sell and distribute this software is granted provided this
|
||||
// copyright notice appears in all copies. This software is provided "as is"
|
||||
// without express or implied warranty, and with no claim as to its
|
||||
// suitability for any purpose.
|
||||
|
||||
// See http://www.boost.org for most recent version including documentation.
|
||||
|
||||
// Revision History
|
||||
// 21 May 01 Initial complete version (Beman Dawes)
|
||||
|
||||
// The original code for this example appeared in the shared_ptr documentation.
|
||||
// Ray Gallimore pointed out that foo_set was missing a Compare template
|
||||
// argument, so would not work as intended. At that point the code was
|
||||
// turned into an actual .cpp file so it could be compiled and tested.
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
// The application will produce a series of
|
||||
// objects of type Foo which later must be
|
||||
// accessed both by occurrence (std::vector)
|
||||
// and by ordering relationship (std::set).
|
||||
|
||||
struct Foo
|
||||
{
|
||||
Foo( int _x ) : x(_x) {}
|
||||
~Foo() { std::cout << "Destructing a Foo with x=" << x << "\n"; }
|
||||
int x;
|
||||
/* ... */
|
||||
};
|
||||
|
||||
typedef boost::shared_ptr<Foo> FooPtr;
|
||||
|
||||
struct FooPtrOps
|
||||
{
|
||||
bool operator()( const FooPtr & a, const FooPtr & b )
|
||||
{ return a->x > b->x; }
|
||||
void operator()( const FooPtr & a )
|
||||
{ std::cout << a->x << "\n"; }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
std::vector<FooPtr> foo_vector;
|
||||
std::set<FooPtr,FooPtrOps> foo_set; // NOT multiset!
|
||||
|
||||
FooPtr foo_ptr( new Foo( 2 ) );
|
||||
foo_vector.push_back( foo_ptr );
|
||||
foo_set.insert( foo_ptr );
|
||||
|
||||
foo_ptr.reset( new Foo( 1 ) );
|
||||
foo_vector.push_back( foo_ptr );
|
||||
foo_set.insert( foo_ptr );
|
||||
|
||||
foo_ptr.reset( new Foo( 3 ) );
|
||||
foo_vector.push_back( foo_ptr );
|
||||
foo_set.insert( foo_ptr );
|
||||
|
||||
foo_ptr.reset ( new Foo( 2 ) );
|
||||
foo_vector.push_back( foo_ptr );
|
||||
foo_set.insert( foo_ptr );
|
||||
|
||||
std::cout << "foo_vector:\n";
|
||||
std::for_each( foo_vector.begin(), foo_vector.end(), FooPtrOps() );
|
||||
|
||||
std::cout << "\nfoo_set:\n";
|
||||
std::for_each( foo_set.begin(), foo_set.end(), FooPtrOps() );
|
||||
std::cout << "\n";
|
||||
|
||||
// Expected output:
|
||||
//
|
||||
// foo_vector:
|
||||
// 2
|
||||
// 1
|
||||
// 3
|
||||
// 2
|
||||
//
|
||||
// foo_set:
|
||||
// 3
|
||||
// 2
|
||||
// 1
|
||||
//
|
||||
// Destructing a Foo with x=2
|
||||
// Destructing a Foo with x=1
|
||||
// Destructing a Foo with x=3
|
||||
// Destructing a Foo with x=2
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
// Boost shared_ptr_example2 implementation file -----------------------------//
|
||||
|
||||
#include "shared_ptr_example2.hpp"
|
||||
#include <iostream>
|
||||
|
||||
class example::implementation
|
||||
{
|
||||
public:
|
||||
~implementation() { std::cout << "destroying implementation\n"; }
|
||||
};
|
||||
|
||||
example::example() : _imp( new implementation ) {}
|
||||
example::example( const example & s ) : _imp( s._imp ) {}
|
||||
|
||||
example & example::operator=( const example & s )
|
||||
{ _imp = s._imp; return *this; }
|
||||
|
||||
void example::do_something()
|
||||
{ std::cout << "use_count() is " << _imp.use_count() << "\n"; }
|
||||
@@ -0,0 +1,26 @@
|
||||
// Boost shared_ptr_example2 header file -----------------------------------//
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
// This example demonstrates the handle/body idiom (also called pimpl and
|
||||
// several other names). It separates the interface (in this header file)
|
||||
// from the implementation (in shared_ptr_example2.cpp).
|
||||
|
||||
// Note that even though example::implementation is an incomplete type in
|
||||
// some translation units using this header, shared_ptr< implementation >
|
||||
// is still valid because the type is complete where it counts - in the
|
||||
// shared_ptr_example2.cpp translation unit where functions requiring a
|
||||
// complete type are actually instantiated.
|
||||
|
||||
class example
|
||||
{
|
||||
public:
|
||||
example();
|
||||
example( const example & );
|
||||
example & operator=( const example & );
|
||||
void do_something();
|
||||
private:
|
||||
class implementation;
|
||||
boost::shared_ptr< implementation > _imp; // hide implementation details
|
||||
};
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// Boost shared_ptr_example2_test main program ------------------------------//
|
||||
|
||||
#include "shared_ptr_example2.hpp"
|
||||
|
||||
int main()
|
||||
{
|
||||
example a;
|
||||
a.do_something();
|
||||
example b(a);
|
||||
b.do_something();
|
||||
example c;
|
||||
c = a;
|
||||
c.do_something();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user