您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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