1//
2// ip_hash_test.cpp
3//
4// Copyright 2011 Peter Dimov
5//
6// Distributed under the Boost Software License, Version 1.0.
7// See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt
9//
10
11#include <boost/intrusive_ptr.hpp>
12#include <boost/functional/hash.hpp>
13#include <boost/core/lightweight_test.hpp>
14
15class base
16{
17private:
18
19 int use_count_;
20
21 base(base const &);
22 base & operator=(base const &);
23
24protected:
25
26 base(): use_count_(0)
27 {
28 }
29
30 virtual ~base()
31 {
32 }
33
34public:
35
36 long use_count() const
37 {
38 return use_count_;
39 }
40
41 inline friend void intrusive_ptr_add_ref(base * p)
42 {
43 ++p->use_count_;
44 }
45
46 inline friend void intrusive_ptr_release(base * p)
47 {
48 if(--p->use_count_ == 0) delete p;
49 }
50};
51
52struct X: public base
53{
54};
55
56int main()
57{
58 boost::hash< boost::intrusive_ptr<X> > hasher;
59
60 boost::intrusive_ptr<X> p1, p2( p1 ), p3( new X ), p4( p3 ), p5( new X );
61
62 BOOST_TEST_EQ( p1, p2 );
63 BOOST_TEST_EQ( hasher( p1 ), hasher( p2 ) );
64
65 BOOST_TEST_NE( p1, p3 );
66 BOOST_TEST_NE( hasher( p1 ), hasher( p3 ) );
67
68 BOOST_TEST_EQ( p3, p4 );
69 BOOST_TEST_EQ( hasher( p3 ), hasher( p4 ) );
70
71 BOOST_TEST_NE( p3, p5 );
72 BOOST_TEST_NE( hasher( p3 ), hasher( p5 ) );
73
74 return boost::report_errors();
75}
76

source code of boost/libs/smart_ptr/test/ip_hash_test.cpp