Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2012 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_internal.h"
      6 
      7 #include "base/logging.h"
      8 
      9 namespace base {
     10 namespace internal {
     11 
     12 namespace {
     13 
     14 bool ReturnFalse(const BindStateBase*) {
     15   return false;
     16 }
     17 
     18 }  // namespace
     19 
     20 void BindStateBaseRefCountTraits::Destruct(const BindStateBase* bind_state) {
     21   bind_state->destructor_(bind_state);
     22 }
     23 
     24 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
     25                              void (*destructor)(const BindStateBase*))
     26     : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
     27 }
     28 
     29 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
     30                              void (*destructor)(const BindStateBase*),
     31                              bool (*is_cancelled)(const BindStateBase*))
     32     : polymorphic_invoke_(polymorphic_invoke),
     33       destructor_(destructor),
     34       is_cancelled_(is_cancelled) {}
     35 
     36 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default;
     37 
     38 CallbackBase<CopyMode::MoveOnly>&
     39 CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default;
     40 
     41 CallbackBase<CopyMode::MoveOnly>::CallbackBase(
     42     const CallbackBase<CopyMode::Copyable>& c)
     43     : bind_state_(c.bind_state_) {}
     44 
     45 CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=(
     46     const CallbackBase<CopyMode::Copyable>& c) {
     47   bind_state_ = c.bind_state_;
     48   return *this;
     49 }
     50 
     51 CallbackBase<CopyMode::MoveOnly>::CallbackBase(
     52     CallbackBase<CopyMode::Copyable>&& c)
     53     : bind_state_(std::move(c.bind_state_)) {}
     54 
     55 CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=(
     56     CallbackBase<CopyMode::Copyable>&& c) {
     57   bind_state_ = std::move(c.bind_state_);
     58   return *this;
     59 }
     60 
     61 void CallbackBase<CopyMode::MoveOnly>::Reset() {
     62   // NULL the bind_state_ last, since it may be holding the last ref to whatever
     63   // object owns us, and we may be deleted after that.
     64   bind_state_ = nullptr;
     65 }
     66 
     67 bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const {
     68   DCHECK(bind_state_);
     69   return bind_state_->IsCancelled();
     70 }
     71 
     72 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
     73     const CallbackBase& other) const {
     74   return bind_state_ == other.bind_state_;
     75 }
     76 
     77 CallbackBase<CopyMode::MoveOnly>::CallbackBase(BindStateBase* bind_state)
     78     : bind_state_(bind_state ? AdoptRef(bind_state) : nullptr) {
     79   DCHECK(!bind_state_.get() || bind_state_->HasOneRef());
     80 }
     81 
     82 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
     83 
     84 CallbackBase<CopyMode::Copyable>::CallbackBase(
     85     const CallbackBase& c)
     86     : CallbackBase<CopyMode::MoveOnly>(nullptr) {
     87   bind_state_ = c.bind_state_;
     88 }
     89 
     90 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default;
     91 
     92 CallbackBase<CopyMode::Copyable>&
     93 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
     94   bind_state_ = c.bind_state_;
     95   return *this;
     96 }
     97 
     98 CallbackBase<CopyMode::Copyable>&
     99 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default;
    100 
    101 template class CallbackBase<CopyMode::MoveOnly>;
    102 template class CallbackBase<CopyMode::Copyable>;
    103 
    104 }  // namespace internal
    105 }  // namespace base
    106