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.

111 rindas
3.6KB

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