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.

238 line
9.1KB

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