You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 satır
4.4KB

  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2019 offa
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #pragma once
  23. #include <utility>
  24. #include <type_traits>
  25. namespace sr::detail
  26. {
  27. // From P0550
  28. template<class T>
  29. using remove_cvref = std::remove_cv<std::remove_reference<T>>;
  30. template<class T>
  31. using remove_cvref_t = typename remove_cvref<T>::type;
  32. template<class F, class S>
  33. struct is_noexcept_dtor
  34. {
  35. static inline constexpr bool value = true;
  36. };
  37. template<class F, class S>
  38. inline constexpr bool is_noexcept_dtor_v = is_noexcept_dtor<F, S>::value;
  39. template<class T>
  40. constexpr decltype(auto) forward_if_nothrow_move_constructible(T&& arg)
  41. {
  42. if constexpr( std::is_nothrow_move_constructible_v<T> == true )
  43. {
  44. return std::forward<T>(arg);
  45. }
  46. return arg;
  47. }
  48. template<class EF, class Strategy>
  49. class scope_guard_base : private Strategy
  50. {
  51. public:
  52. template<class EFP,
  53. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  54. std::enable_if_t<(!std::is_lvalue_reference_v<EFP>)
  55. && std::is_nothrow_constructible_v<EF, EFP>, int> = 0
  56. >
  57. explicit scope_guard_base(EFP&& exitFunction) noexcept(std::is_nothrow_constructible_v<EF, EFP>
  58. || std::is_nothrow_constructible_v<EF, EFP&>)
  59. : exitfunction(std::forward<EFP>(exitFunction)),
  60. execute_on_destruction(true)
  61. {
  62. }
  63. template<class EFP,
  64. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  65. std::enable_if_t<std::is_lvalue_reference_v<EFP>, int> = 0
  66. >
  67. explicit scope_guard_base(EFP&& exitFunction) try : exitfunction(exitFunction),
  68. execute_on_destruction(true)
  69. {
  70. }
  71. catch( ... )
  72. {
  73. exitFunction();
  74. throw;
  75. }
  76. template<class EFP = EF, std::enable_if_t<(std::is_nothrow_move_constructible_v<EF>
  77. || std::is_copy_constructible_v<EF>), int> = 0
  78. >
  79. scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible_v<EF>
  80. || std::is_nothrow_copy_constructible_v<EF>)
  81. : Strategy(other),
  82. exitfunction(forward_if_nothrow_move_constructible(other.exitfunction)),
  83. execute_on_destruction(other.execute_on_destruction)
  84. {
  85. other.release();
  86. }
  87. scope_guard_base(const scope_guard_base&) = delete;
  88. ~scope_guard_base() noexcept(is_noexcept_dtor_v<EF, Strategy>)
  89. {
  90. if( (execute_on_destruction == true) && (this->should_execute() == true) )
  91. {
  92. exitfunction();
  93. }
  94. }
  95. void release() noexcept
  96. {
  97. execute_on_destruction = false;
  98. }
  99. scope_guard_base& operator=(const scope_guard_base&) = delete;
  100. scope_guard_base& operator=(scope_guard_base&&) = delete;
  101. private:
  102. EF exitfunction;
  103. bool execute_on_destruction;
  104. };
  105. }