Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / ipc / chromium / src / base / revocable_store.h
blob789c011cde4650e308f902faeceb862ec232f081
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.
7 #ifndef BASE_REVOCABLE_STORE_H_
8 #define BASE_REVOCABLE_STORE_H_
10 #include "base/basictypes.h"
11 #include "nsISupportsImpl.h"
13 // |RevocableStore| is a container of items that can be removed from the store.
14 class RevocableStore {
15 public:
16 // A |StoreRef| is used to link the |RevocableStore| to its items. There is
17 // one StoreRef per store, and each item holds a reference to it. If the
18 // store wishes to revoke its items, it sets |store_| to null. Items are
19 // permitted to release their reference to the |StoreRef| when they no longer
20 // require the store.
21 class StoreRef final {
22 public:
23 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(StoreRef)
24 explicit StoreRef(RevocableStore* aStore) : store_(aStore) {}
26 void set_store(RevocableStore* aStore) { store_ = aStore; }
27 RevocableStore* store() const { return store_; }
29 protected:
30 ~StoreRef() {}
32 private:
33 RevocableStore* store_;
35 DISALLOW_EVIL_CONSTRUCTORS(StoreRef);
38 // An item in the store. On construction, the object adds itself to the
39 // store.
40 class Revocable {
41 public:
42 explicit Revocable(RevocableStore* store);
43 ~Revocable() = default;
45 // This item has been revoked if it no longer has a pointer to the store.
46 bool revoked() const { return !store_reference_->store(); }
48 private:
49 // We hold a reference to the store through this ref pointer. We release
50 // this reference on destruction.
51 RefPtr<StoreRef> store_reference_;
53 DISALLOW_EVIL_CONSTRUCTORS(Revocable);
56 RevocableStore();
57 ~RevocableStore();
59 // Revokes all the items in the store.
60 void RevokeAll();
62 private:
63 friend class Revocable;
65 // Adds an item to the store. To add an item to the store, construct it
66 // with a pointer to the store.
67 void Add(Revocable* item);
69 // This is the reference the unrevoked items in the store hold.
70 RefPtr<StoreRef> owning_reference_;
72 DISALLOW_EVIL_CONSTRUCTORS(RevocableStore);
75 #endif // BASE_REVOCABLE_STORE_H_