1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
//
// Boost.Pointer Container
//
// Copyright Thorsten Ottosen 2003-2005. Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// For more information, see http://www.boost.org/libs/ptr_container/
//
#include <boost/static_assert.hpp>
#include <boost/type_traits.hpp>
#include <boost/config.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/lexical_cast.hpp>
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <cstdlib>
using namespace std;
using namespace boost;
//////////////////////////////////////////////////////////////////////////////
// Test class 1: a class hierarchy
//////////////////////////////////////////////////////////////////////////////
class Base
{
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& );
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 true;
}
virtual bool equal( const Base& b ) const
{
return this == &b ;
}
bool data_less_than( const Base& b ) const
{
return data1 == b.data1 && data2 == b.data2 && data3 < b.data3;
}
bool data_less_than2( const Base& b ) const
{
return data_less_than(b) && 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
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 !l.less_than( r ) && r.less_than( 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 );
}
class Derived_class : public Base
{
int i_;
virtual void do_print( ostream& out ) const
{
out << i_;
}
virtual Base* do_clone() const
{
return new Derived_class;
}
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_;
}
};
//////////////////////////////////////////////////////////////////////////////
// Test class 2: a value class
//////////////////////////////////////////////////////////////////////////////
class Value
{
string s_;
public:
Value() : s_( boost::lexical_cast<string>( rand() ) )
{}
~Value() { /** debug code here */ }
string name() const
{
return s_;
}
};
inline bool operator<( const Value& l, const Value& r )
{
return l.name() < r.name();
}
inline bool operator>( const Value& l, const Value& r )
{
return l.name() > r.name();
}
inline bool operator==( const Value& l, const Value& r )
{
return l.name() == r.name();
}
inline bool operator!=( const Value& l, const Value& r )
{
return l.name() != r.name();
}
inline ostream& operator<<( ostream& out, const Value& v )
{
return out << v.name() << " ";
}
//
// used to hide "unused variable" warnings
//
template< class T >
inline void hide_warning( T& r )
{ }
//
// transfer() test
//
template< class Cont1, class Cont2 >
void transfer_test( Cont1& from, Cont2& to )
{
BOOST_CHECK( !from.empty() );
to. BOOST_NESTED_TEMPLATE transfer<Cont1>( from );
BOOST_CHECK( !to.empty() );
}
|