Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

212 lines
7.8KB

  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 "scope_exit.h"
  22. #include "detail/wrapper.h"
  23. #include <utility>
  24. #include <type_traits>
  25. namespace sr
  26. {
  27. namespace detail
  28. {
  29. template<class T, class TT>
  30. using is_ntmocp_constructible = std::conditional_t<std::is_reference_v<TT>
  31. || !std::is_nothrow_move_constructible_v<TT>,
  32. typename std::is_constructible<T, const TT&>::type,
  33. typename std::is_constructible<T, TT>::type>;
  34. template<class T, class TT>
  35. constexpr auto is_nothrow_move_or_copy_constructible_from_v = is_ntmocp_constructible<T, TT>::value;
  36. template<class T, class U = std::conditional_t<std::is_nothrow_move_constructible_v<T>, T&&, const T&>>
  37. constexpr U forward_if_nothrow_move_constructible(T&& value) noexcept
  38. {
  39. return std::forward<T>(value);
  40. }
  41. }
  42. template<class R, class D>
  43. class unique_resource
  44. {
  45. public:
  46. template<class RR, class DD,
  47. std::enable_if_t<(std::is_copy_constructible_v<R> || std::is_nothrow_move_constructible_v<R>)
  48. && (std::is_copy_constructible_v<D> || std::is_nothrow_move_constructible_v<D>), int> = 0,
  49. std::enable_if_t<detail::is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  50. std::enable_if_t<detail::is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  51. >
  52. explicit unique_resource(RR&& r, DD&& d) noexcept(std::is_nothrow_constructible_v<R, RR>
  53. && std::is_nothrow_constructible_v<D, DD>)
  54. : m_resource(std::forward<RR>(r), scope_exit{[&r, &d] { d(r); }}),
  55. m_deleter(std::forward<DD>(d), scope_exit{[this, &d] { d(get()); }}),
  56. m_execute_on_destruction(true)
  57. {
  58. }
  59. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible_v<R>
  60. && std::is_nothrow_move_constructible_v<D>)
  61. : m_resource(detail::forward_if_nothrow_move_constructible(other.m_resource.get()), scope_exit{[] { }}),
  62. m_deleter(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()),
  63. scope_exit{[&other] {
  64. other.get_deleter()(other.m_resource.get());
  65. other.release(); }}),
  66. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  67. {
  68. }
  69. unique_resource(const unique_resource&) = delete;
  70. ~unique_resource()
  71. {
  72. reset();
  73. }
  74. void reset() noexcept
  75. {
  76. if( m_execute_on_destruction == true )
  77. {
  78. m_execute_on_destruction = false;
  79. get_deleter()(m_resource.get());
  80. }
  81. }
  82. template<class RR>
  83. void reset(RR&& r)
  84. {
  85. auto se = scope_exit{[this, &r] { get_deleter()(r); }};
  86. reset();
  87. m_resource.reset(std::forward<RR>(r));
  88. m_execute_on_destruction = true;
  89. se.release();
  90. }
  91. void release() noexcept
  92. {
  93. m_execute_on_destruction = false;
  94. }
  95. decltype(auto) get() const noexcept
  96. {
  97. return m_resource.get();
  98. }
  99. template<class RR = R,
  100. std::enable_if_t<std::is_pointer_v<RR>
  101. && (std::is_class_v<std::remove_pointer_t<RR>>
  102. || std::is_union_v<std::remove_pointer_t<RR>>), int> = 0
  103. >
  104. RR operator->() const noexcept
  105. {
  106. return m_resource.get();
  107. }
  108. template<class RR = R,
  109. std::enable_if_t<( std::is_pointer_v<RR>
  110. && !std::is_void_v<std::remove_pointer_t<RR>>), int> = 0>
  111. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  112. {
  113. return *get();
  114. }
  115. const D& get_deleter() const noexcept
  116. {
  117. return m_deleter.get();
  118. }
  119. template<class RR = R, class DD = D,
  120. std::enable_if_t<(std::is_nothrow_move_assignable_v<RR>
  121. || std::is_nothrow_copy_assignable_v<RR>)
  122. && (std::is_nothrow_move_assignable_v<DD>
  123. || std::is_nothrow_copy_assignable_v<DD>), int> = 0
  124. >
  125. unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v<R&, R>
  126. && std::is_nothrow_assignable_v<D&, D>)
  127. {
  128. if( this != &other )
  129. {
  130. reset();
  131. if constexpr( std::is_nothrow_move_assignable_v<R> == true )
  132. {
  133. m_deleter.reset(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()));
  134. m_resource.reset(std::forward<RR>(other.m_resource.get()));
  135. }
  136. else if constexpr( std::is_nothrow_move_assignable_v<D> == true )
  137. {
  138. m_resource.reset(detail::forward_if_nothrow_move_constructible(other.m_resource.get()));
  139. m_deleter.reset(std::forward<DD>(other.m_deleter.get()));
  140. }
  141. else
  142. {
  143. m_resource = other.m_resource;
  144. m_deleter = other.m_deleter;
  145. }
  146. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  147. }
  148. return *this;
  149. }
  150. unique_resource& operator=(const unique_resource&) = delete;
  151. private:
  152. detail::Wrapper<R> m_resource;
  153. detail::Wrapper<D> m_deleter;
  154. bool m_execute_on_destruction;
  155. };
  156. template<class R, class D>
  157. unique_resource(R, D) -> unique_resource<R, D>;
  158. template<class R, class D, class S = R>
  159. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  160. noexcept(std::is_nothrow_constructible_v<std::decay_t<R>, R>
  161. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  162. {
  163. const bool must_release{r == invalid};
  164. auto ur = unique_resource{r, d};
  165. if( must_release == true )
  166. {
  167. ur.release();
  168. }
  169. return ur;
  170. }
  171. }