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

unique_resource.h 9.4KB

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