Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(171)

Unified Diff: third_party/WebKit/Source/wtf/MakeCancellable.h

Issue 2177283005: Add WTF::makeCancellable (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: +example Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/MakeCancellable.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/MakeCancellable.h
diff --git a/third_party/WebKit/Source/wtf/MakeCancellable.h b/third_party/WebKit/Source/wtf/MakeCancellable.h
new file mode 100644
index 0000000000000000000000000000000000000000..3b66eda9407ad4cfb835a45e5253ebd97a6c733e
--- /dev/null
+++ b/third_party/WebKit/Source/wtf/MakeCancellable.h
@@ -0,0 +1,122 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef WTF_MakeCancellable_h
+#define WTF_MakeCancellable_h
+
+#include "base/logging.h"
+#include "wtf/Functional.h"
+#include "wtf/RefCounted.h"
+#include "wtf/WTFExport.h"
+#include <memory>
+
+namespace WTF {
+
+class FunctionCanceller;
+
+namespace internal {
+
+class WTF_EXPORT FunctionCancellerImplBase : public RefCounted<FunctionCancellerImplBase> {
haraken 2016/07/27 11:18:04 Do we need WTF_EXPORT? It looks strange to export
tzik 2016/07/27 13:56:53 Yes, we need it to expose the ctor and dtor to Fun
+public:
+ virtual void cancel() = 0;
+
+protected:
+ FunctionCancellerImplBase();
+ virtual ~FunctionCancellerImplBase();
+ friend RefCounted<FunctionCancellerImplBase>;
+
+ DISALLOW_COPY_AND_ASSIGN(FunctionCancellerImplBase);
+};
+
+template <typename... Params>
+class FunctionCancellerImpl final : public FunctionCancellerImplBase {
+public:
+ FunctionCancellerImpl(std::unique_ptr<Function<void(Params...)>> function)
+ : m_function(std::move(function))
+ {
+ DCHECK(m_function);
+ }
+
+ void runUnlessCancelled(const FunctionCanceller&, Params... params)
+ {
+ if (m_function)
+ (*m_function)(std::forward<Params>(params)...);
+ }
+
+ void cancel() override
+ {
+ m_function = nullptr;
+ }
+
+private:
+ ~FunctionCancellerImpl() override = default;
+
+ std::unique_ptr<WTF::Function<void(Params...)>> m_function;
+
+ DISALLOW_COPY_AND_ASSIGN(FunctionCancellerImpl);
+};
+
+} // namespace internal
+
+class WTF_EXPORT FunctionCanceller {
+public:
+ FunctionCanceller();
+ explicit FunctionCanceller(PassRefPtr<internal::FunctionCancellerImplBase> cancellation);
+
+ FunctionCanceller(FunctionCanceller&&);
+ FunctionCanceller& operator=(FunctionCanceller&&);
+ FunctionCanceller(const FunctionCanceller&) = delete;
+ FunctionCanceller& operator=(const FunctionCanceller&) = delete;
+
+ ~FunctionCanceller();
+ void detach();
+ void cancel();
+
+private:
+ RefPtr<internal::FunctionCancellerImplBase> m_cancellation;
+};
+
+// makeCancellable wraps a WTF::Function to make cancellable function.
+// This function returns a tuple of a WTF::Function, and a FunctionCanceller.
+// An invocation of the resulting function is relayed to the original function
+// unless the FunctionCanceller is alive and its cancel() is not called.
haraken 2016/07/27 11:18:04 unless => if ?
tzik 2016/07/27 13:56:52 Done.
+//
+// Example:
+// void foo() {}
+// std::unique_ptr<Function<void()>> function = WTF::bind(&foo);
+//
+// std::unique_ptr<Function<void()>> wrappedFunction;
+// FunctionCanceller canceller;
+// std::tie(wrappedFunction, canceller) = makeCancellable(std::move(function));
haraken 2016/07/27 11:18:04 Instead of returning a pair of WTF::Function and F
tzik 2016/07/27 13:56:53 Hmm, since the Function will be posted to a messag
haraken 2016/07/27 14:05:46 Do you mean that the following pattern is complica
+//
+// (*wrappedFunction)(); // Not cancelled. foo() is called.
+// canceller.cancel();
+// (*wrappedFunction)(); // Cancelled. foo() is not called.
+//
+//
+// std::tie(wrappedFunction, canceller) = makeCancellable(bind(&foo));
+// {
+// // FunctionCanceller is movable. |wrappedFunction| will be cancelled
+// // by anotherCanceller.cancel() rather than canceller.cancel().
+// FunctionCanceller anotherCanceller = std::move(canceller);
+//
+// // Destructor of FunctionCanceller implies cancel(), unless detach() is
+// // called.
+// }
+//
+template <typename... Params>
+std::tuple<std::unique_ptr<Function<void(Params...)>>, FunctionCanceller>
+makeCancellable(std::unique_ptr<Function<void(Params...)>> function)
+{
+ using Impl = internal::FunctionCancellerImpl<Params...>;
+ RefPtr<Impl> impl = adoptRef(new Impl(std::move(function)));
+ // Keep a FunctionCanceller instance in the wrappedFunction itself, so that the target function
+ // is destroyed when the Function instance is destroyed.
+ std::unique_ptr<Function<void(Params...)>> wrappedFunction = bind(&Impl::runUnlessCancelled, impl, FunctionCanceller(impl));
+ return std::make_tuple(std::move(wrappedFunction), FunctionCanceller(std::move(impl)));
+}
+
+} // namespace WTF
+
+#endif // WTF_MakeCancellable_h
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/MakeCancellable.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698