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.

233 líneas
8.9KB

  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 <utility>
  22. #include <type_traits>
  23. namespace sr
  24. {
  25. template<class T, class TT>
  26. using is_ntmocp_constructible = std::conditional_t<std::is_reference<TT>::value || !std::is_nothrow_move_constructible<TT>::value,
  27. typename std::is_constructible<T, TT const &>::type,
  28. typename std::is_constructible<T, TT>::type>;
  29. template<class T, class TT>
  30. constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible<T, TT>::value;
  31. template<class T,
  32. class U = std::conditional_t<(!std::is_nothrow_move_assignable<T>::value
  33. && std::is_copy_assignable<T>::value),
  34. T const &,
  35. T &&>>
  36. constexpr U move_assign_if_noexcept(T& value) noexcept
  37. {
  38. return std::move(value);
  39. }
  40. template<class T, std::enable_if_t<std::is_nothrow_move_constructible<T>::value, int> = 0>
  41. constexpr T&& forward_if_nothrow_move_constructible(T&& value)
  42. {
  43. return std::forward<T>(value);
  44. }
  45. template<class T, std::enable_if_t<!std::is_nothrow_move_constructible<T>::value, int> = 0>
  46. constexpr T forward_if_nothrow_move_constructible(T&& value)
  47. {
  48. return value;
  49. }
  50. template<class R, class D>
  51. class unique_resource
  52. {
  53. public:
  54. template<class RR, class DD,
  55. std::enable_if_t<(!std::is_lvalue_reference<RR>::value)
  56. && std::is_nothrow_constructible<R, RR>::value, int> = 0,
  57. std::enable_if_t<(!std::is_lvalue_reference<DD>::value)
  58. && std::is_nothrow_constructible<D, DD>::value, int> = 0,
  59. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  60. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  61. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  62. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  63. >
  64. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R,RR>::value
  65. && std::is_nothrow_constructible<D, DD>::value)
  66. : m_resource(std::move(r)),
  67. m_deleter(std::move(d)),
  68. m_execute_on_destruction(true)
  69. {
  70. }
  71. template<class RR, class DD,
  72. std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
  73. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  74. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  75. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  76. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  77. >
  78. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible<R, RR>::value
  79. && std::is_nothrow_constructible<D, DD>::value)
  80. try : m_resource(r),
  81. m_deleter(d),
  82. m_execute_on_destruction(true)
  83. {
  84. }
  85. catch( ... )
  86. {
  87. d(r);
  88. }
  89. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  90. && std::is_nothrow_move_constructible<D>::value)
  91. : m_resource(forward_if_nothrow_move_constructible<R>(std::forward<R>(other.m_resource))),
  92. m_deleter(forward_if_nothrow_move_constructible<D>(std::forward<D>(other.m_deleter))),
  93. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  94. {
  95. }
  96. unique_resource(const unique_resource&) = delete;
  97. ~unique_resource()
  98. {
  99. reset();
  100. }
  101. void reset()
  102. {
  103. if( m_execute_on_destruction == true )
  104. {
  105. m_execute_on_destruction = false;
  106. get_deleter()(m_resource);
  107. }
  108. }
  109. template<class RR>
  110. void reset(RR&& r)
  111. {
  112. reset();
  113. m_resource = move_assign_if_noexcept(r);
  114. m_execute_on_destruction = true;
  115. }
  116. void release()
  117. {
  118. m_execute_on_destruction = false;
  119. }
  120. const R& get() const noexcept
  121. {
  122. return m_resource;
  123. }
  124. template<class RR = R,
  125. std::enable_if_t<std::is_pointer<RR>::value && std::is_nothrow_copy_constructible<RR>::value
  126. && ( std::is_class<std::remove_pointer_t<RR>>::value
  127. || std::is_union<std::remove_pointer_t<RR>>::value ), int> = 0
  128. >
  129. RR operator->() const noexcept
  130. {
  131. return m_resource;
  132. }
  133. template<class RR = R,
  134. std::enable_if_t<std::is_pointer<RR>::value, int> = 0>
  135. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  136. {
  137. return *get();
  138. }
  139. const D& get_deleter() const noexcept
  140. {
  141. return m_deleter;
  142. }
  143. template<class RR = R, class DD = D,
  144. std::enable_if_t<(std::is_nothrow_move_assignable<RR>::value || std::is_nothrow_copy_assignable<RR>::value)
  145. && (std::is_nothrow_copy_assignable<DD>::value || std::is_nothrow_copy_assignable<DD>::value), int> = 0
  146. >
  147. unique_resource& operator=(unique_resource&& other)
  148. {
  149. if( this != &other )
  150. {
  151. reset();
  152. m_resource = std::forward<RR>(other.m_resource);
  153. m_deleter = std::forward<DD>(other.m_deleter);
  154. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  155. }
  156. return *this;
  157. }
  158. unique_resource& operator=(const unique_resource&) = delete;
  159. private:
  160. R m_resource;
  161. D m_deleter;
  162. bool m_execute_on_destruction;
  163. };
  164. template<class R, class D>
  165. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource(R&& r, D&& d)
  166. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  167. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  168. {
  169. return unique_resource<std::decay_t<R>, std::decay_t<D>>{std::forward<R>(r), std::forward<D>(d)};
  170. }
  171. template<class R, class D>
  172. unique_resource<R&, std::decay_t<D>> make_unique_resource(std::reference_wrapper<R> r, D d)
  173. noexcept(std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  174. {
  175. return unique_resource<R&, std::decay_t<D>>(r.get(), std::forward<D>(d));
  176. }
  177. template<class R, class D, class S = R>
  178. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  179. noexcept(std::is_nothrow_constructible<std::decay_t<R>, R>::value
  180. && std::is_nothrow_constructible<std::decay_t<D>, D>::value)
  181. {
  182. const bool mustRelease{r == invalid};
  183. auto ur = make_unique_resource(r, d);
  184. if( mustRelease == true )
  185. {
  186. ur.release();
  187. }
  188. return ur;
  189. }
  190. }