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.

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