#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lambda/lambda.hpp>
#include <algorithm>
using namespace std;
class Poly
{
int i_;
static int cnt_;
public:
Poly() : i_( cnt_++ ) { }
virtual ~Poly() { }
void foo() { doFoo(); }
private:
virtual void doFoo() { ++i_; }
public:
friend inline bool operator>( const Poly& l, const Poly r )
{
return l.i_ > r.i_;
}
};
int Poly::cnt_ = 0;
template< typename T >
struct sptr_greater
{
bool operator()( const boost::shared_ptr<T>& l, const boost::shared_ptr<T>& r ) const
{
return *l > *r;
}
};
typedef boost::shared_ptr<Poly> PolyPtr;
void simple_test()
{
enum { size = 2000 };
typedef vector<PolyPtr> vector_t;
typedef boost::ptr_vector<Poly> ptr_vector_t;
vector_t svec;
ptr_vector_t pvec;
for( int i = 0; i < size; ++i )
{
svec.push_back( PolyPtr( new Poly ) );
pvec.push_back( new Poly ); }
for( int i = 0; i < size; ++i )
{
svec[i]->foo();
pvec[i].foo(); svec[i] = PolyPtr( new Poly );
pvec.replace( i, new Poly ); }
for( vector_t::iterator i = svec.begin(); i != svec.end(); ++i )
(*i)->foo();
for( ptr_vector_t::iterator i = pvec.begin(); i != pvec.end(); ++i )
i->foo(); }
#include <boost/test/unit_test.hpp>
using boost::unit_test::test_suite;
test_suite* init_unit_test_suite( int argc, char* argv[] )
{
test_suite* test = BOOST_TEST_SUITE( "Pointer Container Test Suite" );
test->add( BOOST_TEST_CASE( &simple_test ) );
return test;
}