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.

148 lines
5.4KB

  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 R, class D>
  32. class unique_resource
  33. {
  34. public:
  35. template<class RR, class DD,
  36. std::enable_if_t<(!std::is_lvalue_reference<RR>::value)
  37. && std::is_nothrow_constructible<R, RR>::value, int> = 0,
  38. std::enable_if_t<(!std::is_lvalue_reference<DD>::value)
  39. && std::is_nothrow_constructible<D, DD>::value, int> = 0,
  40. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  41. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  42. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  43. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  44. >
  45. explicit unique_resource(RR&& r, DD&& d) : m_resource(std::move(r)), m_deleter(std::move(d)), m_execute_on_destruction(true)
  46. {
  47. }
  48. template<class RR, class DD,
  49. std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
  50. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  51. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  52. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  53. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  54. >
  55. explicit unique_resource(RR&& r, DD&& d) try : m_resource(r), m_deleter(d), m_execute_on_destruction(true)
  56. {
  57. }
  58. catch( ... )
  59. {
  60. d(r);
  61. throw;
  62. }
  63. template<class TR = R, class TD = D,
  64. std::enable_if_t<(std::is_nothrow_move_constructible<TR>::value
  65. && std::is_nothrow_move_constructible<TD>::value), int> = 0
  66. >
  67. unique_resource(unique_resource&& other) : m_resource(std::forward<R>(other.m_resource)),
  68. m_deleter(std::forward<D>(other.m_deleter)),
  69. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  70. {
  71. }
  72. template<class TR = R, class TD = D,
  73. std::enable_if_t<(!std::is_nothrow_move_constructible<TR>::value
  74. || !std::is_nothrow_move_constructible<TD>::value), int> = 0
  75. >
  76. unique_resource(unique_resource&& other) : m_resource(other.m_resource),
  77. m_deleter(other.m_deleter),
  78. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  79. {
  80. }
  81. unique_resource(const unique_resource&) = delete;
  82. ~unique_resource()
  83. {
  84. if( m_execute_on_destruction == true )
  85. {
  86. m_deleter(m_resource);
  87. }
  88. }
  89. void reset()
  90. {
  91. if( m_execute_on_destruction == true )
  92. {
  93. m_execute_on_destruction = false;
  94. m_deleter(m_resource);
  95. }
  96. }
  97. void release()
  98. {
  99. m_execute_on_destruction = false;
  100. }
  101. const R& get() const noexcept
  102. {
  103. return m_resource;
  104. }
  105. unique_resource& operator=(unique_resource&& other);
  106. unique_resource& operator=(const unique_resource&) = delete;
  107. private:
  108. R m_resource;
  109. D m_deleter;
  110. bool m_execute_on_destruction;
  111. };
  112. template<class Resource, class Deleter>
  113. unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>> make_unique_resource(Resource&& r, Deleter&& d)
  114. {
  115. return unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>>{std::forward<Resource>(r), std::forward<Deleter>(d)};
  116. }
  117. }