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.

120 lines
3.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. 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. if( m_execute_on_destruction == true )
  48. {
  49. try
  50. {
  51. m_deleter(m_resource);
  52. }
  53. catch( ... ) { /* Empty */ }
  54. }
  55. }
  56. void invoke(const invoke_it strategy = invoke_it::once) noexcept
  57. {
  58. if( m_execute_on_destruction == true )
  59. {
  60. try
  61. {
  62. m_deleter(m_resource);
  63. }
  64. catch( ... ) { /* Empty */ }
  65. }
  66. m_execute_on_destruction = ( strategy == invoke_it::again );
  67. }
  68. const Ressource& release() noexcept
  69. {
  70. m_execute_on_destruction = false;
  71. return m_resource;
  72. }
  73. unique_resource_t& operator=(unique_resource_t&& other) noexcept
  74. {
  75. m_deleter(m_resource);
  76. m_resource = std::move(other.m_resource);
  77. m_deleter = std::move(other.m_deleter);
  78. m_execute_on_destruction = other.m_execute_on_destruction;
  79. other.release();
  80. return *this;
  81. }
  82. unique_resource_t& operator=(const unique_resource_t&) = delete;
  83. private:
  84. Ressource m_resource;
  85. Deleter m_deleter;
  86. bool m_execute_on_destruction;
  87. };
  88. template<class Ressource, class Deleter>
  89. unique_resource_t<Ressource, Deleter> unique_resource(Ressource&& res, Deleter d) noexcept
  90. {
  91. return unique_resource_t<Ressource, Deleter>{std::move(res), std::move(d), true};
  92. }
  93. }