Files
contract/example/features/throw_on_failure.cpp

115 lines
3.3 KiB
C++

// Copyright (C) 2008-2016 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 <boost/contract.hpp>
#include <iostream>
#include <cstring>
#include <cassert>
//[throw_on_failure_cstring
struct too_large_error {};
template<std::size_t MaxSize>
class cstring
#define BASES private boost::contract::constructor_precondition<cstring< \
MaxSize> >
: BASES
{
public:
typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
#undef BASES
/* implicit */ cstring(char const* chars) :
boost::contract::constructor_precondition<cstring>([&] {
BOOST_CONTRACT_ASSERT(chars); // Throw `assertion_failure`.
// Or, throw user-defined exception.
if(std::strlen(chars) > MaxSize) throw too_large_error();
})
{
/* ... */
//]
boost::contract::check c = boost::contract::constructor(this)
.postcondition([&] {
BOOST_CONTRACT_ASSERT(size() == std::strlen(chars));
})
;
size_ = std::strlen(chars);
for(std::size_t i = 0; i < size_; ++i) chars_[i] = chars[i];
chars_[size_] = '\0';
}
~cstring() {
// Check invariants.
boost::contract::check c = boost::contract::destructor(this);
}
std::size_t size() const {
// Check invariants.
boost::contract::check c = boost::contract::public_function(this);
return size_;
}
void invariant() const {
if(size() > MaxSize) throw too_large_error(); // Throw user-defined ex.
BOOST_CONTRACT_ASSERT(chars_); // Or, throw `assertion_failure`.
BOOST_CONTRACT_ASSERT(chars_[size()] == '\0');
}
private:
char chars_[MaxSize + 1];
std::size_t size_;
};
//[throw_on_failure_handler
int main() {
boost::contract::set_specification_failure(
[] (boost::contract::from context) {
if(context == boost::contract::from_destructor) {
// Ignore exception because destructors should never throw.
std::clog << "destructor contract failed (ignored)" <<
std::endl;
} else throw; // Rethrow (assertion_failure, too_large_error, etc.).
}
);
boost::contract::set_check_failure( // Then do not use CHECK in destructors.
[] {
throw; // Rethrow (assertion_failure, too_large_error, etc.).
}
);
/* ... */
//]
{
cstring<3> s("abc");
assert(s.size() == 3);
}
#ifndef BOOST_CONTRACT_NO_PRECONDITIONS
// These failures properly handled only when preconditions checked.
try {
char* c = 0;
cstring<3> s(c);
assert(false);
} catch(boost::contract::assertion_failure const& error) {
// OK (expected).
std::clog << "ignored: " << error.what() << std::endl;
} catch(...) { assert(false); }
try {
cstring<3> s("abcd");
assert(false);
} catch(too_large_error const&) {} // OK (expected).
catch(...) { assert(false); }
#endif
return 0;
}