|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#ifndef FLUTTER_FML_CLOSURE_H_ |
| 6 | +#define FLUTTER_FML_CLOSURE_H_ |
| 7 | + |
| 8 | +#include <functional> |
| 9 | + |
| 10 | +#include "flutter/fml/macros.h" |
| 11 | + |
| 12 | +namespace fml { |
| 13 | + |
| 14 | +using closure = std::function<void()>; |
| 15 | + |
| 16 | +//------------------------------------------------------------------------------ |
| 17 | +/// @brief Wraps a closure that is invoked in the destructor unless |
| 18 | +/// released by the caller. |
| 19 | +/// |
| 20 | +/// This is especially useful in dealing with APIs that return a |
| 21 | +/// resource by accepting ownership of a sub-resource and a closure |
| 22 | +/// that releases that resource. When such APIs are chained, each |
| 23 | +/// link in the chain must check that the next member in the chain |
| 24 | +/// has accepted the resource. If not, it must invoke the closure |
| 25 | +/// eagerly. Not doing this results in a resource leak in the |
| 26 | +/// erroneous case. Using this wrapper, the closure can be released |
| 27 | +/// once the next call in the chain has successfully accepted |
| 28 | +/// ownership of the resource. If not, the closure gets invoked |
| 29 | +/// automatically at the end of the scope. This covers the cases |
| 30 | +/// where there are early returns as well. |
| 31 | +/// |
| 32 | +class ScopedCleanupClosure final { |
| 33 | + public: |
| 34 | + ScopedCleanupClosure() = default; |
| 35 | + |
| 36 | + ScopedCleanupClosure(ScopedCleanupClosure&& other) { |
| 37 | + closure_ = other.Release(); |
| 38 | + } |
| 39 | + |
| 40 | + ScopedCleanupClosure& operator=(ScopedCleanupClosure&& other) { |
| 41 | + closure_ = other.Release(); |
| 42 | + return *this; |
| 43 | + } |
| 44 | + |
| 45 | + explicit ScopedCleanupClosure(const fml::closure& closure) |
| 46 | + : closure_(closure) {} |
| 47 | + |
| 48 | + ~ScopedCleanupClosure() { Reset(); } |
| 49 | + |
| 50 | + fml::closure SetClosure(const fml::closure& closure) { |
| 51 | + auto old_closure = closure_; |
| 52 | + closure_ = closure; |
| 53 | + return old_closure; |
| 54 | + } |
| 55 | + |
| 56 | + fml::closure Release() { |
| 57 | + fml::closure closure = closure_; |
| 58 | + closure_ = nullptr; |
| 59 | + return closure; |
| 60 | + } |
| 61 | + |
| 62 | + void Reset() { |
| 63 | + if (closure_) { |
| 64 | + closure_(); |
| 65 | + closure_ = nullptr; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private: |
| 70 | + fml::closure closure_; |
| 71 | + |
| 72 | + FML_DISALLOW_COPY_AND_ASSIGN(ScopedCleanupClosure); |
| 73 | +}; |
| 74 | + |
| 75 | +} // namespace fml |
| 76 | + |
| 77 | +#endif // FLUTTER_FML_CLOSURE_H_ |
0 commit comments