File: sp_unique_ptr_test2.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (153 lines) | stat: -rw-r--r-- 3,013 bytes parent folder | download | duplicates (6)
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
// Copyright 2021 Peter Dimov
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/shared_ptr.hpp>
#include <boost/core/lightweight_test.hpp>
#include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
#include <memory>
#include <utility>

#if defined( BOOST_NO_CXX11_SMART_PTR )

BOOST_PRAGMA_MESSAGE("Skipping test because BOOST_NO_CXX11_SMART_PTR is defined")
int main() {}

#elif defined( BOOST_NO_CXX11_RVALUE_REFERENCES )

BOOST_PRAGMA_MESSAGE("Skipping test because BOOST_NO_CXX11_RVALUE_REFERENCES is defined")
int main() {}

#elif defined(BOOST_MSVC) && BOOST_MSVC < 1700

BOOST_PRAGMA_MESSAGE("Skipping test because msvc-10.0 unique_ptr doesn't support move-only deleters")
int main() {}

#else

struct Y
{
    static int instances;

    bool deleted_;

    Y(): deleted_( false )
    {
        ++instances;
    }

    ~Y()
    {
        BOOST_TEST( deleted_ );
        --instances;
    }

private:

    Y( Y const & );
    Y & operator=( Y const & );
};

int Y::instances = 0;

struct YD
{
    bool moved_;

    YD(): moved_( false )
    {
    }

    YD( YD&& r ): moved_( false )
    {
        r.moved_ = true;
    }

    void operator()( Y* p ) const
    {
        BOOST_TEST( !moved_ );

        if( p )
        {
            p->deleted_ = true;
            delete p;
        }
        else
        {
            BOOST_ERROR( "YD::operator()(0) called" );
        }
    }

private:

    YD( YD const & );
    YD & operator=( YD const & );
};

int main()
{
    BOOST_TEST( Y::instances == 0 );

    {
        std::unique_ptr<Y, YD> p( new Y );
        BOOST_TEST( Y::instances == 1 );

        boost::shared_ptr<Y> p2( std::move( p ) );

        BOOST_TEST( Y::instances == 1 );
        BOOST_TEST( p.get() == 0 );
        BOOST_TEST( p.get_deleter().moved_ );

        p2.reset();
        BOOST_TEST( Y::instances == 0 );
    }

    {
        std::unique_ptr<Y, YD> p( new Y );
        BOOST_TEST( Y::instances == 1 );

        boost::shared_ptr<void> p2( std::move( p ) );

        BOOST_TEST( Y::instances == 1 );
        BOOST_TEST( p.get() == 0 );
        BOOST_TEST( p.get_deleter().moved_ );

        p2.reset();
        BOOST_TEST( Y::instances == 0 );
    }

    {
        std::unique_ptr<Y, YD> p( new Y );
        BOOST_TEST( Y::instances == 1 );

        boost::shared_ptr<Y> p2;
        p2 = std::move( p );

        BOOST_TEST( Y::instances == 1 );
        BOOST_TEST( p.get() == 0 );
        BOOST_TEST( p.get_deleter().moved_ );

        p2.reset();
        BOOST_TEST( Y::instances == 0 );
    }

    {
        std::unique_ptr<Y, YD> p( new Y );
        BOOST_TEST( Y::instances == 1 );

        boost::shared_ptr<void> p2( new int(0) );
        p2 = std::move( p );

        BOOST_TEST( Y::instances == 1 );
        BOOST_TEST( p.get() == 0 );
        BOOST_TEST( p.get_deleter().moved_ );

        p2.reset();
        BOOST_TEST( Y::instances == 0 );
    }

    return boost::report_errors();
}

#endif