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

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

Issue 2711453003: Refactor media::CallbackHolder (Closed)
Patch Set: style fix Created 3 years, 10 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MEDIA_BASE_CALLBACK_HOLDER_H_ 5 #ifndef MEDIA_BASE_CALLBACK_HOLDER_H_
6 #define MEDIA_BASE_CALLBACK_HOLDER_H_ 6 #define MEDIA_BASE_CALLBACK_HOLDER_H_
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/callback_helpers.h" 10 #include "base/callback_helpers.h"
11 #include "media/base/bind_to_current_loop.h"
12 11
13 namespace media { 12 namespace media {
14 13
15 // A helper class that can hold a callback from being fired. 14 // A helper class that can hold a callback from being fired.
16 template <typename CB> class CallbackHolder { 15 template <typename CB> class CallbackHolder {
17 public: 16 public:
18 CallbackHolder() : hold_(false) {} 17 CallbackHolder() : hold_(false) {}
19 18
20 ~CallbackHolder() { 19 ~CallbackHolder() {
21 // Make sure all callbacks are satisfied! 20 // Make sure all callbacks are satisfied!
(...skipping 10 matching lines...) Expand all
32 } 31 }
33 32
34 bool IsNull() const { 33 bool IsNull() const {
35 return original_cb_.is_null() && held_cb_.is_null(); 34 return original_cb_.is_null() && held_cb_.is_null();
36 } 35 }
37 36
38 // Holds the callback when Run() is called. 37 // Holds the callback when Run() is called.
39 void HoldCallback() { hold_ = true; } 38 void HoldCallback() { hold_ = true; }
40 39
41 // Runs or holds the callback as specified by |hold_|. 40 // Runs or holds the callback as specified by |hold_|.
42 // This method has overloaded versions to support different types of CB.
43 void RunOrHold() { 41 void RunOrHold() {
44 DCHECK(held_cb_.is_null()); 42 DCHECK(held_cb_.is_null());
45 if (hold_) 43 if (hold_)
46 held_cb_ = base::ResetAndReturn(&original_cb_); 44 held_cb_ = base::ResetAndReturn(&original_cb_);
47 else 45 else
48 base::ResetAndReturn(&original_cb_).Run(); 46 base::ResetAndReturn(&original_cb_).Run();
49 } 47 }
50 48
51 template <typename A1> void RunOrHold(A1 a1) { 49 template <typename... Args>
50 void RunOrHold(Args&&... args) {
52 DCHECK(held_cb_.is_null()); 51 DCHECK(held_cb_.is_null());
53 if (hold_) { 52 if (hold_) {
54 held_cb_ = base::Bind(base::ResetAndReturn(&original_cb_), 53 held_cb_ = base::BindOnce(base::ResetAndReturn(&original_cb_),
55 internal::TrampolineForward(a1)); 54 std::forward<Args>(args)...);
56 } else { 55 } else {
57 base::ResetAndReturn(&original_cb_).Run(a1); 56 base::ResetAndReturn(&original_cb_).Run(std::forward<Args>(args)...);
58 } 57 }
59 } 58 }
60 59
61 template <typename A1, typename A2> void RunOrHold(A1 a1, A2 a2) {
62 DCHECK(held_cb_.is_null());
63 if (hold_) {
64 held_cb_ = base::Bind(base::ResetAndReturn(&original_cb_),
65 internal::TrampolineForward(a1),
66 internal::TrampolineForward(a2));
67 } else {
68 base::ResetAndReturn(&original_cb_).Run(a1, a2);
69 }
70 }
71
72 // Releases and runs the held callback. 60 // Releases and runs the held callback.
73 void RunHeldCallback() { 61 void RunHeldCallback() {
74 DCHECK(hold_); 62 DCHECK(hold_);
75 DCHECK(!held_cb_.is_null()); 63 DCHECK(!held_cb_.is_null());
76 hold_ = false; 64 hold_ = false;
77 base::ResetAndReturn(&held_cb_).Run(); 65 base::ResetAndReturn(&held_cb_).Run();
78 } 66 }
79 67
80 private: 68 private:
81 bool hold_; 69 bool hold_;
82 CB original_cb_; 70 CB original_cb_;
83 base::Closure held_cb_; 71 base::OnceClosure held_cb_;
84 }; 72 };
85 73
86 } // namespace media 74 } // namespace media
87 75
88 #endif // MEDIA_BASE_CALLBACK_HOLDER_H_ 76 #endif // MEDIA_BASE_CALLBACK_HOLDER_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698