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.

unique_resource.h 7.0KB

7 년 전
7 년 전
7 년 전
7 년 전
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. throw;
  79. }
  80. template<class TR = R, class TD = D,
  81. std::enable_if_t<(std::is_nothrow_move_constructible<TR>::value
  82. && std::is_nothrow_move_constructible<TD>::value), int> = 0
  83. >
  84. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  85. && std::is_nothrow_move_constructible<D>::value)
  86. : m_resource(std::forward<R>(other.m_resource)),
  87. m_deleter(std::forward<D>(other.m_deleter)),
  88. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  89. {
  90. }
  91. template<class TR = R, class TD = D,
  92. std::enable_if_t<(!std::is_nothrow_move_constructible<TR>::value
  93. || !std::is_nothrow_move_constructible<TD>::value), int> = 0
  94. >
  95. unique_resource(unique_resource&& other) noexcept(std::is_nothrow_move_constructible<R>::value
  96. && std::is_nothrow_move_constructible<D>::value)
  97. : m_resource(other.m_resource),
  98. m_deleter(other.m_deleter),
  99. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  100. {
  101. }
  102. unique_resource(const unique_resource&) = delete;
  103. ~unique_resource()
  104. {
  105. reset();
  106. }
  107. void reset()
  108. {
  109. if( m_execute_on_destruction == true )
  110. {
  111. m_execute_on_destruction = false;
  112. get_deleter()(m_resource);
  113. }
  114. }
  115. template<class RR>
  116. void reset(RR&& r)
  117. {
  118. reset();
  119. m_resource = move_assign_if_noexcept(r);
  120. m_execute_on_destruction = true;
  121. }
  122. void release()
  123. {
  124. m_execute_on_destruction = false;
  125. }
  126. const R& get() const noexcept
  127. {
  128. return m_resource;
  129. }
  130. const D& get_deleter() const noexcept
  131. {
  132. return m_deleter;
  133. }
  134. unique_resource& operator=(unique_resource&& other); // TODO: Implement
  135. unique_resource& operator=(const unique_resource&) = delete;
  136. private:
  137. R m_resource;
  138. D m_deleter;
  139. bool m_execute_on_destruction;
  140. };
  141. template<class Resource, class Deleter>
  142. unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>> make_unique_resource(Resource&& r, Deleter&& d)
  143. {
  144. return unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>>{std::forward<Resource>(r), std::forward<Deleter>(d)};
  145. }
  146. }