123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828 |
- #ifndef BOOST_CONTRACT_OLD_HPP_
- #define BOOST_CONTRACT_OLD_HPP_
- #include <boost/contract/core/config.hpp>
- #include <boost/contract/core/virtual.hpp>
- #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
- #include <boost/contract/detail/checking.hpp>
- #endif
- #include <boost/contract/detail/operator_safe_bool.hpp>
- #include <boost/contract/detail/declspec.hpp>
- #include <boost/contract/detail/debug.hpp>
- #include <boost/make_shared.hpp>
- #include <boost/shared_ptr.hpp>
- #include <boost/type_traits/is_copy_constructible.hpp>
- #include <boost/utility/enable_if.hpp>
- #include <boost/static_assert.hpp>
- #include <boost/preprocessor/control/expr_iif.hpp>
- #include <boost/preprocessor/config/config.hpp>
- #include <queue>
- #if !BOOST_PP_VARIADICS
- #define BOOST_CONTRACT_OLDOF \
- BOOST_CONTRACT_ERROR_macro_OLDOF_requires_variadic_macros_otherwise_manually_program_old_values
- #else
- #include <boost/preprocessor/facilities/overload.hpp>
- #include <boost/preprocessor/facilities/empty.hpp>
- #include <boost/preprocessor/cat.hpp>
- #include <boost/config.hpp>
- #ifdef BOOST_NO_CXX11_AUTO_DECLARATIONS
- #define BOOST_CONTRACT_OLDOF_AUTO_TYPEOF_(value)
- #else
- #include <boost/typeof/typeof.hpp>
-
- #define BOOST_CONTRACT_OLDOF_AUTO_TYPEOF_(value) \
- boost::contract::old_ptr<BOOST_TYPEOF(value)>
- #endif
- #define BOOST_CONTRACT_ERROR_macro_OLDOF_has_invalid_number_of_arguments_2( \
- v, value) \
- BOOST_CONTRACT_OLDOF_AUTO_TYPEOF_(value)(boost::contract::make_old(v, \
- boost::contract::copy_old(v) ? (value) : boost::contract::null_old() \
- ))
- #define BOOST_CONTRACT_ERROR_macro_OLDOF_has_invalid_number_of_arguments_1( \
- value) \
- BOOST_CONTRACT_OLDOF_AUTO_TYPEOF_(value)(boost::contract::make_old( \
- boost::contract::copy_old() ? (value) : boost::contract::null_old() \
- ))
- #define BOOST_CONTRACT_OLDOF(...) \
- BOOST_PP_CAT( \
- BOOST_PP_OVERLOAD( \
- BOOST_CONTRACT_ERROR_macro_OLDOF_has_invalid_number_of_arguments_, \
- __VA_ARGS__ \
- )(__VA_ARGS__), \
- BOOST_PP_EMPTY() \
- )
- #endif
- namespace boost { namespace contract {
- template<typename T>
- struct is_old_value_copyable : boost::is_copy_constructible<T> {};
- class old_value;
- template<>
- struct is_old_value_copyable<old_value> : boost::true_type {};
- template<typename T>
- struct old_value_copy {
-
- explicit old_value_copy(T const& old) :
- old_(old) {}
-
- T const& old() const { return old_; }
- private:
- T const old_;
- };
- template<typename T>
- class old_ptr_if_copyable;
- template<typename T>
- class old_ptr {
- public:
-
- typedef T element_type;
-
- old_ptr() {}
-
- T const& operator*() const {
- BOOST_STATIC_ASSERT_MSG(
- boost::contract::is_old_value_copyable<T>::value,
- "old_ptr<T> requires T copyable (see is_old_value_copyable<T>), "
- "otherwise use old_ptr_if_copyable<T>"
- );
- BOOST_CONTRACT_DETAIL_DEBUG(typed_copy_);
- return typed_copy_->old();
- }
-
- T const* operator->() const {
- BOOST_STATIC_ASSERT_MSG(
- boost::contract::is_old_value_copyable<T>::value,
- "old_ptr<T> requires T copyble (see is_old_value_copyable<T>), "
- "otherwise use old_ptr_if_copyable<T>"
- );
- if(typed_copy_) return &typed_copy_->old();
- return 0;
- }
- #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
- BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(old_ptr<T>,
- !!typed_copy_)
- #else
-
- explicit operator bool() const;
- #endif
- private:
- #ifndef BOOST_CONTRACT_NO_OLDS
- explicit old_ptr(boost::shared_ptr<old_value_copy<T> > old)
- : typed_copy_(old) {}
- #endif
- boost::shared_ptr<old_value_copy<T> > typed_copy_;
- friend class old_pointer;
- friend class old_ptr_if_copyable<T>;
- };
- template<typename T>
- class old_ptr_if_copyable {
- public:
-
- typedef T element_type;
-
- old_ptr_if_copyable() {}
-
- old_ptr_if_copyable(old_ptr<T> const& other) :
- typed_copy_(other.typed_copy_) {}
-
- T const& operator*() const {
- BOOST_CONTRACT_DETAIL_DEBUG(typed_copy_);
- return typed_copy_->old();
- }
-
- T const* operator->() const {
- if(typed_copy_) return &typed_copy_->old();
- return 0;
- }
- #ifndef BOOST_CONTRACT_DETAIL_DOXYGEN
- BOOST_CONTRACT_DETAIL_OPERATOR_SAFE_BOOL(old_ptr_if_copyable<T>,
- !!typed_copy_)
- #else
-
- explicit operator bool() const;
- #endif
- private:
- #ifndef BOOST_CONTRACT_NO_OLDS
- explicit old_ptr_if_copyable(boost::shared_ptr<old_value_copy<T> > old)
- : typed_copy_(old) {}
- #endif
- boost::shared_ptr<old_value_copy<T> > typed_copy_;
- friend class old_pointer;
- };
- class old_value {
- public:
-
-
- template<typename T>
- old_value(
- T const&
- #if !defined(BOOST_CONTRACT_NO_OLDS) || \
- defined(BOOST_CONTRACT_DETAIL_DOXYGEN)
- old
- #endif
- ,
- typename boost::enable_if<boost::contract::is_old_value_copyable<T>
- >::type* = 0
- )
- #ifndef BOOST_CONTRACT_NO_OLDS
- : untyped_copy_(new old_value_copy<T>(old))
- #endif
- {}
-
-
- template<typename T>
- old_value(
- T const&
- #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
- old
- #endif
- ,
- typename boost::disable_if<boost::contract::is_old_value_copyable<T>
- >::type* = 0
- ) {}
- private:
- explicit old_value() {}
-
- #ifndef BOOST_CONTRACT_NO_OLDS
- boost::shared_ptr<void> untyped_copy_;
- #endif
- friend class old_pointer;
- friend BOOST_CONTRACT_DETAIL_DECLSPEC old_value null_old();
- };
- class old_pointer {
- public:
-
- template<typename T>
- operator old_ptr_if_copyable<T>() {
- return get<old_ptr_if_copyable<T> >();
- }
-
-
- template<typename T>
- operator old_ptr<T>() {
- return get<old_ptr<T> >();
- }
- private:
- #ifndef BOOST_CONTRACT_NO_OLDS
- explicit old_pointer(virtual_* v, old_value const& old)
- : v_(v), untyped_copy_(old.untyped_copy_) {}
- #else
- explicit old_pointer(virtual_* , old_value const& ) {}
- #endif
-
- template<typename Ptr>
- Ptr get() {
- #ifndef BOOST_CONTRACT_NO_OLDS
- if(!boost::contract::is_old_value_copyable<typename
- Ptr::element_type>::value) {
- BOOST_CONTRACT_DETAIL_DEBUG(!untyped_copy_);
- return Ptr();
- #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
- } else if(!v_ && boost::contract::detail::checking::already()) {
- return Ptr();
- #endif
- } else if(!v_) {
- BOOST_CONTRACT_DETAIL_DEBUG(untyped_copy_);
- typedef old_value_copy<typename Ptr::element_type> copied_type;
- boost::shared_ptr<copied_type> typed_copy =
- boost::static_pointer_cast<copied_type>(untyped_copy_);
- BOOST_CONTRACT_DETAIL_DEBUG(typed_copy);
- return Ptr(typed_copy);
- } else if(
- v_->action_ == boost::contract::virtual_::push_old_init_copy ||
- v_->action_ == boost::contract::virtual_::push_old_ftor_copy
- ) {
- BOOST_CONTRACT_DETAIL_DEBUG(untyped_copy_);
- std::queue<boost::shared_ptr<void> >& copies = v_->action_ ==
- boost::contract::virtual_::push_old_ftor_copy ?
- v_->old_ftor_copies_
- :
- v_->old_init_copies_
- ;
- copies.push(untyped_copy_);
- return Ptr();
- } else if(
- boost::contract::virtual_::pop_old_init_copy(v_->action_) ||
- v_->action_ == boost::contract::virtual_::pop_old_ftor_copy
- ) {
-
- BOOST_CONTRACT_DETAIL_DEBUG(!untyped_copy_);
- std::queue<boost::shared_ptr<void> >& copies = v_->action_ ==
- boost::contract::virtual_::pop_old_ftor_copy ?
- v_->old_ftor_copies_
- :
- v_->old_init_copies_
- ;
- boost::shared_ptr<void> untyped_copy = copies.front();
- BOOST_CONTRACT_DETAIL_DEBUG(untyped_copy);
- copies.pop();
- typedef old_value_copy<typename Ptr::element_type> copied_type;
- boost::shared_ptr<copied_type> typed_copy =
- boost::static_pointer_cast<copied_type>(untyped_copy);
- BOOST_CONTRACT_DETAIL_DEBUG(typed_copy);
- return Ptr(typed_copy);
- }
- BOOST_CONTRACT_DETAIL_DEBUG(!untyped_copy_);
- #endif
- return Ptr();
- }
- #ifndef BOOST_CONTRACT_NO_OLDS
- virtual_* v_;
- boost::shared_ptr<void> untyped_copy_;
- #endif
-
- friend BOOST_CONTRACT_DETAIL_DECLSPEC
- old_pointer make_old(old_value const&);
- friend BOOST_CONTRACT_DETAIL_DECLSPEC
- old_pointer make_old(virtual_*, old_value const&);
- };
- BOOST_CONTRACT_DETAIL_DECLSPEC
- old_value null_old();
- BOOST_CONTRACT_DETAIL_DECLSPEC
- old_pointer make_old(old_value const& old);
- BOOST_CONTRACT_DETAIL_DECLSPEC
- old_pointer make_old(virtual_* v, old_value const& old);
- inline bool copy_old() {
- #ifndef BOOST_CONTRACT_NO_OLDS
- #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
- return !boost::contract::detail::checking::already();
- #else
- return true;
- #endif
- #else
- return false;
- #endif
- }
- #ifndef BOOST_CONTRACT_NO_OLDS
- inline bool copy_old(virtual_* v) {
- if(!v) {
- #ifndef BOOST_CONTRACT_ALL_DISABLE_NO_ASSERTION
- return !boost::contract::detail::checking::already();
- #else
- return true;
- #endif
- }
- return v->action_ == boost::contract::virtual_::push_old_init_copy ||
- v->action_ == boost::contract::virtual_::push_old_ftor_copy;
- }
- #else
- inline bool copy_old(virtual_*
- #ifdef BOOST_CONTRACT_DETAIL_DOXYGEN
- v
- #endif
- ) {
- return false;
- }
- #endif
- } }
- #ifdef BOOST_CONTRACT_HEADER_ONLY
- #include <boost/contract/detail/inlined/old.hpp>
- #endif
- #endif
|