Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
7 роки тому
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Scope Guard
  3. * Copyright (C) 2017 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
  24. {
  25. namespace detail
  26. {
  27. template<class F, class S>
  28. constexpr bool is_noexcept_dtor_v = true;
  29. template<class EF, class Strategy>
  30. class scope_guard_base : private Strategy
  31. {
  32. public:
  33. template<class EFP,
  34. std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0,
  35. std::enable_if_t<(!std::is_lvalue_reference<EFP>::value)
  36. && std::is_nothrow_constructible<EF, EFP>::value, int> = 0
  37. >
  38. explicit scope_guard_base(EFP&& exitFunction) : m_exitFunction(std::move(exitFunction)),
  39. m_execute_on_destruction(true)
  40. {
  41. }
  42. template<class EFP,
  43. std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0,
  44. std::enable_if_t<std::is_lvalue_reference<EFP>::value, int> = 0
  45. >
  46. explicit scope_guard_base(EFP&& exitFunction) try : m_exitFunction(exitFunction),
  47. m_execute_on_destruction(true)
  48. {
  49. }
  50. catch( ... )
  51. {
  52. exitFunction();
  53. throw;
  54. }
  55. template<class T = EF,
  56. std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0
  57. >
  58. scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible<T>::value
  59. || std::is_nothrow_copy_constructible<T>::value)
  60. : Strategy(other),
  61. m_exitFunction(std::move(other.m_exitFunction)),
  62. m_execute_on_destruction(other.m_execute_on_destruction)
  63. {
  64. other.release();
  65. }
  66. template<class T = EF,
  67. std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0
  68. >
  69. scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible<T>::value
  70. || std::is_nothrow_copy_constructible<T>::value)
  71. : Strategy(other),
  72. m_exitFunction(other.m_exitFunction),
  73. m_execute_on_destruction(other.m_execute_on_destruction)
  74. {
  75. other.release();
  76. }
  77. scope_guard_base(const scope_guard_base&) = delete;
  78. ~scope_guard_base() noexcept(is_noexcept_dtor_v<EF, Strategy>)
  79. {
  80. if( (m_execute_on_destruction == true) && (this->should_execute() == true) )
  81. {
  82. m_exitFunction();
  83. }
  84. }
  85. void release() noexcept
  86. {
  87. m_execute_on_destruction = false;
  88. }
  89. scope_guard_base& operator=(const scope_guard_base&) = delete;
  90. scope_guard_base& operator=(scope_guard_base&&) = delete;
  91. private:
  92. EF m_exitFunction;
  93. bool m_execute_on_destruction;
  94. };
  95. }
  96. }