Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

230 lines
8.3KB

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