Browse Source

Functional: Add ScopeGuard implementation

v7.0.9
reuk 2 years ago
parent
commit
0fbd7d7b3f
No known key found for this signature in database GPG Key ID: 9ADCD339CFC98A11
2 changed files with 27 additions and 7 deletions
  1. +27
    -0
      modules/juce_core/misc/juce_Functional.h
  2. +0
    -7
      modules/juce_opengl/opengl/juce_OpenGLContext.cpp

+ 27
- 0
modules/juce_core/misc/juce_Functional.h View File

@@ -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 <typename Fn> struct ScopeGuard : Fn { ~ScopeGuard() { Fn::operator()(); } };
template <typename Fn> ScopeGuard (Fn) -> ScopeGuard<Fn>;
} // namespace juce

+ 0
- 7
modules/juce_opengl/opengl/juce_OpenGLContext.cpp View File

@@ -91,13 +91,6 @@ static bool contextHasTextureNpotFeature()
return stringTokens.contains ("GL_ARB_texture_non_power_of_two");
}
template <typename Fn> struct ScopeGuard : Fn
{
~ScopeGuard() { Fn::operator()(); }
};
template <typename Fn> ScopeGuard (Fn) -> ScopeGuard<Fn>;
//==============================================================================
class OpenGLContext::CachedImage : public CachedComponentImage
{


Loading…
Cancel
Save