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.

290 line
9.6KB

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