blob: 0dd5d6c3630709377471a835cf0fb23fe9d0d017 [file] [log] [blame]
[email protected]0f450362012-06-09 02:11:011// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]59eff912011-02-18 23:29:312// 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_internal.h"
6
Hans Wennborgc3cffa62020-04-27 10:09:127#include "base/check.h"
8#include "base/notreached.h"
[email protected]e24f8762011-12-20 00:10:049
[email protected]59eff912011-02-18 23:29:3110namespace base {
11namespace internal {
12
tzik1fdcca32016-09-14 07:15:0013namespace {
14
tzik9697d49e2018-08-02 13:35:1915bool QueryCancellationTraitsForNonCancellables(
16 const BindStateBase*,
17 BindStateBase::CancellationQueryMode mode) {
18 switch (mode) {
19 case BindStateBase::IS_CANCELLED:
20 return false;
21 case BindStateBase::MAYBE_VALID:
22 return true;
23 }
24 NOTREACHED();
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:4825}
26
tzik1fdcca32016-09-14 07:15:0027} // namespace
28
tzik65adef82017-03-30 06:45:2129void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) {
30 bind_state->destructor_(bind_state);
31}
32
tzik1fdcca32016-09-14 07:15:0033BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
tzik30e0c312016-09-21 08:06:5434 void (*destructor)(const BindStateBase*))
tzik9697d49e2018-08-02 13:35:1935 : BindStateBase(polymorphic_invoke,
36 destructor,
37 &QueryCancellationTraitsForNonCancellables) {}
tzik1fdcca32016-09-14 07:15:0038
tzik9697d49e2018-08-02 13:35:1939BindStateBase::BindStateBase(
40 InvokeFuncStorage polymorphic_invoke,
41 void (*destructor)(const BindStateBase*),
42 bool (*query_cancellation_traits)(const BindStateBase*,
43 CancellationQueryMode))
tzik1fdcca32016-09-14 07:15:0044 : polymorphic_invoke_(polymorphic_invoke),
tzik1fdcca32016-09-14 07:15:0045 destructor_(destructor),
tzik9697d49e2018-08-02 13:35:1946 query_cancellation_traits_(query_cancellation_traits) {}
tzikf1b377722016-09-08 06:58:0747
tzike4bb1a02018-05-21 08:05:1748CallbackBase& CallbackBase::operator=(CallbackBase&& c) noexcept = default;
tzikd4bb5b7d2017-08-28 19:08:5249CallbackBase::CallbackBase(const CallbackBaseCopyable& c)
tzik27d1e312016-09-13 05:28:5950 : bind_state_(c.bind_state_) {}
51
tzikd4bb5b7d2017-08-28 19:08:5252CallbackBase& CallbackBase::operator=(const CallbackBaseCopyable& c) {
tzik27d1e312016-09-13 05:28:5953 bind_state_ = c.bind_state_;
54 return *this;
55}
56
tzike4bb1a02018-05-21 08:05:1757CallbackBase::CallbackBase(CallbackBaseCopyable&& c) noexcept
tzikf44c2f8d2017-03-08 08:41:1558 : bind_state_(std::move(c.bind_state_)) {}
59
tzike4bb1a02018-05-21 08:05:1760CallbackBase& CallbackBase::operator=(CallbackBaseCopyable&& c) noexcept {
tzikf44c2f8d2017-03-08 08:41:1561 bind_state_ = std::move(c.bind_state_);
62 return *this;
63}
64
tzikd4bb5b7d2017-08-28 19:08:5265void CallbackBase::Reset() {
tzik77d411392016-03-09 09:47:0366 // NULL the bind_state_ last, since it may be holding the last ref to whatever
67 // object owns us, and we may be deleted after that.
68 bind_state_ = nullptr;
69}
70
tzikd4bb5b7d2017-08-28 19:08:5271bool CallbackBase::IsCancelled() const {
tzik59aa6bb12016-09-08 10:58:5372 DCHECK(bind_state_);
73 return bind_state_->IsCancelled();
74}
75
Nicolas Ouellet-payeur40f8e9a2018-07-30 16:26:4876bool CallbackBase::MaybeValid() const {
77 DCHECK(bind_state_);
78 return bind_state_->MaybeValid();
79}
80
tzikd4bb5b7d2017-08-28 19:08:5281bool CallbackBase::EqualsInternal(const CallbackBase& other) const {
tzik1886c272016-09-08 05:45:3882 return bind_state_ == other.bind_state_;
[email protected]59eff912011-02-18 23:29:3183}
84
Chris Watkinsbb7211c2017-11-29 07:16:3885CallbackBase::~CallbackBase() = default;
tzik77d411392016-03-09 09:47:0386
tzik787d420b2018-06-25 16:12:0287CallbackBaseCopyable::CallbackBaseCopyable(const CallbackBaseCopyable& c) {
tzik77d411392016-03-09 09:47:0388 bind_state_ = c.bind_state_;
[email protected]59eff912011-02-18 23:29:3189}
90
tzikd4bb5b7d2017-08-28 19:08:5291CallbackBaseCopyable& CallbackBaseCopyable::operator=(
92 const CallbackBaseCopyable& c) {
tzik77d411392016-03-09 09:47:0393 bind_state_ = c.bind_state_;
tzik77d411392016-03-09 09:47:0394 return *this;
95}
96
tzikd4bb5b7d2017-08-28 19:08:5297CallbackBaseCopyable& CallbackBaseCopyable::operator=(
tzike4bb1a02018-05-21 08:05:1798 CallbackBaseCopyable&& c) noexcept = default;
tzik77d411392016-03-09 09:47:0399
[email protected]59eff912011-02-18 23:29:31100} // namespace internal
[email protected]0f450362012-06-09 02:11:01101} // namespace base