123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- #ifndef BOOST_SCOPE_SCOPE_SUCCESS_HPP_INCLUDED_
- #define BOOST_SCOPE_SCOPE_SUCCESS_HPP_INCLUDED_
- #include <type_traits>
- #include <boost/scope/detail/config.hpp>
- #include <boost/scope/exception_checker.hpp>
- #include <boost/scope/scope_exit.hpp>
- #include <boost/scope/detail/is_not_like.hpp>
- #include <boost/scope/detail/type_traits/conjunction.hpp>
- #include <boost/scope/detail/type_traits/is_invocable.hpp>
- #include <boost/scope/detail/type_traits/is_nothrow_invocable.hpp>
- #include <boost/scope/detail/header.hpp>
- #ifdef BOOST_HAS_PRAGMA_ONCE
- #pragma once
- #endif
- namespace boost {
- namespace scope {
- template< typename Func, typename Cond >
- class scope_success;
- namespace detail {
- template< typename T >
- using is_not_like_scope_success = detail::is_not_like< T, scope_success >;
- template< typename Func >
- class logical_not;
- template< typename T >
- using is_not_like_logical_not = detail::is_not_like< T, logical_not >;
- template< typename Func >
- class logical_not
- {
- public:
- using result_type = bool;
- private:
- Func m_func;
- public:
- template<
- bool Requires = std::is_default_constructible< Func >::value,
- typename = typename std::enable_if< Requires >::type
- >
- logical_not() noexcept(std::is_nothrow_default_constructible< Func >::value) :
- m_func()
- {
- }
- template<
- typename F,
- typename = typename std::enable_if< detail::conjunction<
- std::is_constructible< Func, F >,
- detail::is_not_like_logical_not< F >
- >::value >::type
- >
- explicit logical_not(F&& func) noexcept(std::is_nothrow_constructible< Func, F >::value) :
- m_func(static_cast< F&& >(func))
- {
- }
- result_type operator()() const noexcept(detail::is_nothrow_invocable< Func const& >::value)
- {
- return !m_func();
- }
- };
- }
- template< typename Func, typename Cond = exception_checker >
- class scope_success :
- public scope_exit< Func, detail::logical_not< Cond > >
- {
- private:
- using base_type = scope_exit< Func, detail::logical_not< Cond > >;
- public:
-
- template<
- typename F
- //! \cond
- , typename = typename std::enable_if< detail::conjunction<
- std::is_constructible< base_type, F, bool >,
- detail::is_not_like_scope_success< F >
- >::value >::type
-
- >
- explicit scope_success(F&& func, bool active = true)
- noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(std::is_nothrow_constructible< base_type, F, bool >::value)) :
- base_type(static_cast< F&& >(func), active)
- {
- }
-
- template<
- typename F,
- typename C
- //! \cond
- , typename = typename std::enable_if< std::is_constructible< base_type, F, C, bool >::value >::type
-
- >
- explicit scope_success(F&& func, C&& cond, bool active = true)
- noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(std::is_nothrow_constructible< base_type, F, C, bool >::value)) :
- base_type(static_cast< F&& >(func), static_cast< C&& >(cond), active)
- {
- }
-
-
- template<
- bool Requires = std::is_move_constructible< base_type >::value,
- typename = typename std::enable_if< Requires >::type
- >
-
- scope_success(scope_success&& that)
- noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(std::is_nothrow_move_constructible< base_type >::value)) :
- base_type(static_cast< base_type&& >(that))
- {
- }
- scope_success& operator= (scope_success&&) = delete;
- scope_success(scope_success const&) = delete;
- scope_success& operator= (scope_success const&) = delete;
- };
- #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
- template< typename Func >
- explicit scope_success(Func) -> scope_success< Func >;
- template< typename Func >
- explicit scope_success(Func, bool) -> scope_success< Func >;
- template<
- typename Func,
- typename Cond,
- typename = typename std::enable_if< detail::is_invocable< Cond const& >::value >::type
- >
- explicit scope_success(Func, Cond) -> scope_success< Func, Cond >;
- template<
- typename Func,
- typename Cond,
- typename = typename std::enable_if< detail::is_invocable< Cond const& >::value >::type
- >
- explicit scope_success(Func, Cond, bool) -> scope_success< Func, Cond >;
- #endif
- template< typename F >
- inline scope_success< typename std::decay< F >::type > make_scope_success(F&& func, bool active = true)
- noexcept(std::is_nothrow_constructible<
- scope_success< typename std::decay< F >::type >,
- F,
- bool
- >::value)
- {
- return scope_success< typename std::decay< F >::type >(static_cast< F&& >(func), active);
- }
- template< typename F, typename C >
- inline
- #if !defined(BOOST_SCOPE_DOXYGEN)
- typename std::enable_if<
- detail::is_invocable< C const& >::value,
- scope_success< typename std::decay< F >::type, typename std::decay< C >::type >
- >::type
- #else
- scope_success< typename std::decay< F >::type, typename std::decay< C >::type >
- #endif
- make_scope_success(F&& func, C&& cond, bool active = true)
- noexcept(std::is_nothrow_constructible<
- scope_success< typename std::decay< F >::type, typename std::decay< C >::type >,
- F,
- C,
- bool
- >::value)
- {
- return scope_success< typename std::decay< F >::type, typename std::decay< C >::type >(static_cast< F&& >(func), static_cast< C&& >(cond), active);
- }
- }
- }
- #include <boost/scope/detail/footer.hpp>
- #endif
|