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
{
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
21 class StoreRef final
{
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_
; }
33 RevocableStore
* store_
;
35 DISALLOW_EVIL_CONSTRUCTORS(StoreRef
);
38 // An item in the store. On construction, the object adds itself to the
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(); }
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
);
59 // Revokes all the items in the store.
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_