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.

305 lines
10KB

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