123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #ifndef BOOST_CONTRACT_DETAIL_COND_BASE_HPP_
- #define BOOST_CONTRACT_DETAIL_COND_BASE_HPP_
- #include <boost/contract/core/exception.hpp>
- #include <boost/contract/core/config.hpp>
- #if !defined(BOOST_CONTRACT_NO_PRECONDITIONS) || \
- !defined(BOOST_CONTRACT_NO_OLDS) || \
- !defined(BOOST_CONTRACT_NO_EXEPTS)
- #include <boost/function.hpp>
- #endif
- #include <boost/noncopyable.hpp>
- #ifndef BOOST_CONTRACT_ON_MISSING_CHECK_DECL
- #include <boost/assert.hpp>
- #endif
- #include <boost/config.hpp>
- namespace boost { namespace contract { namespace detail {
- class cond_base :
- private boost::noncopyable
- {
- public:
- explicit cond_base(boost::contract::from from) :
- BOOST_CONTRACT_ERROR_missing_check_object_declaration(false)
- , init_asserted_(false)
- #ifndef BOOST_CONTRACT_NO_CONDITIONS
- , from_(from)
- , failed_(false)
- #endif
- {}
-
-
- virtual ~cond_base() BOOST_NOEXCEPT_IF(false) {
-
- if(!init_asserted_) assert_initialized();
- }
- void initialize() {
- BOOST_CONTRACT_ERROR_missing_check_object_declaration = true;
- this->init();
- }
-
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- template<typename F>
- void set_pre(F const& f) { pre_ = f; }
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- template<typename F>
- void set_old(F const& f) { old_ = f; }
- #endif
- #ifndef BOOST_CONTRACT_NO_EXCEPTS
- template<typename F>
- void set_except(F const& f) { except_ = f; }
- #endif
- protected:
- void assert_initialized() {
- init_asserted_ = true;
- #ifdef BOOST_CONTRACT_ON_MISSING_CHECK_DECL
- if(!BOOST_CONTRACT_ERROR_missing_check_object_declaration) {
- BOOST_CONTRACT_ON_MISSING_CHECK_DECL;
- }
- #else
-
-
- BOOST_ASSERT(BOOST_CONTRACT_ERROR_missing_check_object_declaration);
- #endif
- }
-
- virtual void init() {}
-
-
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- bool check_pre(bool throw_on_failure = false) {
- if(failed()) return true;
- try { if(pre_) pre_(); else return false; }
- catch(...) {
-
-
- if(throw_on_failure) throw;
- fail(&boost::contract::precondition_failure);
- }
- return true;
- }
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- void copy_old() {
- if(failed()) return;
- try { if(old_) old_(); }
- catch(...) { fail(&boost::contract::old_failure); }
- }
- #endif
- #ifndef BOOST_CONTRACT_NO_EXCEPTS
- void check_except() {
- if(failed()) return;
- try { if(except_) except_(); }
- catch(...) { fail(&boost::contract::except_failure); }
- }
- #endif
-
- #ifndef BOOST_CONTRACT_NO_CONDITIONS
- void fail(void (*h)(boost::contract::from)) {
- failed(true);
- if(h) h(from_);
- }
-
-
- virtual bool failed() const { return failed_; }
- virtual void failed(bool value) { failed_ = value; }
- #endif
- private:
- bool BOOST_CONTRACT_ERROR_missing_check_object_declaration;
- bool init_asserted_;
- #ifndef BOOST_CONTRACT_NO_CONDITIONS
- boost::contract::from from_;
- bool failed_;
- #endif
-
- #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
- boost::function<void ()> pre_;
- #endif
- #ifndef BOOST_CONTRACT_NO_OLDS
- boost::function<void ()> old_;
- #endif
- #ifndef BOOST_CONTRACT_NO_EXCEPTS
- boost::function<void ()> except_;
- #endif
- };
- } } }
- #endif
|