callback_helpers.cc 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. #include "base/callback_helpers.h"
  5. namespace base {
  6. ScopedClosureRunner::ScopedClosureRunner() = default;
  7. ScopedClosureRunner::ScopedClosureRunner(OnceClosure closure)
  8. : closure_(std::move(closure)) {}
  9. ScopedClosureRunner::ScopedClosureRunner(ScopedClosureRunner&& other)
  10. : closure_(other.Release()) {}
  11. ScopedClosureRunner& ScopedClosureRunner::operator=(
  12. ScopedClosureRunner&& other) {
  13. if (this != &other) {
  14. RunAndReset();
  15. ReplaceClosure(other.Release());
  16. }
  17. return *this;
  18. }
  19. ScopedClosureRunner::~ScopedClosureRunner() {
  20. RunAndReset();
  21. }
  22. void ScopedClosureRunner::RunAndReset() {
  23. if (closure_)
  24. std::move(closure_).Run();
  25. }
  26. void ScopedClosureRunner::ReplaceClosure(OnceClosure closure) {
  27. closure_ = std::move(closure);
  28. }
  29. OnceClosure ScopedClosureRunner::Release() {
  30. return std::move(closure_);
  31. }
  32. } // namespace base