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
8.3KB

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