blob: 146de0fa72cbf075abf456b657c048c3c9c40236 (
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
|
// 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>
//[static_public
template<class C>
class make {
public:
static void static_invariant() { // Static class invariants.
BOOST_CONTRACT_ASSERT(instances() >= 0);
}
// Contract for a static public function.
static int instances() {
// Explicit template parameter `make` (check static invariants).
boost::contract::check c = boost::contract::public_function<make>();
return instances_; // Function body.
}
/* ... */
//]
make() : object() {
boost::contract::old_ptr<int> old_instances =
BOOST_CONTRACT_OLDOF(instances());
boost::contract::check c = boost::contract::constructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(instances() == *old_instances + 1);
})
;
++instances_;
}
~make() {
boost::contract::old_ptr<int> old_instances =
BOOST_CONTRACT_OLDOF(instances());
boost::contract::check c = boost::contract::destructor(this)
.postcondition([&] { // (An example of destructor postconditions.)
BOOST_CONTRACT_ASSERT(instances() == *old_instances - 1);
})
;
--instances_;
}
C object;
private:
static int instances_;
};
template<class C>
int make<C>::instances_ = 0;
int main() {
struct x {};
make<x> x1, x2, x3;
assert(make<x>::instances() == 3);
return 0;
}
|