|
1 // Copyright (c) 2006-2008 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/revocable_store.h" |
|
6 |
|
7 #include "base/logging.h" |
|
8 |
|
9 RevocableStore::Revocable::Revocable(RevocableStore* store) |
|
10 : store_reference_(store->owning_reference_) { |
|
11 // We AddRef() the owning reference. |
|
12 DCHECK(store_reference_->store()); |
|
13 store_reference_->store()->Add(this); |
|
14 } |
|
15 |
|
16 RevocableStore::Revocable::~Revocable() { |
|
17 if (!revoked()) { |
|
18 // Notify the store of our destruction. |
|
19 --(store_reference_->store()->count_); |
|
20 } |
|
21 } |
|
22 |
|
23 RevocableStore::RevocableStore() : count_(0) { |
|
24 // Create a new owning reference. |
|
25 owning_reference_ = new StoreRef(this); |
|
26 } |
|
27 |
|
28 RevocableStore::~RevocableStore() { |
|
29 // Revoke all the items in the store. |
|
30 owning_reference_->set_store(NULL); |
|
31 } |
|
32 |
|
33 void RevocableStore::Add(Revocable* item) { |
|
34 DCHECK(!item->revoked()); |
|
35 ++count_; |
|
36 } |
|
37 |
|
38 void RevocableStore::RevokeAll() { |
|
39 // We revoke all the existing items in the store and reset our count. |
|
40 owning_reference_->set_store(NULL); |
|
41 count_ = 0; |
|
42 |
|
43 // Then we create a new owning reference for new items that get added. |
|
44 // This Release()s the old owning reference, allowing it to be freed after |
|
45 // all the items that were in the store are eventually destroyed. |
|
46 owning_reference_ = new StoreRef(this); |
|
47 } |