Flutter Engine
fml::ScopedCleanupClosure Class Reference

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

#include <closure.h>

Public Member Functions

 ScopedCleanupClosure ()=default
 
 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/2]

fml::ScopedCleanupClosure::ScopedCleanupClosure ( )
default

◆ ScopedCleanupClosure() [2/2]

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

Definition at line 36 of file closure.h.

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

◆ ~ScopedCleanupClosure()

fml::ScopedCleanupClosure::~ScopedCleanupClosure ( )
inline

Definition at line 38 of file closure.h.

38  {
39  if (closure_) {
40  closure_();
41  }
42  }

Member Function Documentation

◆ Release()

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

Definition at line 50 of file closure.h.

Referenced by CreateEmbedderRenderTarget(), and FlutterEnginePostDartObject().

50  {
51  fml::closure closure = closure_;
52  closure_ = nullptr;
53  return closure;
54  }
std::function< void()> closure
Definition: closure.h:14

◆ SetClosure()

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

Definition at line 44 of file closure.h.

Referenced by FlutterEnginePostDartObject().

44  {
45  auto old_closure = closure_;
46  closure_ = closure;
47  return old_closure;
48  }
std::function< void()> closure
Definition: closure.h:14

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