| 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 | |
| 7 | namespace base { |
| 8 | |
| 9 | ScopedClosureRunner::ScopedClosureRunner() = default; |
| 10 | |
| 11 | ScopedClosureRunner::ScopedClosureRunner(OnceClosure closure) |
| 12 | : closure_(std::move(closure)) {} |
| 13 | |
| 14 | ScopedClosureRunner::~ScopedClosureRunner() { |
| 15 | if (!closure_.is_null()) |
| 16 | std::move(closure_).Run(); |
| 17 | } |
| 18 | |
| 19 | ScopedClosureRunner::ScopedClosureRunner(ScopedClosureRunner&& other) |
| 20 | : closure_(other.Release()) {} |
| 21 | |
| 22 | ScopedClosureRunner& ScopedClosureRunner::operator=( |
| 23 | ScopedClosureRunner&& other) { |
| 24 | ReplaceClosure(other.Release()); |
| 25 | return *this; |
| 26 | } |
| 27 | |
| 28 | void ScopedClosureRunner::RunAndReset() { |
| 29 | std::move(closure_).Run(); |
| 30 | } |
| 31 | |
| 32 | void ScopedClosureRunner::ReplaceClosure(OnceClosure closure) { |
| 33 | closure_ = std::move(closure); |
| 34 | } |
| 35 | |
| 36 | OnceClosure ScopedClosureRunner::Release() { |
| 37 | return std::move(closure_); |
| 38 | } |
| 39 | |
| 40 | } // namespace base |
| 41 | |