Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

286 lines
9.2KB

  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. namespace sr
  25. {
  26. template<class T, class TT>
  27. using is_ntmocp_constructible = std::conditional_t<std::is_reference<TT>::value || !std::is_nothrow_move_constructible<TT>::value,
  28. typename std::is_constructible<T, const TT&>::type,
  29. typename std::is_constructible<T, TT>::type>;
  30. template<class T, class TT>
  31. constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible<T, TT>::value;
  32. template<class T,
  33. class U = std::conditional_t<(!std::is_nothrow_move_assignable<T>::value
  34. && std::is_copy_assignable<T>::value),
  35. const T&,
  36. T &&>>
  37. constexpr U move_assign_if_noexcept(T& value) noexcept
  38. {
  39. return std::move(value);
  40. }
  41. template<class T, class U = std::conditional_t<std::is_nothrow_move_constructible<T>::value, T&&, const T&>>
  42. constexpr U forward_if_nothrow_move_constructible(T&& value)
  43. {
  44. return std::forward<T>(value);
  45. }
  46. // TODO: Fix old-style casts
  47. // TODO: make class and fix noexcept's
  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) : Wrapper((T&&) value)
  53. {
  54. g.release();
  55. }
  56. T& get()
  57. {
  58. return m_value;
  59. }
  60. const T& get() const
  61. {
  62. return m_value;
  63. }
  64. void reset(T&& newValue)
  65. {
  66. m_value = move_assign_if_noexcept(newValue);
  67. }
  68. void reset(const T& newValue)
  69. {
  70. m_value = newValue;
  71. }
  72. private:
  73. Wrapper(const T& value) : m_value(value)
  74. {
  75. }
  76. Wrapper(T&& 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) : m_value((T&) value)
  86. {
  87. g.release();
  88. }
  89. T& get()
  90. {
  91. return m_value.get();
  92. }
  93. const T& get() const
  94. {
  95. return m_value.get();
  96. }
  97. private:
  98. std::reference_wrapper<T> m_value;
  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<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  108. std::enable_if_t<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(forward_if_nothrow_move_constructible(other.m_resource.get()), make_scope_exit([] { })),
  120. m_deleter(forward_if_nothrow_move_constructible(other.m_deleter.get()), make_scope_exit([&other] {
  121. other.get_deleter()(other.m_resource.get());
  122. other.release(); })),
  123. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  124. {
  125. }
  126. unique_resource(const unique_resource&) = delete;
  127. ~unique_resource()
  128. {
  129. reset();
  130. }
  131. void reset()
  132. {
  133. if( m_execute_on_destruction == true )
  134. {
  135. m_execute_on_destruction = false;
  136. get_deleter()(m_resource.get());
  137. }
  138. }
  139. template<class RR>
  140. void reset(RR&& r)
  141. {
  142. auto se = make_scope_exit([this, &r] { get_deleter()(r); });
  143. reset();
  144. m_resource.reset(std::forward<RR>(r));
  145. m_execute_on_destruction = true;
  146. se.release();
  147. }
  148. void release()
  149. {
  150. m_execute_on_destruction = false;
  151. }
  152. decltype(auto) get() const noexcept
  153. {
  154. return m_resource.get();
  155. }
  156. template<class RR = R,
  157. std::enable_if_t<std::is_pointer<RR>::value
  158. && ( std::is_class<std::remove_pointer_t<RR>>::value
  159. || std::is_union<std::remove_pointer_t<RR>>::value ), int> = 0
  160. >
  161. RR operator->() const noexcept
  162. {
  163. return m_resource.get();
  164. }
  165. template<class RR = R,
  166. std::enable_if_t<( std::is_pointer<RR>::value && !std::is_void<std::remove_pointer_t<RR>>::value), int> = 0>
  167. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  168. {
  169. return *get();
  170. }
  171. const D& get_deleter() const noexcept
  172. {
  173. return m_deleter.get();
  174. }
  175. template<class RR = R, class DD = D,
  176. std::enable_if_t<(std::is_nothrow_move_assignable<RR>::value || std::is_nothrow_copy_assignable<RR>::value)
  177. && (std::is_nothrow_copy_assignable<DD>::value || std::is_nothrow_copy_assignable<DD>::value), int> = 0
  178. >
  179. unique_resource& operator=(unique_resource&& other)
  180. {
  181. if( this != &other )
  182. {
  183. reset();
  184. m_resource = std::move(other.m_resource);
  185. m_deleter = std::move(other.m_deleter);
  186. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  187. }
  188. return *this;
  189. }
  190. unique_resource& operator=(const unique_resource&) = delete;
  191. private:
  192. Wrapper<R> m_resource;
  193. Wrapper<D> m_deleter;
  194. bool m_execute_on_destruction;
  195. };
  196. template<class R, class D>
  197. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
  198. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  199. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  200. {
  201. return unique_resource<std::decay_t<R>, std::decay_t<D>>{std::forward<R>(r), std::forward<D>(d)};
  202. }
  203. template<class R, class D>
  204. unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
  205. noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  206. {
  207. return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
  208. }
  209. template<class R, class D, class S = R>
  210. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  211. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  212. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  213. {
  214. const bool mustRelease{r == invalid};
  215. auto ur = make_unique_resource(r, d);
  216. if( mustRelease == true )
  217. {
  218. ur.release();
  219. }
  220. return ur;
  221. }
  222. }