Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

109 lines
3.3KB

  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::detail
  24. {
  25. template<class F, class S>
  26. struct is_noexcept_dtor
  27. {
  28. static constexpr bool value = true;
  29. };
  30. template<class F, class S>
  31. constexpr bool is_noexcept_dtor_v = is_noexcept_dtor<F, S>::value;
  32. template<class EF, class Strategy>
  33. class scope_guard_base : private Strategy
  34. {
  35. public:
  36. template<class EFP,
  37. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  38. std::enable_if_t<(!std::is_lvalue_reference_v<EFP>)
  39. && std::is_nothrow_constructible_v<EF, EFP>, int> = 0
  40. >
  41. explicit scope_guard_base(EFP&& exitFunction) noexcept(std::is_nothrow_constructible_v<EF, EFP>)
  42. : m_exitfunction(std::move(exitFunction)),
  43. m_execute_on_destruction(true)
  44. {
  45. }
  46. template<class EFP,
  47. std::enable_if_t<std::is_constructible_v<EF, EFP>, int> = 0,
  48. std::enable_if_t<std::is_lvalue_reference_v<EFP>, int> = 0
  49. >
  50. explicit scope_guard_base(EFP&& exitFunction) try : m_exitfunction(exitFunction),
  51. m_execute_on_destruction(true)
  52. {
  53. }
  54. catch( ... )
  55. {
  56. exitFunction();
  57. throw;
  58. }
  59. scope_guard_base(scope_guard_base&& other) noexcept(std::is_nothrow_move_constructible_v<EF>
  60. || std::is_nothrow_copy_constructible_v<EF>)
  61. : Strategy(other),
  62. m_exitfunction(std::move_if_noexcept(other.m_exitfunction)),
  63. m_execute_on_destruction(other.m_execute_on_destruction)
  64. {
  65. other.release();
  66. }
  67. scope_guard_base(const scope_guard_base&) = delete;
  68. ~scope_guard_base() noexcept(is_noexcept_dtor_v<EF, Strategy>)
  69. {
  70. if( (m_execute_on_destruction == true) && (this->should_execute() == true) )
  71. {
  72. m_exitfunction();
  73. }
  74. }
  75. void release() noexcept
  76. {
  77. m_execute_on_destruction = false;
  78. }
  79. scope_guard_base& operator=(const scope_guard_base&) = delete;
  80. scope_guard_base& operator=(scope_guard_base&&) = delete;
  81. private:
  82. EF m_exitfunction;
  83. bool m_execute_on_destruction;
  84. };
  85. }