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
|
// 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 <boost/config.hpp>
#include <cassert>
#ifdef BOOST_GCC // G++ does not support static union members yet.
int instances_ = 0;
#endif
//[union
union positive {
public:
static void static_invariant() { // Static class invariants (as usual).
BOOST_CONTRACT_ASSERT(instances() >= 0);
}
void invariant() const { // Class invariants (as usual).
BOOST_CONTRACT_ASSERT(i_ > 0);
BOOST_CONTRACT_ASSERT(d_ > 0);
}
// Contracts for constructor, as usual but...
explicit positive(int x) : d_(0) {
// ...unions cannot have bases so constructor preconditions here.
boost::contract::constructor_precondition<positive> pre([&] {
BOOST_CONTRACT_ASSERT(x > 0);
});
boost::contract::old_ptr<int> old_instances =
BOOST_CONTRACT_OLDOF(instances());
boost::contract::check c = boost::contract::constructor(this)
.postcondition([&] {
{ int y; get(y); BOOST_CONTRACT_ASSERT(y == x); }
BOOST_CONTRACT_ASSERT(instances() == *old_instances + 1);
})
;
i_ = x;
++instances_;
}
// Contracts for destructor (as usual).
~positive() {
boost::contract::old_ptr<int> old_instances =
BOOST_CONTRACT_OLDOF(instances());
boost::contract::check c = boost::contract::destructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(instances() == *old_instances - 1);
})
;
--instances_;
}
// Contracts for public function (as usual, but no virtual or override).
void get(int& x) const {
boost::contract::check c = boost::contract::public_function(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(x > 0);
})
;
x = i_;
}
// Contracts for static public function (as usual).
static int instances() {
boost::contract::check c = boost::contract::public_function<positive>();
return instances_;
}
private:
int i_;
double d_;
/* ... */
//]
public:
explicit positive(double x) : d_(0) {
// Unions cannot have bases so constructor preconditions here.
boost::contract::constructor_precondition<positive> pre([&] {
BOOST_CONTRACT_ASSERT(x > 0);
});
boost::contract::old_ptr<int> old_instances =
BOOST_CONTRACT_OLDOF(instances());
boost::contract::check c = boost::contract::constructor(this)
.postcondition([&] {
{ double y; get(y); BOOST_CONTRACT_ASSERT(y == x); }
BOOST_CONTRACT_ASSERT(instances() == *old_instances + 1);
})
;
d_ = x;
++instances_;
}
void get(double& x) const {
boost::contract::check c = boost::contract::public_function(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(x > 0);
})
;
x = d_;
}
#ifndef BOOST_GCC // G++ does not support static union members yet.
static int instances_;
#endif
};
#ifndef BOOST_GCC // G++ does not support static union members yet.
int positive::instances_ = 0;
#endif
int main() {
{
positive p(123);
assert(p.instances() == 1);
{ int y = -456; p.get(y); assert(y == 123); }
positive q(1.23);
assert(q.instances() == 2);
{ double y = -4.56; q.get(y); assert(y == 1.23); }
}
assert(positive::instances() == 0);
return 0;
}
|