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.

scope_success.h 4.0KB

7 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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_success
  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_success(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_success(EFP&& exitFunction) : m_exitFunction(exitFunction),
  45. m_execute_on_destruction(true),
  46. m_uncaught_on_creation(uncaught_exceptions())
  47. {
  48. }
  49. scope_success(const scope_success&) = delete;
  50. template<class T = EF,
  51. std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0
  52. >
  53. scope_success(scope_success&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
  54. : m_exitFunction(std::move(other.m_exitFunction)),
  55. m_execute_on_destruction(other.m_execute_on_destruction),
  56. m_uncaught_on_creation(uncaught_exceptions())
  57. {
  58. other.release();
  59. }
  60. template<class T = EF,
  61. std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0
  62. >
  63. scope_success(scope_success&& other) noexcept(std::is_nothrow_move_constructible<T>::value || std::is_nothrow_copy_constructible<T>::value)
  64. : m_exitFunction(other.m_exitFunction),
  65. m_execute_on_destruction(other.m_execute_on_destruction),
  66. m_uncaught_on_creation(other.m_uncaught_on_creation)
  67. {
  68. other.release();
  69. }
  70. ~scope_success() noexcept(noexcept(std::declval<EF>()()))
  71. {
  72. if( ( m_execute_on_destruction == true ) && ( uncaught_exceptions() <= m_uncaught_on_creation ) )
  73. {
  74. m_exitFunction();
  75. }
  76. }
  77. void release() noexcept
  78. {
  79. m_execute_on_destruction = false;
  80. }
  81. scope_success& operator=(const scope_success&) = delete;
  82. scope_success& operator=(scope_success&&) = delete;
  83. private:
  84. int uncaught_exceptions() const noexcept
  85. {
  86. return ( std::uncaught_exception() == true ? 1 : 0 );
  87. }
  88. EF m_exitFunction;
  89. bool m_execute_on_destruction;
  90. int m_uncaught_on_creation;
  91. };
  92. template<class EF>
  93. scope_success<std::decay_t<EF>> make_scope_success(EF&& exitFunction)
  94. {
  95. return scope_success<std::decay_t<EF>>{std::forward<EF>(exitFunction)};
  96. }
  97. }