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.

206 lines
7.2KB

  1. /*
  2. * Scope Guard
  3. * Copyright (C) 2017-2018 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 "scope_exit.h"
  22. #include "detail/wrapper.h"
  23. #include <utility>
  24. #include <type_traits>
  25. namespace sr
  26. {
  27. template<class R, class D>
  28. class unique_resource
  29. {
  30. public:
  31. template<class RR, class DD,
  32. std::enable_if_t<(std::is_constructible_v<R ,RR> && std::is_constructible_v<D, DD>
  33. && (std::is_nothrow_constructible_v<R, RR> || std::is_constructible_v<R, RR&>)
  34. && (std::is_nothrow_constructible_v<D, DD> || std::is_constructible_v<D, DD&>)), int> = 0
  35. >
  36. explicit unique_resource(RR&& r, DD&& d) noexcept((std::is_nothrow_constructible_v<R, RR> || std::is_nothrow_constructible_v<R, RR&>)
  37. && (std::is_nothrow_constructible_v<D, DD> || std::is_nothrow_constructible_v<D, DD&>))
  38. : m_resource(std::forward<RR>(r), scope_exit{[&r, &d] { d(r); }}),
  39. m_deleter(std::forward<DD>(d), scope_exit{[this, &d] { d(get()); }}),
  40. m_execute_on_destruction(true)
  41. {
  42. }
  43. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R>
  44. && std::is_nothrow_move_constructible_v<D>)
  45. : m_resource(std::move_if_noexcept(other.m_resource.get()), scope_exit{[] { }}),
  46. m_deleter(std::move_if_noexcept(other.m_deleter.get()), scope_exit{[&other] {
  47. other.get_deleter()(other.m_resource.get());
  48. other.release(); }}),
  49. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  50. {
  51. }
  52. unique_resource(const unique_resource&) = delete;
  53. ~unique_resource()
  54. {
  55. reset();
  56. }
  57. void reset() noexcept
  58. {
  59. if( m_execute_on_destruction == true )
  60. {
  61. m_execute_on_destruction = false;
  62. get_deleter()(m_resource.get());
  63. }
  64. }
  65. template<class RR>
  66. void reset(RR&& r)
  67. {
  68. reset();
  69. using R1 = typename detail::Wrapper<R>::type;
  70. auto se = scope_exit{[this, &r] { get_deleter()(r); }};
  71. if constexpr( std::is_nothrow_assignable_v<R1&, RR> == true )
  72. {
  73. m_resource.reset(std::forward<RR>(r));
  74. }
  75. else
  76. {
  77. m_resource.reset(std::as_const(r));
  78. }
  79. m_execute_on_destruction = true;
  80. se.release();
  81. }
  82. void release() noexcept
  83. {
  84. m_execute_on_destruction = false;
  85. }
  86. const R& get() const noexcept
  87. {
  88. return m_resource.get();
  89. }
  90. template<class RR = R, std::enable_if_t<std::is_pointer_v<RR>, int> = 0>
  91. RR operator->() const noexcept
  92. {
  93. return m_resource.get();
  94. }
  95. template<class RR = R,
  96. std::enable_if_t<( std::is_pointer_v<RR>
  97. && !std::is_void_v<std::remove_pointer_t<RR>>), int> = 0>
  98. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  99. {
  100. return *get();
  101. }
  102. const D& get_deleter() const noexcept
  103. {
  104. return m_deleter.get();
  105. }
  106. template<class RR = R, class DD = D,
  107. std::enable_if_t<(std::is_nothrow_move_assignable_v<RR>
  108. || std::is_nothrow_copy_assignable_v<RR>)
  109. && (std::is_nothrow_move_assignable_v<DD>
  110. || std::is_nothrow_copy_assignable_v<DD>), int> = 0
  111. >
  112. unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v<R&, R>
  113. && std::is_nothrow_assignable_v<D&, D>)
  114. {
  115. if( this != &other )
  116. {
  117. reset();
  118. if constexpr( std::is_nothrow_move_assignable_v<RR> == true )
  119. {
  120. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  121. {
  122. m_resource.reset(std::move(other.m_resource));
  123. m_deleter.reset(std::move(other.m_deleter));
  124. }
  125. else
  126. {
  127. m_deleter.reset(other.m_deleter);
  128. m_resource.reset(std::move(other.m_resource));
  129. }
  130. }
  131. else
  132. {
  133. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  134. {
  135. m_resource.reset(other.m_resource);
  136. m_deleter.reset(std::move(other.m_deleter));
  137. }
  138. else
  139. {
  140. m_resource.reset(other.m_resource);
  141. m_deleter.reset(other.m_deleter);
  142. }
  143. }
  144. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  145. }
  146. return *this;
  147. }
  148. unique_resource& operator=(const unique_resource&) = delete;
  149. private:
  150. detail::Wrapper<R> m_resource;
  151. detail::Wrapper<D> m_deleter;
  152. bool m_execute_on_destruction;
  153. };
  154. template<class R, class D>
  155. unique_resource(R, D) -> unique_resource<R, D>;
  156. template<class R, class D, class S = std::decay_t<R>>
  157. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  158. noexcept(std::is_nothrow_constructible_v<std::decay_t<R>, R>
  159. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  160. {
  161. const bool must_release{r == invalid};
  162. unique_resource<std::decay_t<R>, std::decay_t<D>> ur{std::forward<R>(r), std::forward<D>(d)};
  163. if( must_release == true )
  164. {
  165. ur.release();
  166. }
  167. return ur;
  168. }
  169. }