Flutter Engine
The Flutter Engine
Public Member Functions | List of all members
fml::ScopedCleanupClosure Class Referencefinal

Wraps a closure that is invoked in the destructor unless released by the caller. More...

#include <closure.h>

Public Member Functions

 ScopedCleanupClosure ()=default
 
 ScopedCleanupClosure (ScopedCleanupClosure &&other)
 
ScopedCleanupClosureoperator= (ScopedCleanupClosure &&other)
 
 ScopedCleanupClosure (const fml::closure &closure)
 
 ~ScopedCleanupClosure ()
 
fml::closure SetClosure (const fml::closure &closure)
 
fml::closure Release ()
 
void Reset ()
 

Detailed Description

Wraps a closure that is invoked in the destructor unless released by the caller.

This is especially useful in dealing with APIs that return a resource by accepting ownership of a sub-resource and a closure that releases that resource. When such APIs are chained, each link in the chain must check that the next member in the chain has accepted the resource. If not, it must invoke the closure eagerly. Not doing this results in a resource leak in the erroneous case. Using this wrapper, the closure can be released once the next call in the chain has successfully accepted ownership of the resource. If not, the closure gets invoked automatically at the end of the scope. This covers the cases where there are early returns as well.

Definition at line 32 of file closure.h.

Constructor & Destructor Documentation

◆ ScopedCleanupClosure() [1/3]

fml::ScopedCleanupClosure::ScopedCleanupClosure ( )
default

◆ ScopedCleanupClosure() [2/3]

fml::ScopedCleanupClosure::ScopedCleanupClosure ( ScopedCleanupClosure &&  other)
inline

Definition at line 36 of file closure.h.

36 {
37 closure_ = other.Release();
38 }

◆ ScopedCleanupClosure() [3/3]

fml::ScopedCleanupClosure::ScopedCleanupClosure ( const fml::closure closure)
inlineexplicit

Definition at line 45 of file closure.h.

46 : closure_(closure) {}
std::function< void()> closure
Definition: closure.h:14

◆ ~ScopedCleanupClosure()

fml::ScopedCleanupClosure::~ScopedCleanupClosure ( )
inline

Definition at line 48 of file closure.h.

48{ Reset(); }

Member Function Documentation

◆ operator=()

ScopedCleanupClosure & fml::ScopedCleanupClosure::operator= ( ScopedCleanupClosure &&  other)
inline

Definition at line 40 of file closure.h.

40 {
41 closure_ = other.Release();
42 return *this;
43 }

◆ Release()

fml::closure fml::ScopedCleanupClosure::Release ( )
inline

Definition at line 56 of file closure.h.

56 {
57 fml::closure closure = closure_;
58 closure_ = nullptr;
59 return closure;
60 }

◆ Reset()

void fml::ScopedCleanupClosure::Reset ( )
inline

Definition at line 62 of file closure.h.

62 {
63 if (closure_) {
64 closure_();
65 closure_ = nullptr;
66 }
67 }

◆ SetClosure()

fml::closure fml::ScopedCleanupClosure::SetClosure ( const fml::closure closure)
inline

Definition at line 50 of file closure.h.

50 {
51 auto old_closure = closure_;
52 closure_ = closure;
53 return old_closure;
54 }

The documentation for this class was generated from the following file: