Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 ()
 

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 {
49 if (closure_) {
50 closure_();
51 }
52 }

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 60 of file closure.h.

60 {
61 fml::closure closure = closure_;
62 closure_ = nullptr;
63 return closure;
64 }

◆ SetClosure()

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

Definition at line 54 of file closure.h.

54 {
55 auto old_closure = closure_;
56 closure_ = closure;
57 return old_closure;
58 }

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