diff --git a/modules/juce_core/misc/juce_Functional.h b/modules/juce_core/misc/juce_Functional.h index b458c4811a..05eaca5161 100644 --- a/modules/juce_core/misc/juce_Functional.h +++ b/modules/juce_core/misc/juce_Functional.h @@ -86,4 +86,31 @@ Object withMember (Object copy, Member OtherObject::* member, Other&& value) return copy; } +/** An easy way to ensure that a function is called at the end of the current + scope. + + Usage: + @code + { + if (flag == true) + return; + + // While this code executes, flag is true e.g. to prevent reentrancy + flag = true; + // When we exit this scope, flag must be false + const ScopeGuard scope { [&] { flag = false; } }; + + if (checkInitialCondition()) + return; // Scope's lambda will fire here... + + if (checkCriticalCondition()) + throw std::runtime_error{}; // ...or here... + + doWorkHavingEstablishedPreconditions(); + } // ...or here! + @endcode +*/ +template struct ScopeGuard : Fn { ~ScopeGuard() { Fn::operator()(); } }; +template ScopeGuard (Fn) -> ScopeGuard; + } // namespace juce diff --git a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp index 8f17f8ef54..9dbe093d11 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp @@ -91,13 +91,6 @@ static bool contextHasTextureNpotFeature() return stringTokens.contains ("GL_ARB_texture_non_power_of_two"); } -template struct ScopeGuard : Fn -{ - ~ScopeGuard() { Fn::operator()(); } -}; - -template ScopeGuard (Fn) -> ScopeGuard; - //============================================================================== class OpenGLContext::CachedImage : public CachedComponentImage {