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.

219 lines
7.9KB

  1. // MIT License
  2. //
  3. // Copyright (c) 2017-2018 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. template<class RR, class DD,
  44. std::enable_if_t<(std::is_constructible_v<R ,RR> && std::is_constructible_v<D, DD>
  45. && (std::is_nothrow_constructible_v<R, RR> || std::is_constructible_v<R, RR&>)
  46. && (std::is_nothrow_constructible_v<D, DD> || std::is_constructible_v<D, DD&>)), int> = 0
  47. >
  48. explicit unique_resource(RR&& r, DD&& d) noexcept((std::is_nothrow_constructible_v<R, RR> || std::is_nothrow_constructible_v<R, RR&>)
  49. && (std::is_nothrow_constructible_v<D, DD> || std::is_nothrow_constructible_v<D, DD&>))
  50. : resource(detail::forward_if_nothrow_constructible<R, RR>(std::forward<RR>(r)), scope_exit{[&r, &d] { d(r); }}),
  51. deleter(detail::forward_if_nothrow_constructible<D, DD>(std::forward<DD>(d)), scope_exit{[this, &d] { d(get()); }}),
  52. execute_on_destruction(true)
  53. {
  54. }
  55. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R>
  56. && std::is_nothrow_move_constructible_v<D>)
  57. : resource(std::move_if_noexcept(other.resource.get()), scope_exit{[] { }}),
  58. deleter(std::move_if_noexcept(other.deleter.get()), scope_exit{[&other] {
  59. other.get_deleter()(other.resource.get());
  60. other.release(); }}),
  61. execute_on_destruction(std::exchange(other.execute_on_destruction, false))
  62. {
  63. }
  64. unique_resource(const unique_resource&) = delete;
  65. ~unique_resource()
  66. {
  67. reset();
  68. }
  69. void reset() noexcept
  70. {
  71. if( execute_on_destruction == true )
  72. {
  73. execute_on_destruction = false;
  74. get_deleter()(resource.get());
  75. }
  76. }
  77. template<class RR>
  78. void reset(RR&& r)
  79. {
  80. reset();
  81. using R1 = typename detail::Wrapper<R>::type;
  82. auto se = scope_exit{[this, &r] { get_deleter()(r); }};
  83. if constexpr( std::is_nothrow_assignable_v<R1&, RR> == true )
  84. {
  85. resource.reset(std::forward<RR>(r));
  86. }
  87. else
  88. {
  89. resource.reset(std::as_const(r));
  90. }
  91. execute_on_destruction = true;
  92. se.release();
  93. }
  94. void release() noexcept
  95. {
  96. execute_on_destruction = false;
  97. }
  98. const R& get() const noexcept
  99. {
  100. return resource.get();
  101. }
  102. template<class RR = R, std::enable_if_t<std::is_pointer_v<RR>, int> = 0>
  103. RR operator->() const noexcept
  104. {
  105. return resource.get();
  106. }
  107. template<class RR = R,
  108. std::enable_if_t<( std::is_pointer_v<RR>
  109. && !std::is_void_v<std::remove_pointer_t<RR>>), int> = 0>
  110. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  111. {
  112. return *get();
  113. }
  114. const D& get_deleter() const noexcept
  115. {
  116. return deleter.get();
  117. }
  118. template<class RR = R, class DD = D,
  119. std::enable_if_t<(std::is_nothrow_move_assignable_v<RR>
  120. || std::is_nothrow_copy_assignable_v<RR>)
  121. && (std::is_nothrow_move_assignable_v<DD>
  122. || std::is_nothrow_copy_assignable_v<DD>), int> = 0
  123. >
  124. unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v<R&, R>
  125. && std::is_nothrow_assignable_v<D&, D>)
  126. {
  127. if( this != &other )
  128. {
  129. reset();
  130. if constexpr( std::is_nothrow_move_assignable_v<RR> == true )
  131. {
  132. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  133. {
  134. resource.reset(std::move(other.resource));
  135. deleter.reset(std::move(other.deleter));
  136. }
  137. else
  138. {
  139. deleter.reset(other.deleter);
  140. resource.reset(std::move(other.resource));
  141. }
  142. }
  143. else
  144. {
  145. if constexpr( std::is_nothrow_move_assignable_v<DD> == true )
  146. {
  147. resource.reset(other.resource);
  148. deleter.reset(std::move(other.deleter));
  149. }
  150. else
  151. {
  152. resource.reset(other.resource);
  153. deleter.reset(other.deleter);
  154. }
  155. }
  156. execute_on_destruction = std::exchange(other.execute_on_destruction, false);
  157. }
  158. return *this;
  159. }
  160. unique_resource& operator=(const unique_resource&) = delete;
  161. private:
  162. detail::Wrapper<R> resource;
  163. detail::Wrapper<D> deleter;
  164. bool execute_on_destruction;
  165. };
  166. template<class R, class D>
  167. unique_resource(R, D) -> unique_resource<R, D>;
  168. template<class R, class D, class S = std::decay_t<R>>
  169. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  170. noexcept(std::is_nothrow_constructible_v<std::decay_t<R>, R>
  171. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  172. {
  173. const bool must_release{r == invalid};
  174. unique_resource<std::decay_t<R>, std::decay_t<D>> ur{std::forward<R>(r), std::forward<D>(d)};
  175. if( must_release == true )
  176. {
  177. ur.release();
  178. }
  179. return ur;
  180. }
  181. }