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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
// 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 <vector>
#include <limits>
#include <cassert>
//[ifdef_function
// Use #ifdef to completely disable contract code compilation.
#include <boost/contract/core/config.hpp>
#ifndef BOOST_CONTRACT_NO_ALL
#include <boost/contract.hpp>
#endif
int inc(int& x) {
int result;
#ifndef BOOST_CONTRACT_NO_OLDS
boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
#endif
#ifndef BOOST_CONTRACT_NO_FUNCTIONS
boost::contract::check c = boost::contract::function()
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
.precondition([&] {
BOOST_CONTRACT_ASSERT(x < std::numeric_limits<int>::max());
})
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
.postcondition([&] {
BOOST_CONTRACT_ASSERT(x == *old_x + 1);
BOOST_CONTRACT_ASSERT(result == *old_x);
})
#endif
;
#endif
return result = x++;
}
//]
template<typename T>
class pushable {
#ifndef BOOST_CONTRACT_NO_ALL
friend class boost::contract::access;
#endif
#ifndef BOOST_CONTRACT_NO_INVARIANTS
void invariant() const {
BOOST_CONTRACT_ASSERT(capacity() <= max_size());
}
#endif
public:
virtual void push_back(
T const& x
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
, boost::contract::virtual_* v = 0
#endif
) = 0;
protected:
virtual unsigned capacity() const = 0;
virtual unsigned max_size() const = 0;
};
template<typename T>
void pushable<T>::push_back(
T const& x
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
, boost::contract::virtual_* v
#endif
) {
#ifndef BOOST_CONTRACT_NO_OLDS
boost::contract::old_ptr<unsigned> old_capacity =
BOOST_CONTRACT_OLDOF(v, capacity());
#endif
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
boost::contract::check c = boost::contract::public_function(v, this)
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
.precondition([&] {
BOOST_CONTRACT_ASSERT(capacity() < max_size());
})
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
.postcondition([&] {
BOOST_CONTRACT_ASSERT(capacity() >= *old_capacity);
})
#endif
;
#endif
assert(false); // Shall never execute this body.
}
//[ifdef_class
class integers
#define BASES public pushable<int>
:
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
private boost::contract::constructor_precondition<integers>, BASES
#else
BASES
#endif
{
#ifndef BOOST_CONTRACT_NO_ALL
friend class boost::contract::access;
#endif
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#endif
#undef BASES
#ifndef BOOST_CONTRACT_NO_INVARIANTS
void invariant() const {
BOOST_CONTRACT_ASSERT(size() <= capacity());
}
#endif
public:
integers(int from, int to) :
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
boost::contract::constructor_precondition<integers>([&] {
BOOST_CONTRACT_ASSERT(from <= to);
}),
#endif
vect_(to - from + 1)
{
#ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
boost::contract::check c = boost::contract::constructor(this)
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
.postcondition([&] {
BOOST_CONTRACT_ASSERT(int(size()) == (to - from + 1));
})
#endif
;
#endif
for(int x = from; x <= to; ++x) vect_.at(x - from) = x;
}
virtual ~integers() {
#ifndef BOOST_CONTRACT_NO_DESTRUCTORS
// Check invariants.
boost::contract::check c = boost::contract::destructor(this);
#endif
}
virtual void push_back(
int const& x
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
, boost::contract::virtual_* v = 0
#endif
) /* override */ {
#ifndef BOOST_CONTRACT_NO_OLDS
boost::contract::old_ptr<unsigned> old_size;
#endif
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
boost::contract::check c = boost::contract::public_function<
override_push_back>(v, &integers::push_back, this, x)
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
.precondition([&] {
BOOST_CONTRACT_ASSERT(size() < max_size());
})
#endif
#ifndef BOOST_CONTRACT_NO_OLDS
.old([&] {
old_size = BOOST_CONTRACT_OLDOF(v, size());
})
#endif
#ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
.postcondition([&] {
BOOST_CONTRACT_ASSERT(size() == *old_size + 1);
})
#endif
#ifndef BOOST_CONTRACT_NO_EXCEPTS
.except([&] {
BOOST_CONTRACT_ASSERT(size() == *old_size);
})
#endif
;
#endif
vect_.push_back(x);
}
private:
#ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
BOOST_CONTRACT_OVERRIDE(push_back)
#endif
/* ... */
//]
public: // Could program contracts for these too...
unsigned size() const { return vect_.size(); }
unsigned max_size() const { return vect_.max_size(); }
unsigned capacity() const { return vect_.capacity(); }
private:
std::vector<int> vect_;
};
int main() {
integers i(1, 10);
int x = 123;
i.push_back(inc(x));
assert(x == 124);
assert(i.size() == 11);
return 0;
}
|