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