No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

294 líneas
9.8KB

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