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

Side by Side Diff: media/base/run_on_destruction.h

Issue 2778953003: media: Add RunOnDestruction
Patch Set: Created 3 years, 8 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 unified diff | Download patch
« no previous file with comments | « media/base/BUILD.gn ('k') | media/base/run_on_destruction_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 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 MEDIA_BASE_RUN_ON_DESTRUCTION_H_
6 #define MEDIA_BASE_RUN_ON_DESTRUCTION_H_
7
8 #include <memory>
9
10 #include "base/callback_forward.h"
11 #include "base/bind.h"
12 #include "base/tuple.h"
13 #include "base/memory/ptr_util.h"
14
15 // This is a helper utility to wrap a base::Callback such that if the
16 // callback is destructed before it has a chance to run (e.g. the callback is
17 // bound into a task in a task runner and the task is dropped), it will be run
18 // with a set of parameters passed into RunOnDestruction.
19 //
20 // Typical usage:
21 // foo->DoWorkAndReturnResult(
22 // media::RunOnDestruction(base::Bind(&Foo::OnResult, this), false));
23 //
24 // If the callback is destructed without running, it'll be called with "false".
25
26 // TODO(xhwang): This class really only makes sense for base::OnceCallback.
27 // However, mojo uses base::Callback in generated interfaces.
28
29
30 namespace media {
31 namespace internal {
32
33 template <typename CallbackType, typename Tuple, size_t... Ns>
34 inline void DispatchToCallbackImpl(CallbackType callback,
35 Tuple&& args,
36 base::IndexSequence<Ns...>) {
37 std::move(callback).Run(base::get<Ns>(std::forward<Tuple>(args))...);
38 }
39
40 template <typename CallbackType, typename Tuple>
41 inline void DispatchToCallback(CallbackType callback, Tuple&& args) {
42 DispatchToCallbackImpl(std::move(callback), std::forward<Tuple>(args),
43 base::MakeIndexSequenceForTuple<Tuple>());
44 }
45
46 // First, tell the compiler RunOnDestructionHelper is a struct template with one
47 // type parameter. Then define specializations where the type is a function
48 // returning void and taking zero or more arguments.
49 template <typename Signature>
50 class RunOnDestructionHelper;
51
52 template <typename... Args>
53 class RunOnDestructionHelper<void(Args...)> {
54 public:
55 using CallbackType = base::Callback<void(Args...)>;
56
57 RunOnDestructionHelper(CallbackType callback, Args... args)
58 : callback_(std::move(callback)),
59 args_(std::make_tuple(args...)) {
60 DCHECK(callback_);
61 }
62
63 inline void Run(Args... args);
64
65 ~RunOnDestructionHelper() {
66 if (callback_)
67 DispatchToCallback(std::move(callback_), args_);
68 }
69
70 private:
71 CallbackType callback_;
72 std::tuple<Args...> args_;
73 };
74
75 template <typename... Args>
76 inline void RunOnDestructionHelper<void(Args...)>::Run(Args... args) {
77 std::move(callback_).Run(args...);
78 }
79
80 } // namespace internal
81
82 template <typename T, typename... Args>
83 inline base::Callback<T> RunOnDestruction(base::Callback<T> cb, Args... args) {
84 return base::Bind(
85 &internal::RunOnDestructionHelper<T>::Run,
86 base::MakeUnique<internal::RunOnDestructionHelper<T>>(
87 std::move(cb), args...));
88 }
89
90 } // namespace media
91
92 #endif // MEDIA_BASE_RUN_ON_DESTRUCTION_H_
OLDNEW
« no previous file with comments | « media/base/BUILD.gn ('k') | media/base/run_on_destruction_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698