renamed ..._member to ..._function, continuing to program more examples

This commit is contained in:
Lorenzo Caminiti
2015-06-09 18:50:31 -07:00
parent 0445983d4e
commit 7c9db6c58f
67 changed files with 2012 additions and 454 deletions

88
example/cline90/stack.cpp Normal file
View File

@@ -0,0 +1,88 @@
#include <boost/contract.hpp>
#include <boost/bind.hpp>
#include <boost/detail/lightweight_test.hpp>
template<typename T>
class stack
#define BASES private boost::contract::constructor_precondition<stack<T> >
: BASES
{
public:
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
// NOTE: Incomplete contract assertions, addressing only `empty` and `full`.
static void stack_precondition(int const& capacity) {
BOOST_CONTRACT_ASSERT(capacity >= 0);
}
explicit stack(int capacity) :
boost::contract::constructor_precondition<stack>(boost::bind(
&stack::stack_precondition, boost::cref(capacity))),
data_(new T[capacity]), capacity_(capacity), size_(0)
{
auto c = boost::contract::constructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(this->empty());
BOOST_CONTRACT_ASSERT(full() == (capacity == 0));
})
;
for(int i = 0; i < capacity_; ++i) data_[i] = T();
}
virtual ~stack() {
auto c = boost::contract::destructor(this);
delete[] data_;
}
bool empty() const {
auto c = boost::contract::public_function(this);
return size_ == 0;
}
bool full() const {
auto c = boost::contract::public_function(this);
return size_ == capacity_;
}
void push(T const& value) {
auto c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(!full());
})
.postcondition([&] {
BOOST_CONTRACT_ASSERT(!empty());
})
;
data_[size_++] = value;
}
T pop() {
auto c = boost::contract::public_function(this)
.precondition([&] {
BOOST_CONTRACT_ASSERT(!empty());
})
.postcondition([&] {
BOOST_CONTRACT_ASSERT(!full());
})
;
return data_[--size_];
}
private:
T* data_;
int capacity_;
int size_;
};
int main() {
stack<int> s(3);
s.push(123);
BOOST_TEST_EQ(s.pop(), 123);
return boost::report_errors();
}