callback_list.cc 1018 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright 2020 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_list.h"
  5. #include <utility>
  6. #include "base/callback.h"
  7. namespace base {
  8. CallbackListSubscription::CallbackListSubscription() = default;
  9. CallbackListSubscription::CallbackListSubscription(base::OnceClosure closure)
  10. : closure_(std::move(closure)) {}
  11. CallbackListSubscription::CallbackListSubscription(
  12. CallbackListSubscription&& subscription)
  13. : closure_(std::move(subscription.closure_)) {}
  14. CallbackListSubscription& CallbackListSubscription::operator=(
  15. CallbackListSubscription&& subscription) {
  16. // Note: This still works properly for self-assignment.
  17. Run();
  18. closure_ = std::move(subscription.closure_);
  19. return *this;
  20. }
  21. CallbackListSubscription::~CallbackListSubscription() {
  22. Run();
  23. }
  24. void CallbackListSubscription::Run() {
  25. if (closure_)
  26. std::move(closure_).Run();
  27. }
  28. } // namespace base