ipc/chromium/src/base/revocable_store.cc

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/revocable_store.cc	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,47 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#include "base/revocable_store.h"
     1.9 +
    1.10 +#include "base/logging.h"
    1.11 +
    1.12 +RevocableStore::Revocable::Revocable(RevocableStore* store)
    1.13 +    : store_reference_(store->owning_reference_) {
    1.14 +  // We AddRef() the owning reference.
    1.15 +  DCHECK(store_reference_->store());
    1.16 +  store_reference_->store()->Add(this);
    1.17 +}
    1.18 +
    1.19 +RevocableStore::Revocable::~Revocable() {
    1.20 +  if (!revoked()) {
    1.21 +    // Notify the store of our destruction.
    1.22 +    --(store_reference_->store()->count_);
    1.23 +  }
    1.24 +}
    1.25 +
    1.26 +RevocableStore::RevocableStore() : count_(0) {
    1.27 +  // Create a new owning reference.
    1.28 +  owning_reference_ = new StoreRef(this);
    1.29 +}
    1.30 +
    1.31 +RevocableStore::~RevocableStore() {
    1.32 +  // Revoke all the items in the store.
    1.33 +  owning_reference_->set_store(NULL);
    1.34 +}
    1.35 +
    1.36 +void RevocableStore::Add(Revocable* item) {
    1.37 +  DCHECK(!item->revoked());
    1.38 +  ++count_;
    1.39 +}
    1.40 +
    1.41 +void RevocableStore::RevokeAll() {
    1.42 +  // We revoke all the existing items in the store and reset our count.
    1.43 +  owning_reference_->set_store(NULL);
    1.44 +  count_ = 0;
    1.45 +
    1.46 +  // Then we create a new owning reference for new items that get added.
    1.47 +  // This Release()s the old owning reference, allowing it to be freed after
    1.48 +  // all the items that were in the store are eventually destroyed.
    1.49 +  owning_reference_ = new StoreRef(this);
    1.50 +}

mercurial