Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r54388 - trunk/libs/ptr_container/test
From: nesotto_at_[hidden]
Date: 2009-06-26 19:36:10


Author: nesotto
Date: 2009-06-26 19:36:09 EDT (Fri, 26 Jun 2009)
New Revision: 54388
URL: http://svn.boost.org/trac/boost/changeset/54388

Log:
minor update to cloning
Text files modified:
   trunk/libs/ptr_container/test/test_data.hpp | 293 ++++++++++++++++++++-------------------
   1 files changed, 152 insertions(+), 141 deletions(-)

Modified: trunk/libs/ptr_container/test/test_data.hpp
==============================================================================
--- trunk/libs/ptr_container/test/test_data.hpp (original)
+++ trunk/libs/ptr_container/test/test_data.hpp 2009-06-26 19:36:09 EDT (Fri, 26 Jun 2009)
@@ -28,177 +28,188 @@
 // Test class 1: a class hierarchy
 //////////////////////////////////////////////////////////////////////////////
 
-class Base
+namespace test
 {
- Base( const Base& r ) : data1(r.data1), data2(r.data2),
- data3(r.data3), data(r.data)
- {
-#ifdef PTR_CONTAINER_DEBUG
- objects++;
- std::cout <<"+ " << objects << "\n";
-#endif
- }
+ class Base
+ {
+ protected:
+ Base( const Base& r ) : data1(r.data1), data2(r.data2),
+ data3(r.data3), data(r.data)
+ {
+ #ifdef PTR_CONTAINER_DEBUG
+ objects++;
+ std::cout <<"+ " << objects << "\n";
+ #endif
+ }
+
+ Base& operator=( const Base& );
+
+ public: // for test reasons only
+ int data1, data2, data3;
+ string data;
+
+ public:
+
+ Base() : data1(1), data2(2), data3(rand()%256),
+ data(lexical_cast<string>(rand()))
+ {
+ #ifdef PTR_CONTAINER_DEBUG
+ objects++;
+ std::cout <<"+ " << objects << "\n";
+ #endif
+ }
+
+ virtual ~Base()
+ {
+ #ifdef PTR_CONTAINER_DEBUG
+ objects--;
+ std::cout <<"- " << objects << "\n";
+ if( objects < 0 )
+ terminate();
+ #endif
+ }
+
+ void print( ostream& out ) const { do_print( out); }
+ Base* clone() const { return do_clone(); }
+ void foo() { do_foo(); }
+
+ virtual bool less_than( const Base& b ) const
+ {
+ return data3 < b.data3;
+ }
+
+ virtual bool equal( const Base& b ) const
+ {
+ return data1 == b.data1 &&
+ data2 == b.data2 &&
+ data3 == b.data3 &&
+ data == b.data;
+ }
+
+ #ifdef PTR_CONTAINER_DEBUG
+ static int objects;
+ #endif
+
+ private:
+ virtual void do_print( ostream& /*out*/ ) const { };
+ virtual Base* do_clone() const { return new Base( *this ); };
+ virtual void do_foo() { };
+ };
+
+ #ifdef PTR_CONTAINER_DEBUG
+ int Base::objects = 0;
+ #endif
     
- Base& operator=( const Base& );
-
-public: // for test reasons only
- int data1, data2, data3;
- string data;
     
-public:
     
- Base() : data1(1), data2(2), data3(rand()%256),
- data(lexical_cast<string>(rand()))
+ ostream& operator<<( ostream& out, const Base& b )
     {
-#ifdef PTR_CONTAINER_DEBUG
- objects++;
- std::cout <<"+ " << objects << "\n";
-#endif
+ b.print( out );
+ return out;
     }
     
- virtual ~Base()
+
+ //
+ // We rely on argument dependent lookup
+ // for this to be found
+ //
+ inline Base* new_clone( const Base& b )
     {
-#ifdef PTR_CONTAINER_DEBUG
- objects--;
- std::cout <<"- " << objects << "\n";
- if( objects < 0 )
- terminate();
-#endif
+ return b.clone();
     }
     
- void print( ostream& out ) const { do_print( out); }
- Base* clone() const { return do_clone(); }
- void foo() { do_foo(); }
     
- virtual bool less_than( const Base& b ) const
+
+ inline bool operator<( const Base& l, const Base& r )
     {
- return data3 < b.data3;
+ return l.less_than( r );
     }
     
- virtual bool equal( const Base& b ) const
+
+
+ inline bool operator>( const Base& l, const Base& r )
     {
- return data1 == b.data1 &&
- data2 == b.data2 &&
- data3 == b.data3 &&
- data == b.data;
+ return r < l;
     }
-
-#ifdef PTR_CONTAINER_DEBUG
- static int objects;
-#endif
-
-private:
- virtual void do_print( ostream& /*out*/ ) const { };
- virtual Base* do_clone() const { return new Base( *this ); };
- virtual void do_foo() { };
-};
-
-#ifdef PTR_CONTAINER_DEBUG
-int Base::objects = 0;
-#endif
-
-
-
-ostream& operator<<( ostream& out, const Base& b )
-{
- b.print( out );
- return out;
-}
-
-
-//
-// We rely on argument dependent lookup
-// for this to be found
-//
-inline Base* new_clone( const Base& b )
-{
- return b.clone();
-}
-
-
-
-inline bool operator<( const Base& l, const Base& r )
-{
- return l.less_than( r );
-}
-
-
-
-inline bool operator>( const Base& l, const Base& r )
-{
- return r < l;
-}
-
-
-
-inline bool operator==( const Base& l, const Base& r )
-{
- return l.equal( r );
-}
-
-
-
-inline bool operator!=( const Base& l, const Base& r )
-{
- return !l.equal( r );
-}
-
-
-
-inline std::size_t hash_value( const Base& b )
-{
- std::size_t seed = 0;
- boost::hash_combine( seed, b.data );
- boost::hash_combine( seed, b.data1 );
- boost::hash_combine( seed, b.data2 );
- boost::hash_combine( seed, b.data3 );
- return seed;
-}
-
-
-class Derived_class : public Base
-{
-public: // for test reasons only
- int i_;
-
-private:
-
- virtual void do_print( ostream& out ) const
+
+
+
+ inline bool operator==( const Base& l, const Base& r )
     {
- out << i_;
+ return l.equal( r );
     }
     
     
- virtual Base* do_clone() const
+
+ inline bool operator!=( const Base& l, const Base& r )
     {
- return new Derived_class;
+ return !l.equal( r );
     }
     
- virtual void do_foo()
+
+
+ inline std::size_t hash_value( const Base& b )
     {
- ++i_;
+ std::size_t seed = 0;
+ boost::hash_combine( seed, b.data );
+ boost::hash_combine( seed, b.data1 );
+ boost::hash_combine( seed, b.data2 );
+ boost::hash_combine( seed, b.data3 );
+ return seed;
     }
     
-public:
- Derived_class() : i_( rand() )
- { }
-
- virtual bool less_than( const Base& b ) const
+
+ class Derived_class : public Base
+ {
+ protected:
+ Derived_class( const Derived_class& r ) : Base( r ), i_(r.i_)
+ { }
+
+ public: // for test reasons only
+ int i_;
+
+ private:
+
+ virtual void do_print( ostream& out ) const
+ {
+ out << i_;
+ }
+
+
+ virtual Base* do_clone() const
+ {
+ return new Derived_class( *this );
+ }
+
+ virtual void do_foo()
+ {
+ ++i_;
+ }
+
+ public:
+ Derived_class() : i_( rand() )
+ { }
+
+ virtual bool less_than( const Base& b ) const
+ {
+ const Derived_class& d = dynamic_cast<const Derived_class&>( b );
+ return i_ < d.i_;
+ }
+ };
+
+
+
+ inline std::size_t hash_value( const Derived_class& b )
     {
- const Derived_class& d = dynamic_cast<const Derived_class&>( b );
- return i_ < d.i_;
+ std::size_t seed = hash_value( static_cast<const Base&>( b ) );
+ boost::hash_combine( seed, b.i_ );
+ return seed;
     }
-};
-
-
-
-inline std::size_t hash_value( const Derived_class& b )
-{
- std::size_t seed = hash_value( static_cast<const Base&>( b ) );
- boost::hash_combine( seed, b.i_ );
- return seed;
 }
 
+using test::Base;
+using test::Derived_class;
+
 //////////////////////////////////////////////////////////////////////////////
 // Test class 2: a value class
 //////////////////////////////////////////////////////////////////////////////


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk