summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/contract/example/features/volatile.cpp
blob: eb9b2e7101ddea4b2c3d20413f436af582e9bb6e (plain)
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
// 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

#include <boost/contract.hpp>
#include <cassert>

//[volatile
class u {
public:
    static void static_invariant();     // Static invariants.
    void invariant() const volatile;    // Volatile invariants.
    void invariant() const;             // Const invariants.

    u() { // Check static, volatile, and const invariants.
        boost::contract::check c= boost::contract::constructor(this);
    }

    ~u() { // Check static, volatile, and const invariants.
        boost::contract::check c = boost::contract::destructor(this);
    }

    void nc() { // Check static and const invariants.
        boost::contract::check c = boost::contract::public_function(this);
    }

    void c() const { // Check static and const invariants.
        boost::contract::check c = boost::contract::public_function(this);
    }
    
    void v() volatile { // Check static and volatile invariants.
        boost::contract::check c = boost::contract::public_function(this);
    }
    
    void cv() const volatile { // Check static and volatile invariants.
        boost::contract::check c = boost::contract::public_function(this);
    }

    static void s() { // Check static invariants only.
        boost::contract::check c = boost::contract::public_function<u>();
    }
};
//]

bool static_inv_checked, cv_inv_checked, const_inv_checked;
void u::static_invariant() { static_inv_checked = true; }
void u::invariant() const volatile { cv_inv_checked = true; }
void u::invariant() const { const_inv_checked = true; }

int main() {
    #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
        {
            static_inv_checked = cv_inv_checked = const_inv_checked = false;
            u x;
            assert(static_inv_checked);
            assert(cv_inv_checked);
            assert(const_inv_checked);

            static_inv_checked = cv_inv_checked = const_inv_checked = false;
            x.nc();
            assert(static_inv_checked);
            assert(!cv_inv_checked);
            assert(const_inv_checked);
            
            static_inv_checked = cv_inv_checked = const_inv_checked = false;
            x.c();
            assert(static_inv_checked);
            assert(!cv_inv_checked);
            assert(const_inv_checked);
            
            static_inv_checked = cv_inv_checked = const_inv_checked = false;
            x.v();
            assert(static_inv_checked);
            assert(cv_inv_checked);
            assert(!const_inv_checked);
            
            static_inv_checked = cv_inv_checked = const_inv_checked = false;
            x.cv();
            assert(static_inv_checked);
            assert(cv_inv_checked);
            assert(!const_inv_checked);
            
            static_inv_checked = cv_inv_checked = const_inv_checked = false;
            x.s();
            assert(static_inv_checked);
            assert(!cv_inv_checked);
            assert(!const_inv_checked);
        
            static_inv_checked = cv_inv_checked = const_inv_checked = false;
        } // Call destructor.
        #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
            assert(static_inv_checked);
            assert(cv_inv_checked);
            assert(const_inv_checked);
        #endif
    #endif

    return 0;
}