mirror of
https://github.com/boostorg/contract.git
synced 2026-02-26 16:42:19 +00:00
renamed ..._member to ..._function, continuing to program more examples
This commit is contained in:
88
example/cline90/stack.cpp
Normal file
88
example/cline90/stack.cpp
Normal 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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user