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.

119 lines
3.5KB

  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 Deleter>
  27. class scope_success
  28. {
  29. public:
  30. template<class D,
  31. std::enable_if_t<std::is_constructible<Deleter, D>::value, int> = 0,
  32. std::enable_if_t<(!std::is_lvalue_reference<D>::value)
  33. && std::is_nothrow_constructible<Deleter, D>::value, int> = 0
  34. >
  35. explicit scope_success(D&& deleter) : m_deleter(std::move(deleter)),
  36. m_execute_on_destruction(true)
  37. {
  38. }
  39. template<class D,
  40. std::enable_if_t<std::is_constructible<Deleter, D>::value, int> = 0,
  41. std::enable_if_t<std::is_lvalue_reference<D>::value, int> = 0
  42. >
  43. explicit scope_success(D&& deleter) try : m_deleter(deleter),
  44. m_execute_on_destruction(true)
  45. {
  46. }
  47. catch( ... )
  48. {
  49. throw;
  50. }
  51. scope_success(const scope_success&) = delete;
  52. template<class T = Deleter,
  53. std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0
  54. >
  55. scope_success(scope_success&& other) : m_deleter(std::move(other.m_deleter)),
  56. m_execute_on_destruction(other.m_execute_on_destruction),
  57. m_uncaught_on_creation(uncaught_exceptions())
  58. {
  59. other.release();
  60. }
  61. template<class T = Deleter,
  62. std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0
  63. >
  64. scope_success(scope_success&& other) : m_deleter(other.m_deleter),
  65. m_execute_on_destruction(other.m_execute_on_destruction)
  66. {
  67. other.release();
  68. }
  69. ~scope_success() noexcept(noexcept(std::declval<Deleter>()))
  70. {
  71. if( (m_execute_on_destruction == true) && ( uncaught_exceptions() <= m_uncaught_on_creation ) )
  72. {
  73. m_deleter();
  74. }
  75. }
  76. void release() noexcept
  77. {
  78. m_execute_on_destruction = false;
  79. }
  80. scope_success& operator=(const scope_success&) = delete;
  81. scope_success& operator=(scope_success&&) = delete;
  82. private:
  83. int uncaught_exceptions() const noexcept
  84. {
  85. return ( std::uncaught_exception() == true ? 1 : 0 );
  86. }
  87. Deleter m_deleter;
  88. bool m_execute_on_destruction;
  89. int m_uncaught_on_creation;
  90. };
  91. template<class Deleter>
  92. scope_success<std::decay_t<Deleter>> make_scope_success(Deleter&& deleter) noexcept
  93. {
  94. return scope_success<std::decay_t<Deleter>>{std::forward<Deleter>(deleter)};
  95. }
  96. }