Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

125 lines
4.0KB

  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. #include <exception>
  24. namespace sr
  25. {
  26. template<class EF>
  27. class scope_fail
  28. {
  29. public:
  30. template<class EFP,
  31. std::enable_if_t<std::is_constructible<EF, EFP>::value, int> = 0,
  32. std::enable_if_t<(!std::is_lvalue_reference<EFP>::value)
  33. && std::is_nothrow_constructible<EF, EFP>::value, int> = 0
  34. >
  35. explicit scope_fail(EFP&& exitFunction) : m_exitFunction(std::move(exitFunction)),
  36. m_execute_on_destruction(true),
  37. m_uncaught_on_creation(uncaught_exceptions())
  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_fail(EFP&& exitFunction) try : m_exitFunction(exitFunction),
  45. m_execute_on_destruction(true),
  46. m_uncaught_on_creation(uncaught_exceptions())
  47. {
  48. }
  49. catch( ... )
  50. {
  51. exitFunction();
  52. throw;
  53. }
  54. scope_fail(const scope_fail&) = delete;
  55. template<class T = EF,
  56. std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0
  57. >
  58. scope_fail(scope_fail&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
  59. : m_exitFunction(std::move(other.m_exitFunction)),
  60. m_execute_on_destruction(other.m_execute_on_destruction),
  61. m_uncaught_on_creation(uncaught_exceptions())
  62. {
  63. other.release();
  64. }
  65. template<class T = EF,
  66. std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0
  67. >
  68. scope_fail(scope_fail&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
  69. : m_exitFunction(other.m_exitFunction),
  70. m_execute_on_destruction(other.m_execute_on_destruction),
  71. m_uncaught_on_creation(other.m_uncaught_on_creation)
  72. {
  73. other.release();
  74. }
  75. ~scope_fail() noexcept(true)
  76. {
  77. if( ( m_execute_on_destruction == true ) && ( uncaught_exceptions() > m_uncaught_on_creation ) )
  78. {
  79. m_exitFunction();
  80. }
  81. }
  82. void release() noexcept
  83. {
  84. m_execute_on_destruction = false;
  85. }
  86. scope_fail& operator=(const scope_fail&) = delete;
  87. scope_fail& operator=(scope_fail&&) = delete;
  88. private:
  89. int uncaught_exceptions() const noexcept
  90. {
  91. return ( std::uncaught_exception() == true ? 1 : 0 );
  92. }
  93. EF m_exitFunction;
  94. bool m_execute_on_destruction;
  95. int m_uncaught_on_creation;
  96. };
  97. template<class EF>
  98. scope_fail<std::decay_t<EF>> make_scope_fail(EF&& exitFunction)
  99. {
  100. return scope_fail<std::decay_t<EF>>{std::forward<EF>(exitFunction)};
  101. }
  102. }