[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #include "base/callback_helpers.h" | ||||
6 | |||||
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 7 | namespace base { |
8 | |||||
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 9 | ScopedClosureRunner::ScopedClosureRunner() = default; |
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 10 | |
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 11 | ScopedClosureRunner::ScopedClosureRunner(OnceClosure closure) |
12 | : closure_(std::move(closure)) {} | ||||
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 13 | |
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 14 | ScopedClosureRunner::ScopedClosureRunner(ScopedClosureRunner&& other) |
15 | : closure_(other.Release()) {} | ||||
16 | |||||
17 | ScopedClosureRunner& ScopedClosureRunner::operator=( | ||||
18 | ScopedClosureRunner&& other) { | ||||
Peter Kasting | 323ccc8 | 2020-11-14 01:41:24 | [diff] [blame] | 19 | if (this != &other) { |
20 | RunAndReset(); | ||||
21 | ReplaceClosure(other.Release()); | ||||
22 | } | ||||
sergeyu | e4be191 | 2016-06-25 00:51:09 | [diff] [blame] | 23 | return *this; |
24 | } | ||||
25 | |||||
Peter Kasting | 323ccc8 | 2020-11-14 01:41:24 | [diff] [blame] | 26 | ScopedClosureRunner::~ScopedClosureRunner() { |
27 | RunAndReset(); | ||||
28 | } | ||||
29 | |||||
sergeyu | 668613aa | 2016-07-08 00:34:27 | [diff] [blame] | 30 | void ScopedClosureRunner::RunAndReset() { |
Peter Kasting | 323ccc8 | 2020-11-14 01:41:24 | [diff] [blame] | 31 | if (closure_) |
32 | std::move(closure_).Run(); | ||||
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 33 | } |
34 | |||||
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 35 | void ScopedClosureRunner::ReplaceClosure(OnceClosure closure) { |
36 | closure_ = std::move(closure); | ||||
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 37 | } |
38 | |||||
tzik | 398065c | 2017-08-08 05:19:40 | [diff] [blame] | 39 | OnceClosure ScopedClosureRunner::Release() { |
40 | return std::move(closure_); | ||||
[email protected] | 1c232c2 | 2013-08-30 02:04:04 | [diff] [blame] | 41 | } |
42 | |||||
43 | } // namespace base |