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.

228 line
8.2KB

  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. >
  133. unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v<R&, R>
  134. && std::is_nothrow_assignable_v<D&, D>)
  135. {
  136. if( this != &other )
  137. {
  138. reset();
  139. if constexpr( std::is_nothrow_move_assignable_v<RR> == true )
  140. {
  141. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  142. {
  143. resource.reset(std::move(other.resource));
  144. deleter.reset(std::move(other.deleter));
  145. }
  146. else
  147. {
  148. deleter.reset(other.deleter);
  149. resource.reset(std::move(other.resource));
  150. }
  151. }
  152. else
  153. {
  154. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  155. {
  156. resource.reset(other.resource);
  157. deleter.reset(std::move(other.deleter));
  158. }
  159. else
  160. {
  161. resource.reset(other.resource);
  162. deleter.reset(other.deleter);
  163. }
  164. }
  165. execute_on_reset = std::exchange(other.execute_on_reset, false);
  166. }
  167. return *this;
  168. }
  169. unique_resource& operator=(const unique_resource&) = delete;
  170. private:
  171. detail::Wrapper<R> resource;
  172. detail::Wrapper<D> deleter;
  173. bool execute_on_reset;
  174. };
  175. template<class R, class D>
  176. unique_resource(R, D) -> unique_resource<R, D>;
  177. template<class R, class D, class S = std::decay_t<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_v<std::decay_t<R>, R>
  180. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  181. {
  182. unique_resource<std::decay_t<R>, std::decay_t<D>> ur{std::forward<R>(r), std::forward<D>(d)};
  183. if( bool(r == invalid) == true )
  184. {
  185. ur.release();
  186. }
  187. return ur;
  188. }
  189. }