Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

208 lines
7.7KB

  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. const R& get() const noexcept
  96. {
  97. return m_resource.get();
  98. }
  99. template<class RR = R, std::enable_if_t<std::is_pointer_v<RR>, int> = 0>
  100. RR operator->() const noexcept
  101. {
  102. return m_resource.get();
  103. }
  104. template<class RR = R,
  105. std::enable_if_t<( std::is_pointer_v<RR>
  106. && !std::is_void_v<std::remove_pointer_t<RR>>), int> = 0>
  107. std::add_lvalue_reference_t<std::remove_pointer_t<RR>> operator*() const noexcept
  108. {
  109. return *get();
  110. }
  111. const D& get_deleter() const noexcept
  112. {
  113. return m_deleter.get();
  114. }
  115. template<class RR = R, class DD = D,
  116. std::enable_if_t<(std::is_nothrow_move_assignable_v<RR>
  117. || std::is_nothrow_copy_assignable_v<RR>)
  118. && (std::is_nothrow_move_assignable_v<DD>
  119. || std::is_nothrow_copy_assignable_v<DD>), int> = 0
  120. >
  121. unique_resource& operator=(unique_resource&& other) noexcept(std::is_nothrow_assignable_v<R&, R>
  122. && std::is_nothrow_assignable_v<D&, D>)
  123. {
  124. if( this != &other )
  125. {
  126. reset();
  127. if constexpr( std::is_nothrow_move_assignable_v<R> == true )
  128. {
  129. m_deleter.reset(detail::forward_if_nothrow_move_constructible(other.m_deleter.get()));
  130. m_resource.reset(std::forward<RR>(other.m_resource.get()));
  131. }
  132. else if constexpr( std::is_nothrow_move_assignable_v<D> == true )
  133. {
  134. m_resource.reset(detail::forward_if_nothrow_move_constructible(other.m_resource.get()));
  135. m_deleter.reset(std::forward<DD>(other.m_deleter.get()));
  136. }
  137. else
  138. {
  139. m_resource = other.m_resource;
  140. m_deleter = other.m_deleter;
  141. }
  142. m_execute_on_destruction = std::exchange(other.m_execute_on_destruction, false);
  143. }
  144. return *this;
  145. }
  146. unique_resource& operator=(const unique_resource&) = delete;
  147. private:
  148. detail::Wrapper<R> m_resource;
  149. detail::Wrapper<D> m_deleter;
  150. bool m_execute_on_destruction;
  151. };
  152. template<class R, class D>
  153. unique_resource(R, D) -> unique_resource<R, D>;
  154. template<class R, class D, class S = R>
  155. unique_resource<std::decay_t<R>, std::decay_t<D>> make_unique_resource_checked(R&& r, const S& invalid, D&& d)
  156. noexcept(std::is_nothrow_constructible_v<std::decay_t<R>, R>
  157. && std::is_nothrow_constructible_v<std::decay_t<D>, D>)
  158. {
  159. const bool must_release{r == invalid};
  160. unique_resource<std::decay_t<R>, std::decay_t<D>> ur{std::forward<R>(r), std::forward<D>(d)};
  161. if( must_release == true )
  162. {
  163. ur.release();
  164. }
  165. return ur;
  166. }
  167. }