finished example section. recompiled all tests and examples.

This commit is contained in:
Lorenzo Caminiti
2016-05-12 08:22:34 -07:00
parent 0c8a75df83
commit 1aa41fd14e
48 changed files with 1812 additions and 1593 deletions

View File

@@ -1,57 +1,73 @@
//[n1962_circle
#include <boost/contract.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cassert>
class shape {
public:
virtual ~shape() {}
virtual int compute_area(boost::contract::virtual_* v = 0) const = 0;
virtual unsigned compute_area(boost::contract::virtual_* v = 0) const = 0;
};
int shape::compute_area(boost::contract::virtual_* v) const {
int result;
auto c = boost::contract::public_function(v, result, this)
unsigned shape::compute_area(boost::contract::virtual_* v) const {
unsigned result;
boost::contract::guard c = boost::contract::public_function(v, result, this)
.postcondition([&] (int const& result) {
BOOST_CONTRACT_ASSERT(result > 0);
})
;
assert(false); return result = -1; // Never gets here.
assert(false);
return result = 0;
}
class circle
#define BASES public shape
: BASES
{
public:
friend class boost::contract::access;
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
BOOST_CONTRACT_OVERRIDE(compute_area);
int radius; // Make sure to set this...
public:
static int const pi = 3; // Truncated to int from 3.14...
virtual int compute_area(boost::contract::virtual_* v = 0)
const /* override */ {
int result;
auto c = boost::contract::public_function<override_compute_area>(
v, result, &circle::compute_area, this)
.postcondition([&] (int const& result) {
BOOST_CONTRACT_ASSERT(result == pi * radius * radius);
explicit circle(unsigned a_radius) : radius_(a_radius) {
boost::contract::guard c = boost::contract::constructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(radius() == a_radius);
})
;
}
virtual unsigned compute_area(boost::contract::virtual_* v = 0) const
/* override */ {
unsigned result;
boost::contract::guard c = boost::contract::public_function<
override_compute_area>(v, result, &circle::compute_area, this)
.postcondition([&] (unsigned const& result) {
BOOST_CONTRACT_ASSERT(result == pi * radius() * radius());
})
;
return result = pi * radius * radius;
return result = pi * radius() * radius();
}
BOOST_CONTRACT_OVERRIDE(compute_area);
unsigned radius() const {
boost::contract::guard c = boost::contract::public_function(this);
return radius_;
}
private:
unsigned radius_;
};
int main() {
circle c;
c.radius = 2;
BOOST_TEST_EQ(c.compute_area(), 12);
return boost::report_errors();
circle c(2);
assert(c.radius() == 2);
assert(c.compute_area() == 12);
return 0;
}
//]