Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

250 Zeilen
9.9KB

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