The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

114 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. /** An easy way to ensure that a function is called at the end of the current
  20. scope.
  21. Usage:
  22. @code
  23. {
  24. if (flag == true)
  25. return;
  26. // While this code executes, flag is true e.g. to prevent reentrancy
  27. flag = true;
  28. // When we exit this scope, flag must be false
  29. const ScopeGuard scope { [&] { flag = false; } };
  30. if (checkInitialCondition())
  31. return; // Scope's lambda will fire here...
  32. if (checkCriticalCondition())
  33. throw std::runtime_error{}; // ...or here...
  34. doWorkHavingEstablishedPreconditions();
  35. } // ...or here!
  36. @endcode
  37. @tags{Core}
  38. */
  39. template <typename Fn> struct ScopeGuard : Fn { ~ScopeGuard() { Fn::operator()(); } };
  40. template <typename Fn> ScopeGuard (Fn) -> ScopeGuard<Fn>;
  41. /**
  42. A ScopeGuard that uses a std::function internally to allow type erasure.
  43. This can be handy; it allows lots of ErasedScopeGuards, all with different
  44. callbacks, to be stored in a homogeneous container.
  45. An instance of this type will automatically call its callback when it is destroyed.
  46. ErasedScopeGuard has a few similarities with std::unique_ptr:
  47. - Calling reset() on a unique_ptr destroys the object if it hasn't been destroyed yet
  48. and puts the unique_ptr back into a default/null state; calling reset() on an
  49. ErasedScopeGuard calls the callback if it hasn't been called yet and puts the Guard
  50. back into a default/null state.
  51. - Calling release() on a unique_ptr returns the unique_ptr back to a default state
  52. without destroying the managed object; calling release() on an ErasedScopeGuard
  53. returns the Guard back to a default state without calling the callback.
  54. - Moving a unique_ptr transfers the responsibility of destroying the managed object
  55. to another unique_ptr instance; moving an ErasedScopeGuard transfers the
  56. responsibility of calling the callback to another Guard instance.
  57. */
  58. class [[nodiscard]] ErasedScopeGuard
  59. {
  60. public:
  61. /** Constructs an ErasedScopeGuard with no callback. */
  62. ErasedScopeGuard() = default;
  63. /** Constructs an ErasedScopeGuard that will call the provided callback
  64. when the Guard is destroyed.
  65. */
  66. explicit ErasedScopeGuard (std::function<void()> d);
  67. /** Constructs an instance that assumes responsibility for calling other's callback. */
  68. ErasedScopeGuard (ErasedScopeGuard&& other) noexcept;
  69. /** Calls the stored callback, if any, then assumes responsibility for calling
  70. other's callback. After this call, other will be reset to its default state.
  71. */
  72. ErasedScopeGuard& operator= (ErasedScopeGuard&& other) noexcept;
  73. /** Destructor, calls the callback assigned to this ScopeGuard.
  74. */
  75. ~ErasedScopeGuard() noexcept;
  76. /** Calls the stored callback, if any, then resets this instance to its
  77. default state.
  78. */
  79. void reset();
  80. /** Resets this instance to its default state without calling the stored
  81. callback.
  82. */
  83. void release();
  84. JUCE_DECLARE_NON_COPYABLE (ErasedScopeGuard)
  85. private:
  86. std::function<void()> detach;
  87. };
  88. } // namespace juce