Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

327 Zeilen
12KB

  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_lvalue_reference<RR>::value)
  106. //&& std::is_nothrow_constructible<R, RR>::value, int> = 0,
  107. //std::enable_if_t<(!std::is_lvalue_reference<DD>::value)
  108. //&& std::is_nothrow_constructible<D, DD>::value, int> = 0,
  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); })), // TODO: Call Deleter
  117. m_deleter(std::forward<DD>(d), make_scope_exit([this, &d] { d(get()); })), // TODO: Call Deleter
  118. m_execute_on_destruction(true)
  119. {
  120. }
  121. //template<class RR, class DD,
  122. //std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
  123. //std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  124. //&& (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  125. //std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  126. //std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  127. //>
  128. //explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
  129. //&& std::is_nothrow_constructible<D, DD>::value)
  130. //try : m_resource(std::forward<RR>(r), make_scope_exit([]() { [> TODO: Delete value <] })),
  131. //m_deleter(d),
  132. //m_execute_on_destruction(true)
  133. //{
  134. //}
  135. //catch( ... )
  136. //{
  137. //d(r);
  138. //}
  139. //template<class TR = R, std::enable_if_t<std::is_nothrow_move_constructible<TR>::value, int> = 0>
  140. //unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  141. //&& std::is_nothrow_move_constructible<D>::value)
  142. //: m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  143. //m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  144. //m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  145. //{
  146. //}
  147. //template<class TR = R, std::enable_if_t<!std::is_nothrow_move_constructible<TR>::value, int> = 0>
  148. //unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  149. //&& std::is_nothrow_move_constructible<D>::value)
  150. //try : m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  151. //m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  152. //m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  153. //{
  154. //}
  155. //catch( ... )
  156. //{
  157. //other.get_deleter()(other.m_resource.get());
  158. //other.release();
  159. //throw;
  160. //}
  161. // FIXME: Needs update
  162. unique_resource(unique_resource&&) = default;
  163. unique_resource(const unique_resource&) = delete;
  164. ~unique_resource()
  165. {
  166. reset();
  167. }
  168. void reset()
  169. {
  170. if( m_execute_on_destruction == true )
  171. {
  172. m_execute_on_destruction = false;
  173. get_deleter()(m_resource.get());
  174. }
  175. }
  176. template<class RR>
  177. void reset(RR&& r)
  178. {
  179. auto se = make_scope_exit([this, &r] { get_deleter()(r); });
  180. reset();
  181. m_resource.reset(std::forward<RR>(r));
  182. m_execute_on_destruction = true;
  183. se.release();
  184. }
  185. void release()
  186. {
  187. m_execute_on_destruction = false;
  188. }
  189. const R& get() const noexcept
  190. {
  191. return m_resource.get();
  192. }
  193. template<class RR = R,
  194. std::enable_if_t<std::is_pointer<RR>::value
  195. && ( std::is_class<std::remove_pointer_t<RR>>::value
  196. || std::is_union<std::remove_pointer_t<RR>>::value ), int> = 0
  197. >
  198. RR operator->() const noexcept
  199. {
  200. return m_resource.get();
  201. }
  202. template<class RR = R,
  203. std::enable_if_t<( std::is_pointer<RR>::value && !std::is_void<std::remove_pointer_t<RR>>::value), int> = 0>
  204. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  205. {
  206. return *get();
  207. }
  208. const D& get_deleter() const noexcept
  209. {
  210. return m_deleter.get();
  211. }
  212. //template<class RR = R, class DD = D,
  213. //std::enable_if_t<(std::is_nothrow_move_assignable<RR>::value || std::is_nothrow_copy_assignable<RR>::value)
  214. //&& (std::is_nothrow_copy_assignable<DD>::value || std::is_nothrow_copy_assignable<DD>::value), int> = 0
  215. //>
  216. //unique_resource& operator=(unique_resource&& other)
  217. //{
  218. //if( this != &other )
  219. //{
  220. //reset();
  221. //m_resource = std::forward<RR>(other.m_resource);
  222. //m_deleter = std::forward<DD>(other.m_deleter);
  223. //m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  224. //}
  225. //return *this;
  226. //}
  227. // FIXME: Needs update
  228. unique_resource& operator=(unique_resource&&) = default;
  229. unique_resource& operator=(const unique_resource&) = delete;
  230. private:
  231. Wrapper<R> m_resource;
  232. Wrapper<D> m_deleter;
  233. bool m_execute_on_destruction;
  234. };
  235. template<class R, class D>
  236. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
  237. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  238. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  239. {
  240. return unique_resource<std::decay_t<R>, std::decay_t<D>>{std::forward<R>(r), std::forward<D>(d)};
  241. }
  242. template<class R, class D>
  243. unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
  244. noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  245. {
  246. return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
  247. }
  248. template<class R, class D, class S = R>
  249. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  250. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  251. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  252. {
  253. const bool mustRelease{r == invalid};
  254. auto ur = make_unique_resource(r, d);
  255. if( mustRelease == true )
  256. {
  257. ur.release();
  258. }
  259. return ur;
  260. }
  261. }