File: shared_from_this_test.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (164 lines) | stat: -rw-r--r-- 2,990 bytes parent folder | download | duplicates (5)
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
//
//  shared_from_this_test.cpp
//
//  Copyright (c) 2002, 2003 Peter Dimov
//
// Distributed under 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)
//

#if defined(__GNUC__) && __GNUC__ > 4
# pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
#endif

#include <boost/enable_shared_from_this.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/config.hpp>

#include <boost/core/lightweight_test.hpp>

//

class X
{
public:

    virtual void f() = 0;

protected:

    ~X() {}
};

class Y
{
public:

    virtual boost::shared_ptr<X> getX() = 0;

protected:

    ~Y() {}
};

boost::shared_ptr<Y> createY();

void test()
{
    boost::shared_ptr<Y> py = createY();
    BOOST_TEST(py.get() != 0);
    BOOST_TEST(py.use_count() == 1);

    try
    {
        boost::shared_ptr<X> px = py->getX();
        BOOST_TEST(px.get() != 0);
        BOOST_TEST(py.use_count() == 2);

        px->f();

#if !defined( BOOST_NO_RTTI )
        boost::shared_ptr<Y> py2 = boost::dynamic_pointer_cast<Y>(px);
        BOOST_TEST(py.get() == py2.get());
        BOOST_TEST(!(py < py2 || py2 < py));
        BOOST_TEST(py.use_count() == 3);
#endif
    }
    catch( boost::bad_weak_ptr const& )
    {
        BOOST_ERROR( "py->getX() failed" );
    }
}

void test2();
void test3();

int main()
{
    test();
    test2();
    test3();
    return boost::report_errors();
}

// virtual inheritance to stress the implementation
// (prevents Y* -> impl*, enable_shared_from_this<impl>* -> impl* casts)

class impl: public X, public virtual Y, public virtual boost::enable_shared_from_this<impl>
{
public:

    virtual void f()
    {
    }

    virtual boost::shared_ptr<X> getX()
    {
        boost::shared_ptr<impl> pi = shared_from_this();
        BOOST_TEST(pi.get() == this);
        return pi;
    }
};

// intermediate impl2 to stress the implementation

class impl2: public impl
{
};

boost::shared_ptr<Y> createY()
{
    boost::shared_ptr<Y> pi(new impl2);
    return pi;
}

void test2()
{
    boost::shared_ptr<Y> pi(static_cast<impl2*>(0));
}

//

struct V: public boost::enable_shared_from_this<V>
{
};

void test3()
{
    boost::shared_ptr<V> p(new V);

    try
    {
        boost::shared_ptr<V> q = p->shared_from_this();
        BOOST_TEST(p == q);
        BOOST_TEST(!(p < q) && !(q < p));
    }
    catch( boost::bad_weak_ptr const & )
    {
        BOOST_ERROR( "p->shared_from_this() failed" );
    }

    V v2(*p);

    try
    {
        boost::shared_ptr<V> r = v2.shared_from_this();
        BOOST_ERROR("v2.shared_from_this() failed to throw");
    }
    catch( boost::bad_weak_ptr const & )
    {
    }

    try
    {
        *p = V();
        boost::shared_ptr<V> r = p->shared_from_this();
        BOOST_TEST(p == r);
        BOOST_TEST(!(p < r) && !(r < p));
    }
    catch( boost::bad_weak_ptr const & )
    {
        BOOST_ERROR("p->shared_from_this() threw bad_weak_ptr after *p = V()");
    }
}