scope_exit.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * https://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2023 Andrey Semashev
  7. */
  8. /*!
  9. * \file scope/scope_exit.hpp
  10. *
  11. * This header contains definition of \c scope_exit template.
  12. */
  13. #ifndef BOOST_SCOPE_SCOPE_EXIT_HPP_INCLUDED_
  14. #define BOOST_SCOPE_SCOPE_EXIT_HPP_INCLUDED_
  15. #include <type_traits>
  16. #include <boost/scope/detail/config.hpp>
  17. #include <boost/scope/detail/is_not_like.hpp>
  18. #include <boost/scope/detail/compact_storage.hpp>
  19. #include <boost/scope/detail/move_or_copy_construct_ref.hpp>
  20. #include <boost/scope/detail/is_nonnull_default_constructible.hpp>
  21. #include <boost/scope/detail/type_traits/conjunction.hpp>
  22. #include <boost/scope/detail/type_traits/is_invocable.hpp>
  23. #include <boost/scope/detail/type_traits/is_nothrow_invocable.hpp>
  24. #include <boost/scope/detail/header.hpp>
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. namespace scope {
  30. template< typename Func, typename Cond >
  31. class scope_exit;
  32. namespace detail {
  33. // Workaround for clang < 5.0 which can't pass scope_exit as a template template parameter from within scope_exit definition
  34. template< typename T >
  35. using is_not_like_scope_exit = detail::is_not_like< T, scope_exit >;
  36. //! The scope guard used to invoke the condition and action functions in case of exception during scope guard construction
  37. template< typename Func, typename Cond >
  38. class init_guard
  39. {
  40. private:
  41. Func& m_func;
  42. Cond& m_cond;
  43. bool m_active;
  44. public:
  45. init_guard(Func& func, Cond& cond, bool active) noexcept :
  46. m_func(func),
  47. m_cond(cond),
  48. m_active(active)
  49. {
  50. }
  51. init_guard(init_guard const&) = delete;
  52. init_guard& operator= (init_guard const&) = delete;
  53. ~init_guard()
  54. noexcept(detail::conjunction<
  55. detail::is_nothrow_invocable< Func& >,
  56. detail::is_nothrow_invocable< Cond& >
  57. >::value)
  58. {
  59. if (m_active && m_cond())
  60. m_func();
  61. }
  62. Func&& get_func() noexcept
  63. {
  64. return static_cast< Func&& >(m_func);
  65. }
  66. Cond&& get_cond() noexcept
  67. {
  68. return static_cast< Cond&& >(m_cond);
  69. }
  70. void deactivate() noexcept
  71. {
  72. m_active = false;
  73. }
  74. };
  75. } // namespace detail
  76. /*!
  77. * \brief A predicate that always returns \c true.
  78. *
  79. * This predicate can be used as the default condition function object for
  80. * \c scope_exit and similar scope guards.
  81. */
  82. class always_true
  83. {
  84. public:
  85. //! Predicate result type
  86. using result_type = bool;
  87. /*!
  88. * **Throws:** Nothing.
  89. *
  90. * \returns \c true.
  91. */
  92. result_type operator()() const noexcept
  93. {
  94. return true;
  95. }
  96. };
  97. /*!
  98. * \brief Scope exit guard that conditionally invokes a function upon leaving the scope.
  99. *
  100. * The scope guard wraps two function objects: the scope guard action and
  101. * a condition for invoking the action. Both function objects must be
  102. * callable with no arguments and can be one of:
  103. *
  104. * \li A user-defined class with a public `operator()`.
  105. * \li An lvalue reference to such class.
  106. * \li An lvalue reference or pointer to function taking no arguments.
  107. *
  108. * The condition function object `operator()` must return a value
  109. * contextually convertible to \c true, if the action function object
  110. * is allowed to be executed, and \c false otherwise. Additionally,
  111. * the condition function object `operator()` must not throw, as
  112. * otherwise the action function object may not be called.
  113. *
  114. * The condition function object is optional, and if not specified in
  115. * template parameters, the scope guard will operate as if the condition
  116. * always returns \c true.
  117. *
  118. * The scope guard can be in either active or inactive state. By default,
  119. * the constructed scope guard is active. When active, and condition
  120. * function object returns \c true, the scope guard invokes the wrapped
  121. * action function object on destruction. Otherwise, the scope guard
  122. * does not call the wrapped action function object.
  123. *
  124. * The scope guard can be made inactive by moving-from the scope guard
  125. * or calling `set_active(false)`. An inactive scope guard can be made
  126. * active by calling `set_active(true)`. If a moved-from scope guard
  127. * is active on destruction, the behavior is undefined.
  128. *
  129. * \tparam Func Scope guard action function object type.
  130. * \tparam Cond Scope guard condition function object type.
  131. */
  132. template< typename Func, typename Cond = always_true >
  133. class scope_exit
  134. {
  135. //! \cond
  136. private:
  137. struct func_holder :
  138. public detail::compact_storage< Func >
  139. {
  140. using func_base = detail::compact_storage< Func >;
  141. template<
  142. typename F,
  143. typename C,
  144. typename = typename std::enable_if< std::is_constructible< Func, F >::value >::type
  145. >
  146. explicit func_holder(F&& func, C&& cond, bool active, std::true_type) noexcept :
  147. func_base(static_cast< F&& >(func))
  148. {
  149. }
  150. template<
  151. typename F,
  152. typename C,
  153. typename = typename std::enable_if< std::is_constructible< Func, F >::value >::type
  154. >
  155. explicit func_holder(F&& func, C&& cond, bool active, std::false_type) :
  156. func_holder(detail::init_guard< F, C >(func, cond, active))
  157. {
  158. }
  159. private:
  160. template< typename F, typename C >
  161. explicit func_holder(detail::init_guard< F, C >&& init) :
  162. func_base(init.get_func())
  163. {
  164. init.deactivate();
  165. }
  166. };
  167. struct cond_holder :
  168. public detail::compact_storage< Cond >
  169. {
  170. using cond_base = detail::compact_storage< Cond >;
  171. template<
  172. typename C,
  173. typename = typename std::enable_if< std::is_constructible< Cond, C >::value >::type
  174. >
  175. explicit cond_holder(C&& cond, Func& func, bool active, std::true_type) noexcept :
  176. cond_base(static_cast< C&& >(cond))
  177. {
  178. }
  179. template<
  180. typename C,
  181. typename = typename std::enable_if< std::is_constructible< Cond, C >::value >::type
  182. >
  183. explicit cond_holder(C&& cond, Func& func, bool active, std::false_type) :
  184. cond_holder(detail::init_guard< Func&, C >(func, cond, active))
  185. {
  186. }
  187. private:
  188. template< typename C >
  189. explicit cond_holder(detail::init_guard< Func&, C >&& init) :
  190. cond_base(init.get_cond())
  191. {
  192. init.deactivate();
  193. }
  194. };
  195. struct data :
  196. public func_holder,
  197. public cond_holder
  198. {
  199. bool m_active;
  200. template<
  201. typename F,
  202. typename C,
  203. typename = typename std::enable_if< detail::conjunction<
  204. std::is_constructible< func_holder, F, C, bool, typename std::is_nothrow_constructible< Func, F >::type >,
  205. std::is_constructible< cond_holder, C, Func&, bool, typename std::is_nothrow_constructible< Cond, C >::type >
  206. >::value >::type
  207. >
  208. explicit data(F&& func, C&& cond, bool active)
  209. noexcept(detail::conjunction< std::is_nothrow_constructible< Func, F >, std::is_nothrow_constructible< Cond, C > >::value) :
  210. func_holder(static_cast< F&& >(func), static_cast< C&& >(cond), active, typename std::is_nothrow_constructible< Func, F >::type()),
  211. cond_holder(static_cast< C&& >(cond), func_holder::get(), active, typename std::is_nothrow_constructible< Cond, C >::type()),
  212. m_active(active)
  213. {
  214. }
  215. Func& get_func() noexcept
  216. {
  217. return func_holder::get();
  218. }
  219. Func const& get_func() const noexcept
  220. {
  221. return func_holder::get();
  222. }
  223. Cond& get_cond() noexcept
  224. {
  225. return cond_holder::get();
  226. }
  227. Cond const& get_cond() const noexcept
  228. {
  229. return cond_holder::get();
  230. }
  231. bool deactivate() noexcept
  232. {
  233. bool active = m_active;
  234. m_active = false;
  235. return active;
  236. }
  237. };
  238. data m_data;
  239. //! \endcond
  240. public:
  241. /*!
  242. * \brief Constructs a scope guard with a given callable action function object.
  243. *
  244. * **Requires:** \c Func is constructible from \a func. \c Cond is nothrow default-constructible
  245. * and is not a pointer to function.
  246. *
  247. * \note The requirement for \c Cond default constructor to be non-throwing is to allow for
  248. * the condition function object to be called in case if constructing either function
  249. * object throws.
  250. *
  251. * **Effects:** Constructs the scope guard as if by calling
  252. * `scope_exit(std::forward< F >(func), Cond(), active)`.
  253. *
  254. * **Throws:** Nothing, unless construction of the function objects throw.
  255. *
  256. * \param func The callable action function object to invoke on destruction.
  257. * \param active Indicates whether the scope guard should be active upon construction.
  258. *
  259. * \post `this->active() == active`
  260. */
  261. template<
  262. typename F
  263. //! \cond
  264. , typename = typename std::enable_if< detail::conjunction<
  265. detail::is_nothrow_nonnull_default_constructible< Cond >,
  266. std::is_constructible<
  267. data,
  268. typename detail::move_or_copy_construct_ref< F, Func >::type,
  269. typename detail::move_or_copy_construct_ref< Cond >::type,
  270. bool
  271. >,
  272. detail::is_not_like_scope_exit< F >
  273. >::value >::type
  274. //! \endcond
  275. >
  276. explicit scope_exit(F&& func, bool active = true)
  277. noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(
  278. std::is_nothrow_constructible<
  279. data,
  280. typename detail::move_or_copy_construct_ref< F, Func >::type,
  281. typename detail::move_or_copy_construct_ref< Cond >::type,
  282. bool
  283. >::value
  284. )) :
  285. m_data
  286. (
  287. static_cast< typename detail::move_or_copy_construct_ref< F, Func >::type >(func),
  288. static_cast< typename detail::move_or_copy_construct_ref< Cond >::type >(Cond()),
  289. active
  290. )
  291. {
  292. }
  293. /*!
  294. * \brief Constructs a scope guard with a given callable action and condition function objects.
  295. *
  296. * **Requires:** \c Func is constructible from \a func. \c Cond is constructible from \a cond.
  297. *
  298. * **Effects:** If \c Func is nothrow constructible from `F&&` then constructs \c Func from
  299. * `std::forward< F >(func)`, otherwise constructs from `func`. If \c Cond is
  300. * nothrow constructible from `C&&` then constructs \c Cond from
  301. * `std::forward< C >(cond)`, otherwise constructs from `cond`.
  302. *
  303. * If \c Func or \c Cond construction throws and \a active is \c true, invokes
  304. * \a cond and, if it returns \c true, \a func before returning with the exception.
  305. *
  306. * **Throws:** Nothing, unless construction of the function objects throw.
  307. *
  308. * \param func The callable action function object to invoke on destruction.
  309. * \param cond The callable condition function object.
  310. * \param active Indicates whether the scope guard should be active upon construction.
  311. *
  312. * \post `this->active() == active`
  313. */
  314. template<
  315. typename F,
  316. typename C
  317. //! \cond
  318. , typename = typename std::enable_if< detail::conjunction<
  319. detail::is_invocable< C const& >,
  320. std::is_constructible<
  321. data,
  322. typename detail::move_or_copy_construct_ref< F, Func >::type,
  323. typename detail::move_or_copy_construct_ref< C, Cond >::type,
  324. bool
  325. >
  326. >::value >::type
  327. //! \endcond
  328. >
  329. explicit scope_exit(F&& func, C&& cond, bool active = true)
  330. noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(
  331. std::is_nothrow_constructible<
  332. data,
  333. typename detail::move_or_copy_construct_ref< F, Func >::type,
  334. typename detail::move_or_copy_construct_ref< C, Cond >::type,
  335. bool
  336. >::value
  337. )) :
  338. m_data
  339. (
  340. static_cast< typename detail::move_or_copy_construct_ref< F, Func >::type >(func),
  341. static_cast< typename detail::move_or_copy_construct_ref< C, Cond >::type >(cond),
  342. active
  343. )
  344. {
  345. }
  346. /*!
  347. * \brief Move-constructs a scope guard.
  348. *
  349. * **Requires:** \c Func and \c Cond are nothrow move-constructible or copy-constructible.
  350. *
  351. * **Effects:** If \c Func is nothrow move-constructible then move-constructs \c Func from
  352. * a member of \a that, otherwise copy-constructs \c Func. If \c Cond is nothrow
  353. * move-constructible then move-constructs \c Cond from a member of \a that,
  354. * otherwise copy-constructs \c Cond.
  355. *
  356. * If \c Func or \c Cond construction throws and `that.active() == true`, invokes
  357. * \c Cond object stored in \a that and, if it returns \c true, \a Func object
  358. * (either the newly constructed one, if its construction succeeded, or the original
  359. * one stored in \a that) before returning with the exception.
  360. *
  361. * If the construction succeeds, marks \a that as inactive.
  362. *
  363. * **Throws:** Nothing, unless move-construction of the function objects throw.
  364. *
  365. * \param that Move source.
  366. *
  367. * \post `that.active() == false`
  368. */
  369. //! \cond
  370. template<
  371. bool Requires = std::is_constructible<
  372. data,
  373. typename detail::move_or_copy_construct_ref< Func >::type,
  374. typename detail::move_or_copy_construct_ref< Cond >::type,
  375. bool
  376. >::value,
  377. typename = typename std::enable_if< Requires >::type
  378. >
  379. //! \endcond
  380. scope_exit(scope_exit&& that)
  381. noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(
  382. std::is_nothrow_constructible<
  383. data,
  384. typename detail::move_or_copy_construct_ref< Func >::type,
  385. typename detail::move_or_copy_construct_ref< Cond >::type,
  386. bool
  387. >::value
  388. )) :
  389. m_data
  390. (
  391. static_cast< typename detail::move_or_copy_construct_ref< Func >::type >(that.m_data.get_func()),
  392. static_cast< typename detail::move_or_copy_construct_ref< Cond >::type >(that.m_data.get_cond()),
  393. that.m_data.deactivate()
  394. )
  395. {
  396. }
  397. scope_exit& operator= (scope_exit&&) = delete;
  398. scope_exit(scope_exit const&) = delete;
  399. scope_exit& operator= (scope_exit const&) = delete;
  400. /*!
  401. * \brief If `active() == true`, and invoking the condition function object returns \c true, invokes
  402. * the wrapped callable action function object. Destroys the function objects.
  403. *
  404. * **Throws:** Nothing, unless invoking a function object throws.
  405. */
  406. ~scope_exit()
  407. noexcept(BOOST_SCOPE_DETAIL_DOC_HIDDEN(
  408. detail::conjunction<
  409. detail::is_nothrow_invocable< Func& >,
  410. detail::is_nothrow_invocable< Cond& >
  411. >::value
  412. ))
  413. {
  414. if (BOOST_LIKELY(m_data.m_active && m_data.get_cond()()))
  415. m_data.get_func()();
  416. }
  417. /*!
  418. * \brief Returns \c true if the scope guard is active, otherwise \c false.
  419. *
  420. * \note This method does not call the condition function object specified on construction.
  421. *
  422. * **Throws:** Nothing.
  423. */
  424. bool active() const noexcept
  425. {
  426. return m_data.m_active;
  427. }
  428. /*!
  429. * \brief Activates or deactivates the scope guard.
  430. *
  431. * **Throws:** Nothing.
  432. *
  433. * \param active The active status to set.
  434. *
  435. * \post `this->active() == active`
  436. */
  437. void set_active(bool active) noexcept
  438. {
  439. m_data.m_active = active;
  440. }
  441. };
  442. #if !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  443. template< typename Func >
  444. explicit scope_exit(Func) -> scope_exit< Func >;
  445. template< typename Func >
  446. explicit scope_exit(Func, bool) -> scope_exit< Func >;
  447. template< typename Func, typename Cond >
  448. explicit scope_exit(Func, Cond) -> scope_exit< Func, Cond >;
  449. template< typename Func, typename Cond >
  450. explicit scope_exit(Func, Cond, bool) -> scope_exit< Func, Cond >;
  451. #endif // !defined(BOOST_NO_CXX17_DEDUCTION_GUIDES)
  452. /*!
  453. * \brief Creates a scope guard with a given action function object.
  454. *
  455. * **Effects:** Constructs a scope guard as if by calling
  456. * `scope_exit< std::decay_t< F > >(std::forward< F >(func), active)`.
  457. *
  458. * \param func The callable action function object to invoke on destruction.
  459. * \param active Indicates whether the scope guard should be active upon construction.
  460. */
  461. template< typename F >
  462. inline scope_exit< typename std::decay< F >::type > make_scope_exit(F&& func, bool active = true)
  463. noexcept(std::is_nothrow_constructible<
  464. scope_exit< typename std::decay< F >::type >,
  465. F,
  466. bool
  467. >::value)
  468. {
  469. return scope_exit< typename std::decay< F >::type >(static_cast< F&& >(func), active);
  470. }
  471. /*!
  472. * \brief Creates a conditional scope guard with given callable function objects.
  473. *
  474. * **Effects:** Constructs a scope guard as if by calling
  475. * `scope_exit< std::decay_t< F >, std::decay_t< C > >(
  476. * std::forward< F >(func), std::forward< C >(cond), active)`.
  477. *
  478. * \param func The callable action function object to invoke on destruction.
  479. * \param cond The callable condition function object.
  480. * \param active Indicates whether the scope guard should be active upon construction.
  481. */
  482. template< typename F, typename C >
  483. inline
  484. #if !defined(BOOST_SCOPE_DOXYGEN)
  485. typename std::enable_if<
  486. std::is_constructible<
  487. scope_exit< typename std::decay< F >::type, typename std::decay< C >::type >,
  488. F,
  489. C,
  490. bool
  491. >::value,
  492. scope_exit< typename std::decay< F >::type, typename std::decay< C >::type >
  493. >::type
  494. #else
  495. scope_exit< typename std::decay< F >::type, typename std::decay< C >::type >
  496. #endif
  497. make_scope_exit(F&& func, C&& cond, bool active = true)
  498. noexcept(std::is_nothrow_constructible<
  499. scope_exit< typename std::decay< F >::type, typename std::decay< C >::type >,
  500. F,
  501. C,
  502. bool
  503. >::value)
  504. {
  505. return scope_exit< typename std::decay< F >::type, typename std::decay< C >::type >(static_cast< F&& >(func), static_cast< C&& >(cond), active);
  506. }
  507. } // namespace scope
  508. } // namespace boost
  509. #include <boost/scope/detail/footer.hpp>
  510. #endif // BOOST_SCOPE_SCOPE_EXIT_HPP_INCLUDED_