mirror of
https://github.com/boostorg/contract.git
synced 2026-02-26 16:42:19 +00:00
replaced free/private/protected_function with just function and started to program n1962 examples
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
|
||||
#include "vector.hpp"
|
||||
#include <boost/contract.hpp>
|
||||
#incldue <boost/detail/lightweight_test.hpp>
|
||||
#include <boost/optional.hpp>
|
||||
#include <boost/detail/lightweight_test.hpp>
|
||||
#include <cassert>
|
||||
|
||||
template<typename T>
|
||||
class abstract_stack {
|
||||
@@ -10,9 +12,8 @@ public:
|
||||
|
||||
abstract_stack() {
|
||||
auto c = boost::contract::constructor(this)
|
||||
.postcondition([&] {
|
||||
BOOST_CONTRACT_ASSERT(empty());
|
||||
})
|
||||
// Postcondition: empty() (but it cannot be checked here to avoid
|
||||
// calling pure virtual function length() during construction).
|
||||
;
|
||||
}
|
||||
|
||||
@@ -41,38 +42,46 @@ public:
|
||||
}
|
||||
|
||||
virtual int length(boost::contract::virtual_* v = 0) const = 0;
|
||||
|
||||
virtual int capacity(boost::contract::virtual_* v = 0) const = 0;
|
||||
|
||||
virtual void push(T const& value, boost::contract::virtual_* v = 0) = 0;
|
||||
virtual T const& item(boost::contract::virtual_* v = 0) const = 0;
|
||||
|
||||
virtual T pop(boost::contract::virtual_* v = 0) = 0;
|
||||
virtual void push(T const& value, boost::contract::virtual_* v = 0) = 0;
|
||||
virtual T const& pop(boost::contract::virtual_* v = 0) = 0;
|
||||
|
||||
virtual void clear(boost::contract::virtual_* v = 0) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
int abstract_stack<T>::length(boost::contract::virtual_* v) const {
|
||||
auto c = boost::contract::public_function(v, this)
|
||||
int result;
|
||||
auto c = boost::contract::public_function(v, result, this)
|
||||
.postcondition([&] (int const& result) {
|
||||
BOOST_CONTRACT_ASSERT(result >= 0);
|
||||
})
|
||||
;
|
||||
|
||||
throw std::logic_error();
|
||||
return -1;
|
||||
assert(false); return result;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
int abstract_stack<T>::capacity(boost::contract::virtual_* v) const {
|
||||
auto c = boost::contract::public_function(v, this)
|
||||
int result;
|
||||
auto c = boost::contract::public_function(v, result, this)
|
||||
.postcondition([&] (int const& result) {
|
||||
BOOST_CONTRACT_ASSERT(result >= 0);
|
||||
})
|
||||
;
|
||||
assert(false); return result;
|
||||
}
|
||||
|
||||
throw std::logic_error();
|
||||
return -1;
|
||||
template<typename T>
|
||||
T const& abstract_stack<T>::item(boost::contract::virtual_* v) const {
|
||||
auto c = boost::contract::public_function(v, this)
|
||||
.precondition([&] {
|
||||
BOOST_CONTRACT_ASSERT(!empty());
|
||||
})
|
||||
;
|
||||
assert(false); return *boost::optional<T const&>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -85,33 +94,33 @@ void abstract_stack<T>::push(T const& value, boost::contract::virtual_* v) {
|
||||
BOOST_CONTRACT_ASSERT(!empty());
|
||||
})
|
||||
;
|
||||
|
||||
throw std::logic_error();
|
||||
assert(false);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void abstract_stack<T>::pop(boost::contract::virtual_* v) {
|
||||
auto c = boost::contract::public_function(v, this)
|
||||
T const& abstract_stack<T>::pop(boost::contract::virtual_* v) {
|
||||
boost::optional<T const&> result;
|
||||
auto old_item = BOOST_CONTRACT_OLDOF(v, item());
|
||||
auto c = boost::contract::public_function(v, result, this)
|
||||
.precondition([&] {
|
||||
BOOST_CONTRACT_ASSERT(!empty());
|
||||
})
|
||||
.postcondition([&] {
|
||||
.postcondition([&] (boost::optional<T const&> const& result) {
|
||||
BOOST_CONTRACT_ASSERT(!full());
|
||||
BOOST_CONTRACT_ASSERT(*result == *old_item);
|
||||
})
|
||||
;
|
||||
|
||||
throw std::logic_error();
|
||||
assert(false); return *boost::optional<T const&>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void abstract_stack<T>::clear(boost::contract::virtual_* v) {
|
||||
auto c = boost::contract::public_function(v, this)
|
||||
.postcondition([&] {
|
||||
BOOST_CONTRACT_ASSERT(empty());
|
||||
BOOST_CONTRACT_ASSERT(this->empty());
|
||||
})
|
||||
;
|
||||
|
||||
throw std::logic_error();
|
||||
assert(false);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -129,13 +138,13 @@ public:
|
||||
BOOST_CONTRACT_ASSERT(length() < capacity());
|
||||
}
|
||||
|
||||
static vstack_precondition(int const& count) {
|
||||
static void vstack_precondition(int const& count) {
|
||||
BOOST_CONTRACT_ASSERT(count >= 0);
|
||||
}
|
||||
explicit vstack(int count = 10) :
|
||||
boost::contract::constructor_precondition<vstack>(boost::bind(
|
||||
&vstack::vstack_precondition, boost::cref(count))),
|
||||
v_(count), // OK, executed after precondition so count >= 0.
|
||||
vect_(count), // OK, executed after precondition so count >= 0.
|
||||
len_(0)
|
||||
{
|
||||
auto c = boost::contract::constructor(this)
|
||||
@@ -146,36 +155,49 @@ public:
|
||||
;
|
||||
}
|
||||
|
||||
virtual ~vstack() { auto c = boost::contract::destrucotr(this); }
|
||||
virtual ~vstack() { auto c = boost::contract::destructor(this); }
|
||||
|
||||
// Inherited from abstract_stack.
|
||||
|
||||
virtual int length(boost::contract::virtual_* v = 0) const /* override */ {
|
||||
int result;
|
||||
auto c = boost::contract::public_function<override_length>(
|
||||
v, &vstack::length, this);
|
||||
return len_;
|
||||
v, result, &vstack::length, this);
|
||||
return result = len_;
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(length)
|
||||
|
||||
virtual int capacity() const /* override */ {
|
||||
virtual int capacity(boost::contract::virtual_* v = 0)
|
||||
const /* override */ {
|
||||
int result;
|
||||
auto c = boost::contract::public_function<override_capacity>(
|
||||
v, &vstack::capacity, this);
|
||||
return v_.size();
|
||||
v, result, &vstack::capacity, this);
|
||||
return result = vect_.size();
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(capacity)
|
||||
|
||||
virtual T const& item(boost::contract::virtual_* v = 0)
|
||||
const /* override */ {
|
||||
boost::optional<T const&> result;
|
||||
auto c = boost::contract::public_function<override_item>(
|
||||
v, result, &vstack::item, this);
|
||||
return *(result = vect_[len_ - 1]);
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(item)
|
||||
|
||||
virtual void push(T const& value, boost::contract::virtual_* v = 0)
|
||||
/* override */ {
|
||||
auto c = boost::contract::public_function<override_push>(
|
||||
v, &vstack::push, this, value);
|
||||
v_[len_++] = value;
|
||||
vect_[len_++] = value;
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(push)
|
||||
|
||||
virtual T pop(boost::contract::virtual_* v = 0) /* override */ {
|
||||
virtual T const& pop(boost::contract::virtual_* v = 0) /* override */ {
|
||||
boost::optional<T const&> result;
|
||||
auto c = boost::contract::public_function<override_pop>(
|
||||
v, &vstack::pop, this);
|
||||
return v_[--len_];
|
||||
v, result, &vstack::pop, this);
|
||||
return *(result = vect_[--len_]);
|
||||
}
|
||||
BOOST_CONTRACT_OVERRIDE(pop)
|
||||
|
||||
@@ -187,7 +209,7 @@ public:
|
||||
BOOST_CONTRACT_OVERRIDE(clear);
|
||||
|
||||
private:
|
||||
vector<T> v_;
|
||||
vector<T> vect_;
|
||||
int len_;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user