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.

111 lines
3.2KB

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