Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
4 : // Use of this source code is governed by a BSD-style license that can be
5 : // found in the LICENSE file.
6 :
7 : #include "base/revocable_store.h"
8 :
9 : #include "base/logging.h"
10 :
11 0 : RevocableStore::Revocable::Revocable(RevocableStore* store)
12 0 : : store_reference_(store->owning_reference_) {
13 : // We AddRef() the owning reference.
14 0 : DCHECK(store_reference_->store());
15 0 : store_reference_->store()->Add(this);
16 0 : }
17 :
18 0 : RevocableStore::Revocable::~Revocable() {
19 0 : if (!revoked()) {
20 : // Notify the store of our destruction.
21 0 : --(store_reference_->store()->count_);
22 : }
23 0 : }
24 :
25 44 : RevocableStore::RevocableStore() : count_(0) {
26 : // Create a new owning reference.
27 44 : owning_reference_ = new StoreRef(this);
28 44 : }
29 :
30 26 : RevocableStore::~RevocableStore() {
31 : // Revoke all the items in the store.
32 13 : owning_reference_->set_store(NULL);
33 13 : }
34 :
35 0 : void RevocableStore::Add(Revocable* item) {
36 0 : DCHECK(!item->revoked());
37 0 : ++count_;
38 0 : }
39 :
40 0 : void RevocableStore::RevokeAll() {
41 : // We revoke all the existing items in the store and reset our count.
42 0 : owning_reference_->set_store(NULL);
43 0 : count_ = 0;
44 :
45 : // Then we create a new owning reference for new items that get added.
46 : // This Release()s the old owning reference, allowing it to be freed after
47 : // all the items that were in the store are eventually destroyed.
48 0 : owning_reference_ = new StoreRef(this);
49 0 : }
|