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.

317 lines
11KB

  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. #include <functional>
  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<TT>::value
  31. || !std::is_nothrow_move_constructible<TT>::value,
  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<T>::value
  38. && std::is_nothrow_move_assignable<T>::value;
  39. template<class T, class U = std::conditional_t<std::is_nothrow_move_constructible<T>::value, T&&, const T&>>
  40. constexpr U forward_if_nothrow_move_constructible(T&& value) noexcept
  41. {
  42. return std::forward<T>(value);
  43. }
  44. template<class T>
  45. struct Wrapper
  46. {
  47. template<class TT, class G, std::enable_if_t<std::is_constructible<T, TT>::value, int> = 0>
  48. explicit Wrapper(TT&& value, G&& g) noexcept(noexcept(Wrapper{value})) : Wrapper(value)
  49. {
  50. g.release();
  51. }
  52. T& get() noexcept
  53. {
  54. return m_value;
  55. }
  56. const T& get() const noexcept
  57. {
  58. return m_value;
  59. }
  60. void reset(T&& newValue) noexcept(std::is_nothrow_assignable<T, decltype(std::move_if_noexcept(newValue))>::value)
  61. {
  62. m_value = std::move_if_noexcept(newValue);
  63. }
  64. void reset(const T& newValue) noexcept(std::is_nothrow_assignable<T, const T&>::value)
  65. {
  66. m_value = newValue;
  67. }
  68. private:
  69. Wrapper(const T& value) noexcept(noexcept(T{value})) : m_value(value)
  70. {
  71. }
  72. Wrapper(T&& value) noexcept(noexcept(T{std::move_if_noexcept(value)})) : m_value(std::move_if_noexcept(value))
  73. {
  74. }
  75. T m_value;
  76. };
  77. template<class T>
  78. struct Wrapper<T&>
  79. {
  80. template<class TT, class G, std::enable_if_t<std::is_convertible<TT, T&>::value, int> = 0>
  81. explicit Wrapper(TT&& value, G&& g) noexcept(noexcept(static_cast<T&>(value))) : m_value(static_cast<T&>(value))
  82. {
  83. g.release();
  84. }
  85. T& get() noexcept
  86. {
  87. return m_value.get();
  88. }
  89. const T& get() const noexcept
  90. {
  91. return m_value.get();
  92. }
  93. void reset(T& newValue) noexcept
  94. {
  95. m_value = std::ref(newValue);
  96. }
  97. private:
  98. std::reference_wrapper<T> m_value;
  99. };
  100. }
  101. template<class R, class D>
  102. class unique_resource
  103. {
  104. public:
  105. template<class RR, class DD,
  106. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  107. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  108. std::enable_if_t<detail::is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  109. std::enable_if_t<detail::is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  110. >
  111. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
  112. && std::is_nothrow_constructible<D, DD>::value)
  113. : m_resource(std::forward<RR>(r), make_scope_exit([&r, &d] { d(r); })),
  114. m_deleter(std::forward<DD>(d), make_scope_exit([this, &d] { d(get()); })),
  115. m_execute_on_destruction(true)
  116. {
  117. }
  118. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  119. && std::is_nothrow_move_constructible<D>::value)
  120. : m_resource(detail::forward_if_nothrow_move_constructible(other.m_resource.get()), make_scope_exit([] { })),
  121. m_deleter(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()),
  122. make_scope_exit([&other] {
  123. other.get_deleter()(other.m_resource.get());
  124. other.release(); })),
  125. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  126. {
  127. }
  128. unique_resource(const unique_resource&) = delete;
  129. ~unique_resource()
  130. {
  131. reset();
  132. }
  133. void swap(unique_resource& other) noexcept(detail::is_nothrow_swappable_v<R>
  134. && detail::is_nothrow_swappable_v<D>
  135. && detail::is_nothrow_swappable_v<bool>)
  136. {
  137. using std::swap;
  138. swap(m_resource.get(), other.m_resource.get());
  139. swap(m_deleter.get(), other.m_deleter.get());
  140. swap(m_execute_on_destruction, other.m_execute_on_destruction);
  141. }
  142. void reset()
  143. {
  144. if( m_execute_on_destruction == true )
  145. {
  146. m_execute_on_destruction = false;
  147. get_deleter()(m_resource.get());
  148. }
  149. }
  150. template<class RR>
  151. void reset(RR&& r)
  152. {
  153. auto se = make_scope_exit([this, &r] { get_deleter()(r); });
  154. reset();
  155. m_resource.reset(std::forward<RR>(r));
  156. m_execute_on_destruction = true;
  157. se.release();
  158. }
  159. void release()
  160. {
  161. m_execute_on_destruction = false;
  162. }
  163. decltype(auto) get() const noexcept
  164. {
  165. return m_resource.get();
  166. }
  167. template<class RR = R,
  168. std::enable_if_t<std::is_pointer<RR>::value
  169. && ( std::is_class<std::remove_pointer_t<RR>>::value
  170. || std::is_union<std::remove_pointer_t<RR>>::value ), int> = 0
  171. >
  172. RR operator->() const noexcept
  173. {
  174. return m_resource.get();
  175. }
  176. template<class RR = R,
  177. std::enable_if_t<( std::is_pointer<RR>::value
  178. && !std::is_void<std::remove_pointer_t<RR>>::value), int> = 0>
  179. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  180. {
  181. return *get();
  182. }
  183. const D& get_deleter() const noexcept
  184. {
  185. return m_deleter.get();
  186. }
  187. template<class RR = R, class DD = D,
  188. std::enable_if_t<(std::is_nothrow_move_assignable<RR>::value
  189. || std::is_nothrow_copy_assignable<RR>::value)
  190. && (std::is_nothrow_move_assignable<DD>::value
  191. || std::is_nothrow_copy_assignable<DD>::value), int> = 0
  192. >
  193. unique_resource& operator=(unique_resource&& other)
  194. {
  195. if( this != &other )
  196. {
  197. reset();
  198. if( std::is_nothrow_move_assignable<R>::value == true )
  199. {
  200. m_deleter.reset(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()));
  201. m_resource.reset(std::forward<RR>(other.m_resource.get()));
  202. }
  203. else if( std::is_nothrow_move_assignable<D>::value == true )
  204. {
  205. m_resource.reset(detail::forward_if_nothrow_move_constructible(other.m_resource.get()));
  206. m_deleter.reset(std::forward<DD>(other.m_deleter.get()));
  207. }
  208. else
  209. {
  210. m_resource = other.m_resource;
  211. m_deleter = other.m_deleter;
  212. }
  213. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  214. }
  215. return *this;
  216. }
  217. unique_resource& operator=(const unique_resource&) = delete;
  218. private:
  219. detail::Wrapper<R> m_resource;
  220. detail::Wrapper<D> m_deleter;
  221. bool m_execute_on_destruction;
  222. };
  223. template<class R, class D>
  224. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
  225. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  226. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  227. {
  228. return unique_resource<std::decay_t<R>, std::decay_t<D>>{std::forward<R>(r), std::forward<D>(d)};
  229. }
  230. template<class R, class D>
  231. unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
  232. noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  233. {
  234. return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
  235. }
  236. template<class R, class D, class S = R>
  237. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  238. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  239. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  240. {
  241. const bool mustRelease{r == invalid};
  242. auto ur = make_unique_resource(r, d);
  243. if( mustRelease == true )
  244. {
  245. ur.release();
  246. }
  247. return ur;
  248. }
  249. }