Skip to content

Commit

Permalink
Add const to BindStateBase to align to RefCounted constness
Browse files Browse the repository at this point in the history
Review-Url: https://codereview.chromium.org/2344143002
Cr-Commit-Position: refs/heads/master@{#420005}
  • Loading branch information
tzik authored and Commit bot committed Sep 21, 2016
1 parent 2de6151 commit 30e0c31
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions base/bind_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ struct BindState final : BindStateBase {

~BindState() {}

static void Destroy(BindStateBase* self) {
delete static_cast<BindState*>(self);
static void Destroy(const BindStateBase* self) {
delete static_cast<const BindState*>(self);
}
};

Expand Down
8 changes: 4 additions & 4 deletions base/callback_internal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,23 @@ bool ReturnFalse(const BindStateBase*) {
} // namespace

BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*))
void (*destructor)(const BindStateBase*))
: BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
}

BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*),
void (*destructor)(const BindStateBase*),
bool (*is_cancelled)(const BindStateBase*))
: polymorphic_invoke_(polymorphic_invoke),
ref_count_(0),
destructor_(destructor),
is_cancelled_(is_cancelled) {}

void BindStateBase::AddRef() {
void BindStateBase::AddRef() const {
AtomicRefCountInc(&ref_count_);
}

void BindStateBase::Release() {
void BindStateBase::Release() const {
if (!AtomicRefCountDec(&ref_count_))
destructor_(this);
}
Expand Down
12 changes: 6 additions & 6 deletions base/callback_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ class BASE_EXPORT BindStateBase {

protected:
BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*));
void (*destructor)(const BindStateBase*));
BindStateBase(InvokeFuncStorage polymorphic_invoke,
void (*destructor)(BindStateBase*),
void (*destructor)(const BindStateBase*),
bool (*is_cancelled)(const BindStateBase*));
~BindStateBase() = default;

Expand All @@ -51,19 +51,19 @@ class BASE_EXPORT BindStateBase {
return is_cancelled_(this);
}

void AddRef();
void Release();
void AddRef() const;
void Release() const;

// In C++, it is safe to cast function pointers to function pointers of
// another type. It is not okay to use void*. We create a InvokeFuncStorage
// that that can store our function pointer, and then cast it back to
// the original type on usage.
InvokeFuncStorage polymorphic_invoke_;

AtomicRefCount ref_count_;
mutable AtomicRefCount ref_count_;

// Pointer to a function that will properly destroy |this|.
void (*destructor_)(BindStateBase*);
void (*destructor_)(const BindStateBase*);
bool (*is_cancelled_)(const BindStateBase*);

DISALLOW_COPY_AND_ASSIGN(BindStateBase);
Expand Down
8 changes: 4 additions & 4 deletions base/callback_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ struct FakeBindState1 : internal::BindStateBase {
FakeBindState1() : BindStateBase(&NopInvokeFunc, &Destroy, &IsCancelled) {}
private:
~FakeBindState1() {}
static void Destroy(internal::BindStateBase* self) {
delete static_cast<FakeBindState1*>(self);
static void Destroy(const internal::BindStateBase* self) {
delete static_cast<const FakeBindState1*>(self);
}
static bool IsCancelled(const internal::BindStateBase*) {
return false;
Expand All @@ -37,8 +37,8 @@ struct FakeBindState2 : internal::BindStateBase {
FakeBindState2() : BindStateBase(&NopInvokeFunc, &Destroy, &IsCancelled) {}
private:
~FakeBindState2() {}
static void Destroy(internal::BindStateBase* self) {
delete static_cast<FakeBindState2*>(self);
static void Destroy(const internal::BindStateBase* self) {
delete static_cast<const FakeBindState2*>(self);
}
static bool IsCancelled(const internal::BindStateBase*) {
return false;
Expand Down

0 comments on commit 30e0c31

Please sign in to comment.