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.

247 lines
9.8KB

  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 <utility>
  23. #include <type_traits>
  24. namespace sr
  25. {
  26. template<class T, class TT>
  27. using is_ntmocp_constructible = std::conditional_t<std::is_reference<TT>::value || !std::is_nothrow_move_constructible<TT>::value,
  28. typename std::is_constructible<T, const TT&>::type,
  29. typename std::is_constructible<T, TT>::type>;
  30. template<class T, class TT>
  31. constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible<T, TT>::value;
  32. template<class T,
  33. class U = std::conditional_t<(!std::is_nothrow_move_assignable<T>::value
  34. && std::is_copy_assignable<T>::value),
  35. const T&,
  36. T &&>>
  37. constexpr U move_assign_if_noexcept(T& value) noexcept
  38. {
  39. return std::move(value);
  40. }
  41. template<class T, class U = std::conditional_t<std::is_nothrow_move_constructible<T>::value, T&&, const T&>>
  42. constexpr U forward_if_nothrow_move_constructible(T&& value)
  43. {
  44. return std::forward<T>(value);
  45. }
  46. template<class R, class D>
  47. class unique_resource
  48. {
  49. public:
  50. template<class RR, class DD,
  51. std::enable_if_t<(!std::is_lvalue_reference<RR>::value)
  52. && std::is_nothrow_constructible<R, RR>::value, int> = 0,
  53. std::enable_if_t<(!std::is_lvalue_reference<DD>::value)
  54. && std::is_nothrow_constructible<D, DD>::value, int> = 0,
  55. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  56. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  57. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  58. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  59. >
  60. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R,RR>::value
  61. && std::is_nothrow_constructible<D, DD>::value)
  62. : m_resource(std::move(r)),
  63. m_deleter(std::move(d)),
  64. m_execute_on_destruction(true)
  65. {
  66. }
  67. template<class RR, class DD,
  68. std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
  69. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  70. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  71. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  72. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  73. >
  74. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
  75. && std::is_nothrow_constructible<D, DD>::value)
  76. try : m_resource(r),
  77. m_deleter(d),
  78. m_execute_on_destruction(true)
  79. {
  80. }
  81. catch( ... )
  82. {
  83. d(r);
  84. }
  85. template<class TR = R, std::enable_if_t<std::is_nothrow_move_constructible<TR>::value, int> = 0>
  86. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  87. && std::is_nothrow_move_constructible<D>::value)
  88. : m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  89. m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  90. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  91. {
  92. }
  93. template<class TR = R, std::enable_if_t<!std::is_nothrow_move_constructible<TR>::value, int> = 0>
  94. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  95. && std::is_nothrow_move_constructible<D>::value)
  96. try : m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  97. m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  98. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  99. {
  100. }
  101. catch( ... )
  102. {
  103. other.get_deleter()(other.m_resource);
  104. other.release();
  105. throw;
  106. }
  107. unique_resource(const unique_resource&) = delete;
  108. ~unique_resource()
  109. {
  110. reset();
  111. }
  112. void reset()
  113. {
  114. if( m_execute_on_destruction == true )
  115. {
  116. m_execute_on_destruction = false;
  117. get_deleter()(m_resource);
  118. }
  119. }
  120. template<class RR>
  121. void reset(RR&& r)
  122. {
  123. auto se = make_scope_exit([this, &r] { get_deleter()(r); });
  124. reset();
  125. m_resource = move_assign_if_noexcept(r);
  126. m_execute_on_destruction = true;
  127. se.release();
  128. }
  129. void release()
  130. {
  131. m_execute_on_destruction = false;
  132. }
  133. const R& get() const noexcept
  134. {
  135. return m_resource;
  136. }
  137. template<class RR = R,
  138. std::enable_if_t<std::is_pointer<RR>::value
  139. && ( std::is_class<std::remove_pointer_t<RR>>::value
  140. || std::is_union<std::remove_pointer_t<RR>>::value ), int> = 0
  141. >
  142. RR operator->() const noexcept
  143. {
  144. return m_resource;
  145. }
  146. template<class RR = R,
  147. std::enable_if_t<( std::is_pointer<RR>::value && !std::is_void<std::remove_pointer_t<RR>>::value), int> = 0>
  148. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  149. {
  150. return *get();
  151. }
  152. const D& get_deleter() const noexcept
  153. {
  154. return m_deleter;
  155. }
  156. template<class RR = R, class DD = D,
  157. std::enable_if_t<(std::is_nothrow_move_assignable<RR>::value || std::is_nothrow_copy_assignable<RR>::value)
  158. && (std::is_nothrow_copy_assignable<DD>::value || std::is_nothrow_copy_assignable<DD>::value), int> = 0
  159. >
  160. unique_resource& operator=(unique_resource&& other)
  161. {
  162. if( this != &other )
  163. {
  164. reset();
  165. m_resource = std::forward<RR>(other.m_resource);
  166. m_deleter = std::forward<DD>(other.m_deleter);
  167. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  168. }
  169. return *this;
  170. }
  171. unique_resource& operator=(const unique_resource&) = delete;
  172. private:
  173. R m_resource;
  174. D m_deleter;
  175. bool m_execute_on_destruction;
  176. };
  177. template<class R, class D>
  178. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
  179. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  180. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  181. {
  182. return unique_resource<std::decay_t<R>, std::decay_t<D>>{std::forward<R>(r), std::forward<D>(d)};
  183. }
  184. template<class R, class D>
  185. unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
  186. noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  187. {
  188. return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
  189. }
  190. template<class R, class D, class S = R>
  191. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  192. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  193. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  194. {
  195. const bool mustRelease{r == invalid};
  196. auto ur = make_unique_resource(r, d);
  197. if( mustRelease == true )
  198. {
  199. ur.release();
  200. }
  201. return ur;
  202. }
  203. }