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.

131 linhas
3.5KB

  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. namespace sr
  23. {
  24. enum class invoke_it
  25. {
  26. once,
  27. again
  28. };
  29. template<class Ressource, class Deleter>
  30. class unique_resource_t
  31. {
  32. public:
  33. explicit unique_resource_t(Ressource&& res, Deleter&& deleter, bool shouldrun = true) noexcept : m_resource(std::move(res)),
  34. m_deleter(std::move(deleter)),
  35. m_execute_on_destruction(shouldrun)
  36. {
  37. }
  38. unique_resource_t(const unique_resource_t&) = delete;
  39. unique_resource_t(unique_resource_t&& other) noexcept : m_resource(std::move(other.m_resource)),
  40. m_deleter(std::move(other.m_deleter)),
  41. m_execute_on_destruction(other.m_execute_on_destruction)
  42. {
  43. other.release();
  44. }
  45. ~unique_resource_t()
  46. {
  47. invoke(invoke_it::once);
  48. }
  49. void invoke(const invoke_it strategy = invoke_it::once) noexcept
  50. {
  51. if( m_execute_on_destruction == true )
  52. {
  53. call_deleter_safe();
  54. }
  55. m_execute_on_destruction = ( strategy == invoke_it::again );
  56. }
  57. const Ressource& release() noexcept
  58. {
  59. m_execute_on_destruction = false;
  60. return m_resource;
  61. }
  62. void reset(Ressource&& res) noexcept
  63. {
  64. invoke(invoke_it::again);
  65. m_resource = std::move(res);
  66. }
  67. const Ressource& get() const noexcept
  68. {
  69. return m_resource;
  70. }
  71. unique_resource_t& operator=(unique_resource_t&& other) noexcept
  72. {
  73. invoke(invoke_it::once);
  74. m_resource = std::move(other.m_resource);
  75. m_deleter = std::move(other.m_deleter);
  76. m_execute_on_destruction = other.m_execute_on_destruction;
  77. other.release();
  78. return *this;
  79. }
  80. unique_resource_t& operator=(const unique_resource_t&) = delete;
  81. private:
  82. void call_deleter_safe() noexcept
  83. {
  84. try
  85. {
  86. m_deleter(m_resource);
  87. }
  88. catch( ... ) { /* Empty */ }
  89. }
  90. Ressource m_resource;
  91. Deleter m_deleter;
  92. bool m_execute_on_destruction;
  93. };
  94. template<class Ressource, class Deleter>
  95. unique_resource_t<Ressource, Deleter> unique_resource(Ressource&& res, Deleter d) noexcept
  96. {
  97. return unique_resource_t<Ressource, Deleter>{std::move(res), std::move(d), true};
  98. }
  99. }