File: if_copyable_macro.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 (172 lines) | stat: -rw-r--r-- 5,012 bytes parent folder | download | duplicates (14)
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

// Copyright (C) 2008-2018 Lorenzo Caminiti
// Distributed under the Boost Software License, Version 1.0 (see accompanying
// file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
// See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html

// Test old values of non-copyable types (using macro interface).

#include "if_copyable.hpp"
#include "../detail/unprotected_commas.hpp"
#include <boost/contract_macro.hpp>
#include <boost/noncopyable.hpp>
#include <boost/detail/lightweight_test.hpp>

unsigned old_checks;

template<typename T>
struct b {
    virtual void next(T& x, boost::contract::virtual_* v = 0) {
        BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(
            typename boost::contract::test::detail::unprotected_commas<T, void,
                    void>::type1
        )(
            (boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(v)),
            old_x,
            (boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(x))
        );
        BOOST_CONTRACT_PUBLIC_FUNCTION(
            boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(v),
            boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(this)
        )
            BOOST_CONTRACT_POSTCONDITION([&] {
                boost::contract::test::detail::unprotected_commas<
                        void, void, void>::call();
                if(old_x) {
                    BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
                    ++old_checks;
                }
            })
        ;
        ++x;
    }
    BOOST_CONTRACT_OVERRIDE(next)
};

template<typename T>
struct a
    #define BASES public b<T>
    : BASES
{
    typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
    #undef BASES

    virtual void next(T& x, boost::contract::virtual_* v = 0) /* override */ {
        BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(
            typename boost::contract::test::detail::unprotected_commas<T, void,
                    void>::type1
        )(
            (boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(v)),
            old_x,
            (boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(x))
        );
        BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(
            typename boost::contract::test::detail::unprotected_commas<
                    override_next, void, void>::type1
        )(
            boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(v),
            &a::next,
            boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(this),
            boost::contract::test::detail::unprotected_commas<void, void,
                    void>::same(x)
        )
            BOOST_CONTRACT_POSTCONDITION([&] {
                boost::contract::test::detail::unprotected_commas<
                        void, void, void>::call();
                if(old_x) {
                    BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
                    ++old_checks;
                }
            })
        ;
        ++x;
    }
    BOOST_CONTRACT_OVERRIDE(next)
};

template<typename T>
void next(T& x) {
    BOOST_CONTRACT_OLD_PTR_IF_COPYABLE(
        typename boost::contract::test::detail::unprotected_commas<T, void,
                void>::type1
    )(
        old_x,
        (boost::contract::test::detail::unprotected_commas<void, void,
                void>::same(x))
    );
    BOOST_CONTRACT_FUNCTION()
        BOOST_CONTRACT_POSTCONDITION([&] {
            boost::contract::test::detail::unprotected_commas<void, void, void>
                    ::call();
            if(old_x) {
                BOOST_CONTRACT_ASSERT(x == *old_x + T(1));
                ++old_checks;
            }
        })
    ;
    ++x;
}
    
int main() {
    int i = 1; // Test built-in copyable type.
    cp c(1); // Test custom copyable type.
    ncp n(1); // Test non-copyable type.

    // Test free functions (old values without `v`).

    unsigned cnt =
        #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
            1
        #else
            0
        #endif
    ;

    old_checks = 0;
    next(i);
    BOOST_TEST_EQ(old_checks, cnt);
    
    old_checks = 0;
    next(c);
    BOOST_TEST_EQ(old_checks, cnt);

    old_checks = 0;
    next(n);
    BOOST_TEST_EQ(old_checks, 0u);

    // Test virtual functions (old values with `v`).
    
    cnt =
        #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
            2
        #else
            0
        #endif
    ;

    a<int> ai;
    old_checks = 0;
    ai.next(i);
    BOOST_TEST_EQ(old_checks, cnt);
    
    a<cp> ac;
    old_checks = 0;
    ac.next(c);
    BOOST_TEST_EQ(old_checks, cnt);

    a<ncp> an;
    old_checks = 0;
    an.next(n);
    BOOST_TEST_EQ(old_checks, 0u);

    return boost::report_errors();
}