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.

331 lines
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. template<class T, class U>
  47. constexpr T&& cast(U&& u)
  48. {
  49. return static_cast<T&&>(const_cast<T&&>(u));
  50. }
  51. // TODO: make class and fix noexcept's
  52. template<class T>
  53. struct Wrapper
  54. {
  55. template<class TT, class G, std::enable_if_t<std::is_constructible<T, TT>::value, int> = 0>
  56. explicit Wrapper(TT&& value, G&& g) : Wrapper(cast<T&&>(value))
  57. {
  58. g.release();
  59. }
  60. T& get()
  61. {
  62. return m_value;
  63. }
  64. const T& get() const
  65. {
  66. return m_value;
  67. }
  68. void reset(T&& newValue)
  69. {
  70. m_value = move_assign_if_noexcept(newValue);
  71. }
  72. void reset(const T& newValue)
  73. {
  74. m_value = newValue;
  75. }
  76. private:
  77. Wrapper(const T& value) : m_value(value)
  78. {
  79. }
  80. Wrapper(T&& value) : m_value(std::move_if_noexcept(value))
  81. {
  82. }
  83. T m_value;
  84. };
  85. template<class T>
  86. struct Wrapper<T&>
  87. {
  88. template<class TT, class G, std::enable_if_t<std::is_convertible<TT, T&>::value, int> = 0>
  89. explicit Wrapper(TT&& value, G&& g) : m_value(cast<T&>(value))
  90. {
  91. g.release();
  92. }
  93. T& get()
  94. {
  95. return m_value.get();
  96. }
  97. const T& get() const
  98. {
  99. return m_value.get();
  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_lvalue_reference<RR>::value)
  110. && std::is_nothrow_constructible<R, RR>::value, int> = 0,
  111. std::enable_if_t<(!std::is_lvalue_reference<DD>::value)
  112. && std::is_nothrow_constructible<D, DD>::value, int> = 0,
  113. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  114. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  115. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  116. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  117. >
  118. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
  119. && std::is_nothrow_constructible<D, DD>::value)
  120. : m_resource(std::forward<RR>(r), make_scope_exit([]() { })),
  121. m_deleter(std::move(d)),
  122. m_execute_on_destruction(true)
  123. {
  124. }
  125. template<class RR, class DD,
  126. std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
  127. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  128. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  129. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  130. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  131. >
  132. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
  133. && std::is_nothrow_constructible<D, DD>::value)
  134. try : m_resource(std::forward<RR>(r), make_scope_exit([]() { /* TODO: Delete value */ })),
  135. m_deleter(d),
  136. m_execute_on_destruction(true)
  137. {
  138. }
  139. catch( ... )
  140. {
  141. d(r);
  142. }
  143. //template<class TR = R, std::enable_if_t<std::is_nothrow_move_constructible<TR>::value, int> = 0>
  144. //unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  145. //&& std::is_nothrow_move_constructible<D>::value)
  146. //: m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  147. //m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  148. //m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  149. //{
  150. //}
  151. //template<class TR = R, std::enable_if_t<!std::is_nothrow_move_constructible<TR>::value, int> = 0>
  152. //unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  153. //&& std::is_nothrow_move_constructible<D>::value)
  154. //try : m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  155. //m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  156. //m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  157. //{
  158. //}
  159. //catch( ... )
  160. //{
  161. //other.get_deleter()(other.m_resource.get());
  162. //other.release();
  163. //throw;
  164. //}
  165. // FIXME: Needs update
  166. unique_resource(unique_resource&&) = default;
  167. unique_resource(const unique_resource&) = delete;
  168. ~unique_resource()
  169. {
  170. reset();
  171. }
  172. void reset()
  173. {
  174. if( m_execute_on_destruction == true )
  175. {
  176. m_execute_on_destruction = false;
  177. get_deleter()(m_resource.get());
  178. }
  179. }
  180. template<class RR>
  181. void reset(RR&& r)
  182. {
  183. auto se = make_scope_exit([this, &r] { get_deleter()(r); });
  184. reset();
  185. m_resource.reset(std::forward<RR>(r));
  186. m_execute_on_destruction = true;
  187. se.release();
  188. }
  189. void release()
  190. {
  191. m_execute_on_destruction = false;
  192. }
  193. const R& get() const noexcept
  194. {
  195. return m_resource.get();
  196. }
  197. template<class RR = R,
  198. std::enable_if_t<std::is_pointer<RR>::value
  199. && ( std::is_class<std::remove_pointer_t<RR>>::value
  200. || std::is_union<std::remove_pointer_t<RR>>::value ), int> = 0
  201. >
  202. RR operator->() const noexcept
  203. {
  204. return m_resource.get();
  205. }
  206. template<class RR = R,
  207. std::enable_if_t<( std::is_pointer<RR>::value && !std::is_void<std::remove_pointer_t<RR>>::value), int> = 0>
  208. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  209. {
  210. return *get();
  211. }
  212. const D& get_deleter() const noexcept
  213. {
  214. return m_deleter;
  215. }
  216. //template<class RR = R, class DD = D,
  217. //std::enable_if_t<(std::is_nothrow_move_assignable<RR>::value || std::is_nothrow_copy_assignable<RR>::value)
  218. //&& (std::is_nothrow_copy_assignable<DD>::value || std::is_nothrow_copy_assignable<DD>::value), int> = 0
  219. //>
  220. //unique_resource& operator=(unique_resource&& other)
  221. //{
  222. //if( this != &other )
  223. //{
  224. //reset();
  225. //m_resource = std::forward<RR>(other.m_resource);
  226. //m_deleter = std::forward<DD>(other.m_deleter);
  227. //m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  228. //}
  229. //return *this;
  230. //}
  231. // FIXME: Needs update
  232. unique_resource& operator=(unique_resource&&) = default;
  233. unique_resource& operator=(const unique_resource&) = delete;
  234. private:
  235. Wrapper<R> m_resource;
  236. D m_deleter;
  237. bool m_execute_on_destruction;
  238. };
  239. template<class R, class D>
  240. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
  241. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  242. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  243. {
  244. return unique_resource<std::decay_t<R>, std::decay_t<D>>{std::forward<R>(r), std::forward<D>(d)};
  245. }
  246. template<class R, class D>
  247. unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
  248. noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  249. {
  250. return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
  251. }
  252. template<class R, class D, class S = R>
  253. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  254. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  255. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  256. {
  257. const bool mustRelease{r == invalid};
  258. auto ur = make_unique_resource(r, d);
  259. if( mustRelease == true )
  260. {
  261. ur.release();
  262. }
  263. return ur;
  264. }
  265. }