Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

171 linhas
6.0KB

  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) : m_resource(std::move(r)), m_deleter(std::move(d)), m_execute_on_destruction(true)
  55. {
  56. }
  57. template<class RR, class DD,
  58. std::enable_if_t<std::is_lvalue_reference<RR>::value || std::is_lvalue_reference<DD>::value, int> = 0,
  59. std::enable_if_t<(std::is_copy_constructible<R>::value || std::is_nothrow_move_constructible<R>::value)
  60. && (std::is_copy_constructible<D>::value || std::is_nothrow_move_constructible<D>::value), int> = 0,
  61. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<R, RR>, int> = 0,
  62. std::enable_if_t<is_nothrow_move_or_copy_constructible_from_v<D, DD>, int> = 0
  63. >
  64. explicit unique_resource(RR&& r, DD&& d) try : m_resource(r), m_deleter(d), m_execute_on_destruction(true)
  65. {
  66. }
  67. catch( ... )
  68. {
  69. d(r);
  70. throw;
  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(std::forward<R>(other.m_resource)),
  77. m_deleter(std::forward<D>(other.m_deleter)),
  78. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  79. {
  80. }
  81. template<class TR = R, class TD = D,
  82. std::enable_if_t<(!std::is_nothrow_move_constructible<TR>::value
  83. || !std::is_nothrow_move_constructible<TD>::value), int> = 0
  84. >
  85. unique_resource(unique_resource&& other) : m_resource(other.m_resource),
  86. m_deleter(other.m_deleter),
  87. m_execute_on_destruction(std::exchange(other.m_execute_on_destruction, false))
  88. {
  89. }
  90. unique_resource(const unique_resource&) = delete;
  91. ~unique_resource()
  92. {
  93. reset();
  94. }
  95. void reset()
  96. {
  97. if( m_execute_on_destruction == true )
  98. {
  99. m_execute_on_destruction = false;
  100. m_deleter(m_resource);
  101. }
  102. }
  103. template<class RR>
  104. void reset(RR&& r)
  105. {
  106. reset();
  107. m_resource = move_assign_if_noexcept(r);
  108. m_execute_on_destruction = true;
  109. }
  110. void release()
  111. {
  112. m_execute_on_destruction = false;
  113. }
  114. const R& get() const noexcept
  115. {
  116. return m_resource;
  117. }
  118. const D& get_deleter() const noexcept
  119. {
  120. return m_deleter;
  121. }
  122. unique_resource& operator=(unique_resource&& other); // TODO: Implement
  123. unique_resource& operator=(const unique_resource&) = delete;
  124. private:
  125. R m_resource;
  126. D m_deleter;
  127. bool m_execute_on_destruction;
  128. };
  129. template<class Resource, class Deleter>
  130. unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>> make_unique_resource(Resource&& r, Deleter&& d)
  131. {
  132. return unique_resource<std::decay_t<Resource>, std::decay_t<Deleter>>{std::forward<Resource>(r), std::forward<Deleter>(d)};
  133. }
  134. }