Chromium Code Reviews| Index: media/capture/video/scoped_callback.h |
| diff --git a/media/capture/video/scoped_callback.h b/media/capture/video/scoped_callback.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dd1b33460927f3ae45cea211fbebe7f5ae57dd26 |
| --- /dev/null |
| +++ b/media/capture/video/scoped_callback.h |
| @@ -0,0 +1,52 @@ |
| +// 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 MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |
| +#define MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |
| + |
| +#include "base/callback_forward.h" |
| + |
| +namespace media { |
| + |
| +// This class wraps a |callback_| and guarantees that it will be called on |
| +// destruction and according to the provided OnErrorCallback if it hasn't been |
| +// retrieved before (via PassCallback()). Inspired by ScopedWebCallbacks |
| +// template. |
| +template <typename CallbackType> |
| +class ScopedCallback { |
| + MOVE_ONLY_TYPE_FOR_CPP_03(ScopedCallback); |
| + |
| + public: |
| + using OnErrorCallback = base::Callback<void(const CallbackType&)>; |
| + ScopedCallback(const CallbackType& callback, |
| + const OnErrorCallback& on_error_callback) |
| + : callback_(std::move(callback)), on_error_callback_(on_error_callback) {} |
|
Reilly Grant (use Gerrit)
2016/05/26 00:27:45
nit: The move here and below are unnecessary.
mcasas
2016/05/26 17:24:04
Done.
|
| + |
| + ~ScopedCallback() { |
| + if (!callback_.is_null()) |
| + on_error_callback_.Run(callback_); |
| + } |
| + |
| + ScopedCallback(ScopedCallback&& other) { *this = std::move(other); } |
| + |
| + ScopedCallback& operator=(ScopedCallback&& other) { |
| + callback_ = other.PassCallback(); |
| + on_error_callback_ = other.on_error_callback_; |
| + return *this; |
| + } |
| + |
| + CallbackType PassCallback() { |
| + CallbackType callback = callback_; |
| + callback_.reset(); |
| + return std::move(callback); |
| + } |
| + |
| + private: |
| + CallbackType callback_; |
| + OnErrorCallback on_error_callback_; |
| +}; |
| + |
| +} // namespace media |
| + |
| +#endif // MEDIA_CAPTURE_VIDEO_SCOPED_CALLBACK_H_ |