|
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 #ifndef BASE_REVOCABLE_STORE_H_ |
|
6 #define BASE_REVOCABLE_STORE_H_ |
|
7 |
|
8 #include "base/ref_counted.h" |
|
9 |
|
10 // |RevocableStore| is a container of items that can be removed from the store. |
|
11 class RevocableStore { |
|
12 public: |
|
13 // A |StoreRef| is used to link the |RevocableStore| to its items. There is |
|
14 // one StoreRef per store, and each item holds a reference to it. If the |
|
15 // store wishes to revoke its items, it sets |store_| to null. Items are |
|
16 // permitted to release their reference to the |StoreRef| when they no longer |
|
17 // require the store. |
|
18 class StoreRef : public base::RefCounted<StoreRef> { |
|
19 public: |
|
20 StoreRef(RevocableStore* store) : store_(store) { } |
|
21 |
|
22 void set_store(RevocableStore* store) { store_ = store; } |
|
23 RevocableStore* store() const { return store_; } |
|
24 |
|
25 private: |
|
26 RevocableStore* store_; |
|
27 |
|
28 DISALLOW_EVIL_CONSTRUCTORS(StoreRef); |
|
29 }; |
|
30 |
|
31 // An item in the store. On construction, the object adds itself to the |
|
32 // store. |
|
33 class Revocable { |
|
34 public: |
|
35 Revocable(RevocableStore* store); |
|
36 ~Revocable(); |
|
37 |
|
38 // This item has been revoked if it no longer has a pointer to the store. |
|
39 bool revoked() const { return !store_reference_->store(); } |
|
40 |
|
41 private: |
|
42 // We hold a reference to the store through this ref pointer. We release |
|
43 // this reference on destruction. |
|
44 scoped_refptr<StoreRef> store_reference_; |
|
45 |
|
46 DISALLOW_EVIL_CONSTRUCTORS(Revocable); |
|
47 }; |
|
48 |
|
49 RevocableStore(); |
|
50 ~RevocableStore(); |
|
51 |
|
52 // Revokes all the items in the store. |
|
53 void RevokeAll(); |
|
54 |
|
55 // Returns true if there are no items in the store. |
|
56 bool empty() const { return count_ == 0; } |
|
57 |
|
58 private: |
|
59 friend class Revocable; |
|
60 |
|
61 // Adds an item to the store. To add an item to the store, construct it |
|
62 // with a pointer to the store. |
|
63 void Add(Revocable* item); |
|
64 |
|
65 // This is the reference the unrevoked items in the store hold. |
|
66 scoped_refptr<StoreRef> owning_reference_; |
|
67 |
|
68 // The number of unrevoked items in the store. |
|
69 int count_; |
|
70 |
|
71 DISALLOW_EVIL_CONSTRUCTORS(RevocableStore); |
|
72 }; |
|
73 |
|
74 #endif // BASE_REVOCABLE_STORE_H_ |