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 | |
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/27 22:36:38
I think this exists already (in base/callback_help
mcasas
2016/05/27 23:36:06
Partially. There is ScopedClosureRunner(), which w
| |
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_); | |
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() { | |
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 |