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.

224 lines
8.3KB

  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, 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 swap(unique_resource& other) noexcept(std::is_nothrow_swappable_v<R>
  75. && std::is_nothrow_swappable_v<D>
  76. && std::is_nothrow_swappable_v<bool>)
  77. {
  78. using std::swap;
  79. swap(m_resource.get(), other.m_resource.get());
  80. swap(m_deleter.get(), other.m_deleter.get());
  81. swap(m_execute_on_destruction, other.m_execute_on_destruction);
  82. }
  83. void reset()
  84. {
  85. if( m_execute_on_destruction == true )
  86. {
  87. m_execute_on_destruction = false;
  88. get_deleter()(m_resource.get());
  89. }
  90. }
  91. template<class RR>
  92. void reset(RR&& r)
  93. {
  94. auto se = scope_exit{[this, &r] { get_deleter()(r); }};
  95. reset();
  96. m_resource.reset(std::forward<RR>(r));
  97. m_execute_on_destruction = true;
  98. se.release();
  99. }
  100. void release()
  101. {
  102. m_execute_on_destruction = false;
  103. }
  104. decltype(auto) get() const noexcept
  105. {
  106. return m_resource.get();
  107. }
  108. template<class RR = R,
  109. std::enable_if_t<std::is_pointer_v<RR>
  110. && (std::is_class_v<std::remove_pointer_t<RR>>
  111. || std::is_union_v<std::remove_pointer_t<RR>>), int> = 0
  112. >
  113. RR operator->() const noexcept
  114. {
  115. return m_resource.get();
  116. }
  117. template<class RR = R,
  118. std::enable_if_t<( std::is_pointer_v<RR>
  119. && !std::is_void_v<std::remove_pointer_t<RR>>), int> = 0>
  120. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  121. {
  122. return *get();
  123. }
  124. const D& get_deleter() const noexcept
  125. {
  126. return m_deleter.get();
  127. }
  128. template<class RR = R, class DD = D,
  129. std::enable_if_t<(std::is_nothrow_move_assignable_v<RR>
  130. || std::is_nothrow_copy_assignable_v<RR>)
  131. && (std::is_nothrow_move_assignable_v<DD>
  132. || std::is_nothrow_copy_assignable_v<DD>), int> = 0
  133. >
  134. unique_resource& operator=(unique_resource&& other)
  135. {
  136. if( this != &other )
  137. {
  138. reset();
  139. if constexpr( std::is_nothrow_move_assignable_v<R> == true )
  140. {
  141. m_deleter.reset(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()));
  142. m_resource.reset(std::forward<RR>(other.m_resource.get()));
  143. }
  144. else if constexpr( std::is_nothrow_move_assignable_v<D> == true )
  145. {
  146. m_resource.reset(detail::forward_if_nothrow_move_constructible(other.m_resource.get()));
  147. m_deleter.reset(std::forward<DD>(other.m_deleter.get()));
  148. }
  149. else
  150. {
  151. m_resource = other.m_resource;
  152. m_deleter = other.m_deleter;
  153. }
  154. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  155. }
  156. return *this;
  157. }
  158. unique_resource& operator=(const unique_resource&) = delete;
  159. private:
  160. detail::Wrapper<R> m_resource;
  161. detail::Wrapper<D> m_deleter;
  162. bool m_execute_on_destruction;
  163. };
  164. template<class R, class D>
  165. unique_resource(R&& r, D&& d) -> unique_resource<std::decay_t<R>, std::decay_t<D>>;
  166. template<class R, class D>
  167. unique_resource(std::reference_wrapper<R> r, D&& d) -> unique_resource<R&, std::decay_t<D>>;
  168. template<class R, class D, class S = R>
  169. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  170. noexcept(std::is_nothrow_constructible_v<std::decay_t<R>, R>
  171. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  172. {
  173. const bool must_release{r == invalid};
  174. auto ur = unique_resource{r, d};
  175. if( must_release == true )
  176. {
  177. ur.release();
  178. }
  179. return ur;
  180. }
  181. }