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.

312 lines
11KB

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