Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

scope_guard_base.h 4.4KB

pirms 3 gadiem
pirms 7 gadiem
pirms 7 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2021 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 : public std::true_type
  34. {
  35. };
  36. template<class F, class S>
  37. inline constexpr bool is_noexcept_dtor_v = is_noexcept_dtor<F, S>::value;
  38. template<class T>
  39. constexpr decltype(auto) forward_if_nothrow_move_constructible(T&& arg)
  40. {
  41. if constexpr( std::is_nothrow_move_constructible_v<T> == true )
  42. {
  43. return std::forward<T>(arg);
  44. }
  45. return arg;
  46. }
  47. template<class EF, class Strategy>
  48. class scope_guard_base : private Strategy
  49. {
  50. public:
  51. template<class EFP,
  52. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  53. std::enable_if_t<(!std::is_lvalue_reference_v<EFP>)
  54. && std::is_nothrow_constructible_v<EF, EFP>, int> = 0
  55. >
  56. explicit scope_guard_base(EFP&& exitFunction) noexcept(std::is_nothrow_constructible_v<EF, EFP>
  57. || std::is_nothrow_constructible_v<EF, EFP&>)
  58. : exitfunction(std::forward<EFP>(exitFunction)),
  59. execute_on_destruction(true)
  60. {
  61. }
  62. template<class EFP,
  63. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  64. std::enable_if_t<std::is_lvalue_reference_v<EFP>, int> = 0
  65. >
  66. explicit scope_guard_base(EFP&& exitFunction) try : exitfunction(exitFunction),
  67. execute_on_destruction(true)
  68. {
  69. }
  70. catch( ... )
  71. {
  72. exitFunction();
  73. throw;
  74. }
  75. template<class EFP = EF, std::enable_if_t<(std::is_nothrow_move_constructible_v<EF>
  76. || std::is_copy_constructible_v<EF>), int> = 0
  77. >
  78. scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible_v<EF>
  79. || std::is_nothrow_copy_constructible_v<EF>)
  80. : Strategy(other),
  81. exitfunction(forward_if_nothrow_move_constructible(other.exitfunction)),
  82. execute_on_destruction(other.execute_on_destruction)
  83. {
  84. other.release();
  85. }
  86. scope_guard_base(const scope_guard_base&) = delete;
  87. ~scope_guard_base() noexcept(is_noexcept_dtor_v<EF, Strategy>)
  88. {
  89. if( (execute_on_destruction == true) && (this->should_execute() == true) )
  90. {
  91. exitfunction();
  92. }
  93. }
  94. void release() noexcept
  95. {
  96. execute_on_destruction = false;
  97. }
  98. scope_guard_base& operator=(const scope_guard_base&) = delete;
  99. scope_guard_base& operator=(scope_guard_base&&) = delete;
  100. private:
  101. EF exitfunction;
  102. bool execute_on_destruction;
  103. };
  104. }