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.

unique_resource.h 8.2KB

5 年之前
7 年之前
7 年之前
7 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2019 offa
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in all
  13. // copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. // SOFTWARE.
  22. #pragma once
  23. #include "scope_exit.h"
  24. #include "detail/wrapper.h"
  25. #include <utility>
  26. #include <type_traits>
  27. namespace sr
  28. {
  29. namespace detail
  30. {
  31. template<class T, class U,
  32. class R = std::conditional_t<std::is_nothrow_constructible_v<T, U>, U&&, U>
  33. >
  34. constexpr R forward_if_nothrow_constructible(U&& arg)
  35. {
  36. return std::forward<U>(arg);
  37. }
  38. }
  39. template<class R, class D>
  40. class unique_resource
  41. {
  42. public:
  43. unique_resource()
  44. : resource(R{}),
  45. deleter(D{}),
  46. execute_on_reset(false)
  47. {
  48. }
  49. template<class RR, class DD,
  50. std::enable_if_t<(std::is_constructible_v<R ,RR> && std::is_constructible_v<D, DD>
  51. && (std::is_nothrow_constructible_v<R, RR> || std::is_constructible_v<R, RR&>)
  52. && (std::is_nothrow_constructible_v<D, DD> || std::is_constructible_v<D, DD&>)), int> = 0
  53. >
  54. unique_resource(RR&& r, DD&& d) noexcept((std::is_nothrow_constructible_v<R, RR> || std::is_nothrow_constructible_v<R, RR&>)
  55. && (std::is_nothrow_constructible_v<D, DD> || std::is_nothrow_constructible_v<D, DD&>))
  56. : resource(detail::forward_if_nothrow_constructible<R, RR>(std::forward<RR>(r)), scope_exit{[&r, &d] { d(r); }}),
  57. deleter(detail::forward_if_nothrow_constructible<D, DD>(std::forward<DD>(d)), scope_exit{[this, &d] { d(get()); }}),
  58. execute_on_reset(true)
  59. {
  60. }
  61. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R>
  62. && std::is_nothrow_move_constructible_v<D>)
  63. : resource(std::move_if_noexcept(other.resource.get())),
  64. deleter(std::move_if_noexcept(other.deleter.get()), scope_exit{[&other] {
  65. if( other.execute_on_reset == true )
  66. {
  67. other.get_deleter()(other.resource.get());
  68. }
  69. other.release(); }}),
  70. execute_on_reset(std::exchange(other.execute_on_reset, false))
  71. {
  72. }
  73. unique_resource(const unique_resource&) = delete;
  74. ~unique_resource()
  75. {
  76. reset();
  77. }
  78. void reset() noexcept
  79. {
  80. if( execute_on_reset == true )
  81. {
  82. execute_on_reset = false;
  83. get_deleter()(resource.get());
  84. }
  85. }
  86. template<class RR>
  87. void reset(RR&& r)
  88. {
  89. reset();
  90. using R1 = typename detail::Wrapper<R>::type;
  91. auto se = scope_exit{[this, &r] { get_deleter()(r); }};
  92. if constexpr( std::is_nothrow_assignable_v<R1&, RR> == true )
  93. {
  94. resource.reset(std::forward<RR>(r));
  95. }
  96. else
  97. {
  98. resource.reset(std::as_const(r));
  99. }
  100. execute_on_reset = true;
  101. se.release();
  102. }
  103. void release() noexcept
  104. {
  105. execute_on_reset = false;
  106. }
  107. const R& get() const noexcept
  108. {
  109. return resource.get();
  110. }
  111. template<class RR = R, std::enable_if_t<std::is_pointer_v<RR>, int> = 0>
  112. RR operator->() const noexcept
  113. {
  114. return resource.get();
  115. }
  116. template<class RR = R,
  117. std::enable_if_t<( std::is_pointer_v<RR>
  118. && !std::is_void_v<std::remove_pointer_t<RR>>), int> = 0>
  119. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  120. {
  121. return *get();
  122. }
  123. const D& get_deleter() const noexcept
  124. {
  125. return deleter.get();
  126. }
  127. template<class RR = R, class DD = D,
  128. std::enable_if_t<(std::is_nothrow_move_assignable_v<RR>
  129. || std::is_nothrow_copy_assignable_v<RR>)
  130. && (std::is_nothrow_move_assignable_v<DD>
  131. || std::is_nothrow_copy_assignable_v<DD>), int> = 0>
  132. unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v<R&, R>
  133. && std::is_nothrow_assignable_v<D&, D>)
  134. {
  135. if( this != &other )
  136. {
  137. reset();
  138. if constexpr( std::is_nothrow_move_assignable_v<RR> == true )
  139. {
  140. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  141. {
  142. resource.reset(std::move(other.resource));
  143. deleter.reset(std::move(other.deleter));
  144. }
  145. else
  146. {
  147. deleter.reset(other.deleter);
  148. resource.reset(std::move(other.resource));
  149. }
  150. }
  151. else
  152. {
  153. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  154. {
  155. resource.reset(other.resource);
  156. deleter.reset(std::move(other.deleter));
  157. }
  158. else
  159. {
  160. resource.reset(other.resource);
  161. deleter.reset(other.deleter);
  162. }
  163. }
  164. execute_on_reset = std::exchange(other.execute_on_reset, false);
  165. }
  166. return *this;
  167. }
  168. unique_resource& operator=(const unique_resource&) = delete;
  169. private:
  170. detail::Wrapper<R> resource;
  171. detail::Wrapper<D> deleter;
  172. bool execute_on_reset;
  173. };
  174. template<class R, class D>
  175. unique_resource(R, D) -> unique_resource<R, D>;
  176. template<class R, class D, class S = std::decay_t<R>>
  177. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  178. noexcept(std::is_nothrow_constructible_v<std::decay_t<R>, R>
  179. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  180. {
  181. unique_resource<std::decay_t<R>, std::decay_t<D>> ur{std::forward<R>(r), std::forward<D>(d)};
  182. if( bool(r == invalid) == true )
  183. {
  184. ur.release();
  185. }
  186. return ur;
  187. }
  188. }