mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-10 23:44:24 +00:00
Functional: Add ScopeGuard implementation
This commit is contained in:
parent
cbf59e185f
commit
0fbd7d7b3f
2 changed files with 27 additions and 7 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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…
Add table
Add a link
Reference in a new issue