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 MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ | |
6 #define MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ | |
7 | |
8 #include "base/callback_forward.h" | |
9 | |
10 namespace media { | |
11 | |
12 // This class wraps a |callback_| and guarantees that it will be called on | |
miu
2016/05/28 01:29:56
This is a little inaccurate. It will guarantee tha
mcasas
2016/05/31 20:19:04
Rewritten.
| |
13 // destruction and according to the provided OnErrorCallback if it hasn't been | |
14 // retrieved before (via PassCallback()). Inspired by ScopedWebCallbacks | |
15 // template. | |
16 template <typename CallbackType> | |
17 class ScopedCallback { | |
miu
2016/05/28 01:29:56
For me, the name of this utility class was a bit c
mcasas
2016/05/31 20:19:04
If this class was to get traction, I think its bas
miu
2016/06/01 00:53:36
nit: ScopedResultCallback? (since this mechanism i
mcasas
2016/06/01 01:21:01
ScopedResultCallback sounds great.
| |
18 MOVE_ONLY_TYPE_FOR_CPP_03(ScopedCallback); | |
19 | |
20 public: | |
21 using OnErrorCallback = base::Callback<void(const CallbackType&)>; | |
22 ScopedCallback(const CallbackType& callback, | |
23 const OnErrorCallback& on_error_callback) | |
24 : callback_(callback), on_error_callback_(on_error_callback) {} | |
25 | |
26 ~ScopedCallback() { | |
27 if (!callback_.is_null()) | |
28 on_error_callback_.Run(callback_); | |
miu
2016/05/28 01:29:56
Just in case that |callback_| has a ref-counted po
mcasas
2016/05/31 20:19:04
Done.
(Note that base::ResetAndReturn() doesn't w
| |
29 } | |
30 | |
31 ScopedCallback(ScopedCallback&& other) { *this = std::move(other); } | |
32 | |
33 ScopedCallback& operator=(ScopedCallback&& other) { | |
34 callback_ = other.PassCallback(); | |
35 on_error_callback_ = other.on_error_callback_; | |
36 return *this; | |
37 } | |
38 | |
39 CallbackType PassCallback() { | |
miu
2016/05/28 01:29:56
A few things:
1. Instead of this method, would cl
mcasas
2016/05/31 20:19:04
Taken all in except the base::ResetAndReturn() for
miu
2016/06/01 00:53:36
Understood.
| |
40 CallbackType callback = callback_; | |
41 callback_.reset(); | |
42 return callback; | |
43 } | |
44 | |
45 private: | |
46 CallbackType callback_; | |
47 OnErrorCallback on_error_callback_; | |
48 }; | |
49 | |
50 } // namespace media | |
51 | |
52 #endif // MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ | |
OLD | NEW |