1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* https://www.boost.org/LICENSE_1_0.txt)
*
* Copyright (c) 2022 Andrey Semashev
*/
/*!
* \file scope/scope_success.hpp
*
* This header contains definition of \c scope_success template.
*/
#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 {
// Workaround for clang < 5.0 which can't pass scope_success as a template template parameter from within scope_success definition
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();
}
};
} // namespace detail
/*!
* \brief Scope exit guard that invokes a function upon leaving the scope, if
* a failure condition is not satisfied.
*
* The scope guard wraps two function objects: the scope guard action and
* a failure condition for invoking the action. Both function objects must
* be callable with no arguments and can be one of:
*
* \li A user-defined class with a public `operator()`.
* \li An lvalue reference to such class.
* \li An lvalue reference or pointer to function taking no arguments.
*
* The condition function object `operator()` must return a value
* contextually convertible to \c true, if the failure is detected and the
* action function object is not allowed to be executed, and \c false otherwise.
* Additionally, the failure condition function object `operator()` must not
* throw, as otherwise the action function object may not be called. If not
* specified, the default failure condition checks whether the scope is left
* due to an exception - the action function object will only be called if
* the scope is left normally.
*
* \sa scope_exit
* \sa scope_fail
*
* \tparam Func Scope guard action function object type.
* \tparam Cond Scope guard failure condition function object type.
*/
template< typename Func, typename Cond = exception_checker >
class scope_success :
public scope_exit< Func, detail::logical_not< Cond > >
{
//! \cond
private:
using base_type = scope_exit< Func, detail::logical_not< Cond > >;
//! \endcond
public:
/*!
* \brief Constructs a scope guard with a given callable function object.
*
* **Requires:** \c Func is constructible from \a func. \c Cond is nothrow default-constructible.
*
* **Effects:** Constructs the scope guard as if by calling
* `scope_success(std::forward< F >(func), Cond(), active)`.
*
* **Throws:** Nothing, unless construction of the function objects throw.
*
* \param func The callable action function object to invoke on destruction.
* \param active Indicates whether the scope guard should be active upon construction.
*
* \post `this->active() == active`
*/
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
//! \endcond
>
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)
{
}
/*!
* \brief Constructs a scope guard with a given callable action and failure condition function objects.
*
* **Requires:** \c Func is constructible from \a func. \c Cond is constructible from \a cond.
*
* **Effects:** If \c Func is nothrow constructible from `F&&` then constructs \c Func from
* `std::forward< F >(func)`, otherwise constructs from `func`. If \c Cond is
* nothrow constructible from `C&&` then constructs \c Cond from
* `std::forward< C >(cond)`, otherwise constructs from `cond`.
*
* If \c Func or \c Cond construction throws and \a active is \c true, invokes
* \a cond and, if it returns \c true, \a func before returning with the exception.
*
* **Throws:** Nothing, unless construction of the function objects throw.
*
* \param func The callable action function object to invoke on destruction.
* \param cond The callable failure condition function object.
* \param active Indicates whether the scope guard should be active upon construction.
*
* \post `this->active() == active`
*/
template<
typename F,
typename C
//! \cond
, typename = typename std::enable_if< std::is_constructible< base_type, F, C, bool >::value >::type
//! \endcond
>
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)
{
}
/*!
* \brief Move-constructs a scope guard.
*
* **Requires:** \c Func and \c Cond are nothrow move-constructible or copy-constructible.
*
* **Effects:** If \c Func is nothrow move-constructible then move-constructs \c Func from
* a member of \a that, otherwise copy-constructs \c Func. If \c Cond is nothrow
* move-constructible then move-constructs \c Cond from a member of \a that,
* otherwise copy-constructs \c Cond.
*
* If \c Func or \c Cond construction throws and `that.active() == true`, invokes
* \c Cond object stored in \a that and, if it returns \c true, \a Func object
* (either the newly constructed one, if its construction succeeded, or the original
* one stored in \a that) before returning with the exception.
*
* If the construction succeeds, marks \a that as inactive.
*
* **Throws:** Nothing, unless move-construction of the function objects throw.
*
* \param that Move source.
*
* \post `that.active() == false`
*/
//! \cond
template<
bool Requires = std::is_move_constructible< base_type >::value,
typename = typename std::enable_if< Requires >::type
>
//! \endcond
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 // !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
/*!
* \brief Creates a scope fail guard with a given action function object.
*
* **Effects:** Constructs a scope guard as if by calling
* `scope_success< std::decay_t< F > >(std::forward< F >(func), active)`.
*
* \param func The callable function object to invoke on destruction.
* \param active Indicates whether the scope guard should be active upon construction.
*/
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);
}
/*!
* \brief Creates a scope fail with given callable function objects.
*
* **Effects:** Constructs a scope guard as if by calling
* `scope_success< std::decay_t< F >, std::decay_t< C > >(
* std::forward< F >(func), std::forward< C >(cond), active)`.
*
* \param func The callable action function object to invoke on destruction.
* \param cond The callable failure condition function object.
* \param active Indicates whether the scope guard should be active upon construction.
*/
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);
}
} // namespace scope
} // namespace boost
#include <boost/scope/detail/footer.hpp>
#endif // BOOST_SCOPE_SCOPE_SUCCESS_HPP_INCLUDED_