Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

131 lines
4.0KB

  1. /*
  2. * Scope Guard
  3. * Copyright (C) 2017-2018 offa
  4. *
  5. * This file is part of Scope Guard.
  6. *
  7. * Scope Guard is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Scope Guard is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Scope Guard. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #include <utility>
  22. #include <type_traits>
  23. namespace sr::detail
  24. {
  25. // From P0550
  26. template<class T>
  27. using remove_cvref = std::remove_cv<std::remove_reference<T>>;
  28. template<class T>
  29. using remove_cvref_t = typename remove_cvref<T>::type;
  30. template<class F, class S>
  31. struct is_noexcept_dtor
  32. {
  33. static constexpr bool value = true;
  34. };
  35. template<class F, class S>
  36. constexpr bool is_noexcept_dtor_v = is_noexcept_dtor<F, S>::value;
  37. template<class T>
  38. constexpr decltype(auto) forward_if_nothrow_move_constructible(T&& arg)
  39. {
  40. if constexpr( std::is_nothrow_move_constructible_v<T> == true )
  41. {
  42. return std::forward<T>(arg);
  43. }
  44. return arg;
  45. }
  46. template<class EF, class Strategy>
  47. class scope_guard_base : private Strategy
  48. {
  49. public:
  50. template<class EFP,
  51. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  52. std::enable_if_t<(!std::is_lvalue_reference_v<EFP>)
  53. && std::is_nothrow_constructible_v<EF, EFP>, int> = 0
  54. >
  55. explicit scope_guard_base(EFP&& exitFunction) noexcept(std::is_nothrow_constructible_v<EF, EFP>
  56. || std::is_nothrow_constructible_v<EF, EFP&>)
  57. : m_exitfunction(std::forward<EFP>(exitFunction)),
  58. m_execute_on_destruction(true)
  59. {
  60. }
  61. template<class EFP,
  62. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  63. std::enable_if_t<std::is_lvalue_reference_v<EFP>, int> = 0
  64. >
  65. explicit scope_guard_base(EFP&& exitFunction) try : m_exitfunction(exitFunction),
  66. m_execute_on_destruction(true)
  67. {
  68. }
  69. catch( ... )
  70. {
  71. exitFunction();
  72. throw;
  73. }
  74. template<class EFP = EF, std::enable_if_t<(std::is_nothrow_move_constructible_v<EF>
  75. || std::is_copy_constructible_v<EF>), int> = 0
  76. >
  77. scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible_v<EF>
  78. || std::is_nothrow_copy_constructible_v<EF>)
  79. : Strategy(other),
  80. m_exitfunction(forward_if_nothrow_move_constructible(other.m_exitfunction)),
  81. m_execute_on_destruction(other.m_execute_on_destruction)
  82. {
  83. other.release();
  84. }
  85. scope_guard_base(const scope_guard_base&) = delete;
  86. ~scope_guard_base() noexcept(is_noexcept_dtor_v<EF, Strategy>)
  87. {
  88. if( (m_execute_on_destruction == true) && (this->should_execute() == true) )
  89. {
  90. m_exitfunction();
  91. }
  92. }
  93. void release() noexcept
  94. {
  95. m_execute_on_destruction = false;
  96. }
  97. scope_guard_base& operator=(const scope_guard_base&) = delete;
  98. scope_guard_base& operator=(scope_guard_base&&) = delete;
  99. private:
  100. EF m_exitfunction;
  101. bool m_execute_on_destruction;
  102. };
  103. }