Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

220 lines
7.8KB

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