File indexing completed on 2025-04-19 08:44:21
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef BOOST_SCOPE_DEFER_HPP_INCLUDED_
0015 #define BOOST_SCOPE_DEFER_HPP_INCLUDED_
0016
0017 #include <type_traits>
0018 #include <boost/scope/detail/config.hpp>
0019 #include <boost/scope/detail/is_not_like.hpp>
0020 #include <boost/scope/detail/move_or_copy_construct_ref.hpp>
0021 #include <boost/scope/detail/type_traits/conjunction.hpp>
0022 #include <boost/scope/detail/type_traits/is_nothrow_invocable.hpp>
0023 #include <boost/scope/detail/header.hpp>
0024
0025 #ifdef BOOST_HAS_PRAGMA_ONCE
0026 #pragma once
0027 #endif
0028
0029 namespace boost {
0030 namespace scope {
0031
0032 template< typename Func >
0033 class defer_guard;
0034
0035 namespace detail {
0036
0037
0038 template< typename T >
0039 using is_not_like_defer_guard = detail::is_not_like< T, defer_guard >;
0040
0041 }
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 template< typename Func >
0057 class defer_guard
0058 {
0059
0060 private:
0061 struct data
0062 {
0063 Func m_func;
0064
0065 template< typename F, typename = typename std::enable_if< std::is_constructible< Func, F >::value >::type >
0066 explicit data(F&& func, std::true_type) noexcept :
0067 m_func(static_cast< F&& >(func))
0068 {
0069 }
0070
0071 template< typename F, typename = typename std::enable_if< std::is_constructible< Func, F >::value >::type >
0072 explicit data(F&& func, std::false_type) try :
0073 m_func(static_cast< F&& >(func))
0074 {
0075 }
0076 catch (...)
0077 {
0078 func();
0079 }
0080 };
0081
0082 data m_data;
0083
0084
0085 public:
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100 template<
0101 typename F
0102
0103 , typename = typename std::enable_if< detail::conjunction<
0104 std::is_constructible<
0105 data,
0106 typename detail::move_or_copy_construct_ref< F, Func >::type,
0107 typename std::is_nothrow_constructible< Func, typename detail::move_or_copy_construct_ref< F, Func >::type >::type
0108 >,
0109 detail::is_not_like_defer_guard< F >
0110 >::value >::type
0111
0112 >
0113 defer_guard(F&& func)
0114 noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(
0115 std::is_nothrow_constructible<
0116 data,
0117 typename detail::move_or_copy_construct_ref< F, Func >::type,
0118 typename std::is_nothrow_constructible< Func, typename detail::move_or_copy_construct_ref< F, Func >::type >::type
0119 >::value
0120 )) :
0121 m_data
0122 (
0123 static_cast< typename detail::move_or_copy_construct_ref< F, Func >::type >(func),
0124 typename std::is_nothrow_constructible< Func, typename detail::move_or_copy_construct_ref< F, Func >::type >::type()
0125 )
0126 {
0127 }
0128
0129 defer_guard(defer_guard const&) = delete;
0130 defer_guard& operator= (defer_guard const&) = delete;
0131
0132
0133
0134
0135
0136
0137 ~defer_guard() noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(detail::is_nothrow_invocable< Func& >::value))
0138 {
0139 m_data.m_func();
0140 }
0141 };
0142
0143 #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
0144 template< typename Func >
0145 defer_guard(Func) -> defer_guard< Func >;
0146 #endif
0147
0148 }
0149
0150
0151 #if defined(BOOST_MSVC)
0152 #define BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG __COUNTER__
0153 #else
0154 #define BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG __LINE__
0155 #endif
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173 #define BOOST_SCOPE_DEFER \
0174 boost::scope::defer_guard BOOST_JOIN(_boost_defer_guard_, BOOST_SCOPE_DETAIL_UNIQUE_VAR_TAG) =
0175
0176 }
0177
0178 #include <boost/scope/detail/footer.hpp>
0179
0180 #endif