OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium 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 WTF_MakeCancellable_h |
| 6 #define WTF_MakeCancellable_h |
| 7 |
| 8 #include "base/logging.h" |
| 9 #include "wtf/Functional.h" |
| 10 #include "wtf/RefCounted.h" |
| 11 #include "wtf/WTFExport.h" |
| 12 #include <memory> |
| 13 |
| 14 namespace WTF { |
| 15 |
| 16 class ScopedFunctionCanceller; |
| 17 |
| 18 namespace internal { |
| 19 |
| 20 class WTF_EXPORT FunctionCanceller : public RefCounted<FunctionCanceller> { |
| 21 public: |
| 22 virtual void cancel() = 0; |
| 23 virtual bool isActive() const = 0; |
| 24 |
| 25 protected: |
| 26 FunctionCanceller(); |
| 27 virtual ~FunctionCanceller(); |
| 28 friend RefCounted<FunctionCanceller>; |
| 29 |
| 30 DISALLOW_COPY_AND_ASSIGN(FunctionCanceller); |
| 31 }; |
| 32 |
| 33 template <typename... Params> |
| 34 class FunctionCancellerImpl final : public FunctionCanceller { |
| 35 public: |
| 36 FunctionCancellerImpl(std::unique_ptr<Function<void(Params...)>> function) |
| 37 : m_function(std::move(function)) |
| 38 { |
| 39 DCHECK(m_function); |
| 40 } |
| 41 |
| 42 void runUnlessCancelled(const ScopedFunctionCanceller&, Params... params) |
| 43 { |
| 44 if (m_function) |
| 45 (*m_function)(std::forward<Params>(params)...); |
| 46 } |
| 47 |
| 48 void cancel() override |
| 49 { |
| 50 m_function = nullptr; |
| 51 } |
| 52 |
| 53 bool isActive() const override |
| 54 { |
| 55 return !!m_function; |
| 56 } |
| 57 |
| 58 private: |
| 59 ~FunctionCancellerImpl() override = default; |
| 60 |
| 61 std::unique_ptr<WTF::Function<void(Params...)>> m_function; |
| 62 |
| 63 DISALLOW_COPY_AND_ASSIGN(FunctionCancellerImpl); |
| 64 }; |
| 65 |
| 66 } // namespace internal |
| 67 |
| 68 // ScopedFunctionCanceller is a handle associated to a Function, and cancels the |
| 69 // invocation of the Function on the scope out or cancel() call. |
| 70 // Example: |
| 71 // void Foo() {} |
| 72 // |
| 73 // std::unique_ptr<Closure> f = bind(&Foo); |
| 74 // auto result = makeCancellable(std::move(f)); |
| 75 // |
| 76 // { |
| 77 // ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); |
| 78 // // Scope out of |scopedCanceller| cancels Foo invocation. |
| 79 // // (*result.function)(); will be no-op. |
| 80 // } |
| 81 // |
| 82 // ScopedFunctionCanceller scopedCanceller = std::move(result.canceller); |
| 83 // |
| 84 // // Manual cancellation is also available. This cancels the invocation |
| 85 // // of Foo too. |
| 86 // scopedCanceller.cancel(); |
| 87 // |
| 88 // // detach() unassociates the FunctionCanceller instance without cancelling |
| 89 // // it. After detach() call, the destructor nor cancel() no longer cancels |
| 90 // // the invocation of Foo. |
| 91 // scopedCanceller.detach(); |
| 92 // |
| 93 class WTF_EXPORT ScopedFunctionCanceller { |
| 94 DISALLOW_NEW(); |
| 95 public: |
| 96 ScopedFunctionCanceller(); |
| 97 explicit ScopedFunctionCanceller(PassRefPtr<internal::FunctionCanceller>); |
| 98 |
| 99 ScopedFunctionCanceller(ScopedFunctionCanceller&&); |
| 100 ScopedFunctionCanceller& operator=(ScopedFunctionCanceller&&); |
| 101 ScopedFunctionCanceller(const ScopedFunctionCanceller&) = delete; |
| 102 ScopedFunctionCanceller& operator=(const ScopedFunctionCanceller&) = delete; |
| 103 |
| 104 ~ScopedFunctionCanceller(); |
| 105 void detach(); |
| 106 void cancel(); |
| 107 bool isActive() const; |
| 108 |
| 109 private: |
| 110 RefPtr<internal::FunctionCanceller> m_canceller; |
| 111 }; |
| 112 |
| 113 template <typename... Params> |
| 114 struct MakeCancellableResult { |
| 115 ScopedFunctionCanceller canceller; |
| 116 std::unique_ptr<Function<void(Params...)>> function; |
| 117 |
| 118 MakeCancellableResult(ScopedFunctionCanceller canceller, std::unique_ptr<Fun
ction<void(Params...)>> function) |
| 119 : canceller(std::move(canceller)), function(std::move(function)) { } |
| 120 }; |
| 121 |
| 122 // makeCancellable wraps a WTF::Function to make the function cancellable. |
| 123 // This function returns a WTF::Function, and a ScopedFunctionCanceller. |
| 124 // An invocation of the resulting function is relayed to the original function |
| 125 // if the resulting ScopedFunctionCanceller is alive and the cancel() is not |
| 126 // called. |
| 127 // The inner Function that is passed to makeCancellable() will be destroyed |
| 128 // when it's cancelled or the outer Function is destroyed. |
| 129 // |
| 130 // Example: |
| 131 // void foo() {} |
| 132 // std::unique_ptr<Function<void()>> function = WTF::bind(&foo); |
| 133 // |
| 134 // auto result = makeCancellable(std::move(function)); |
| 135 // |
| 136 // (*result.function)(); // Not cancelled. foo() is called. |
| 137 // result.canceller.cancel(); |
| 138 // (*result.function)(); // Cancelled. foo() is not called. |
| 139 // |
| 140 template <typename... Params> |
| 141 MakeCancellableResult<Params...> makeCancellable(std::unique_ptr<Function<void(P
arams...)>> function) |
| 142 { |
| 143 using Canceller = internal::FunctionCancellerImpl<Params...>; |
| 144 RefPtr<Canceller> canceller = adoptRef(new Canceller(std::move(function))); |
| 145 |
| 146 // Implementation note: |
| 147 // Keep a ScopedFunctionCanceller instance in |wrappedFunction| below, so |
| 148 // that the destruction of |wrappedFunction| implies the destruction of |
| 149 // |function|. This is needed to avoid a circular strong reference among a |
| 150 // bound parameter, Function, and FunctionCanceller. |
| 151 // |
| 152 // E.g.: |
| 153 // struct Foo : GarbageCollectedFinalized<Foo> { |
| 154 // RefPtr<FunctionCanceller> m_canceller; |
| 155 // void bar(); |
| 156 // }; |
| 157 // |
| 158 // Foo* foo = new Foo; |
| 159 // auto result = makeCancellable(bind(&Foo::bar, wrapPersistent(foo))); |
| 160 // |
| 161 // // Destruction of the resulting Function implies the destruction of |
| 162 // // the original function via ScopedFunctionCanceller below, that |
| 163 // // resolves a circular strong reference: |
| 164 // // foo -> m_canceller -> m_function -> foo |
| 165 // result.function = nullptr; |
| 166 auto wrappedFunction = bind(&Canceller::runUnlessCancelled, canceller, Scope
dFunctionCanceller(canceller)); |
| 167 return MakeCancellableResult<Params...>(ScopedFunctionCanceller(canceller.re
lease()), std::move(wrappedFunction)); |
| 168 } |
| 169 |
| 170 } // namespace WTF |
| 171 |
| 172 #endif // WTF_MakeCancellable_h |
OLD | NEW |