ipc/chromium/src/base/revocable_store.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/ipc/chromium/src/base/revocable_store.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,74 @@
     1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
     1.5 +// Use of this source code is governed by a BSD-style license that can be
     1.6 +// found in the LICENSE file.
     1.7 +
     1.8 +#ifndef BASE_REVOCABLE_STORE_H_
     1.9 +#define BASE_REVOCABLE_STORE_H_
    1.10 +
    1.11 +#include "base/ref_counted.h"
    1.12 +
    1.13 +// |RevocableStore| is a container of items that can be removed from the store.
    1.14 +class RevocableStore {
    1.15 + public:
    1.16 +  // A |StoreRef| is used to link the |RevocableStore| to its items.  There is
    1.17 +  // one StoreRef per store, and each item holds a reference to it.  If the
    1.18 +  // store wishes to revoke its items, it sets |store_| to null.  Items are
    1.19 +  // permitted to release their reference to the |StoreRef| when they no longer
    1.20 +  // require the store.
    1.21 +  class StoreRef : public base::RefCounted<StoreRef> {
    1.22 +   public:
    1.23 +    StoreRef(RevocableStore* store) : store_(store) { }
    1.24 +
    1.25 +    void set_store(RevocableStore* store) { store_ = store; }
    1.26 +    RevocableStore* store() const { return store_; }
    1.27 +
    1.28 +   private:
    1.29 +    RevocableStore* store_;
    1.30 +
    1.31 +    DISALLOW_EVIL_CONSTRUCTORS(StoreRef);
    1.32 +  };
    1.33 +
    1.34 +  // An item in the store.  On construction, the object adds itself to the
    1.35 +  // store.
    1.36 +  class Revocable {
    1.37 +   public:
    1.38 +    Revocable(RevocableStore* store);
    1.39 +    ~Revocable();
    1.40 +
    1.41 +    // This item has been revoked if it no longer has a pointer to the store.
    1.42 +    bool revoked() const { return !store_reference_->store(); }
    1.43 +
    1.44 +  private:
    1.45 +    // We hold a reference to the store through this ref pointer.  We release
    1.46 +    // this reference on destruction.
    1.47 +    scoped_refptr<StoreRef> store_reference_;
    1.48 +
    1.49 +    DISALLOW_EVIL_CONSTRUCTORS(Revocable);
    1.50 +  };
    1.51 +
    1.52 +  RevocableStore();
    1.53 +  ~RevocableStore();
    1.54 +
    1.55 +  // Revokes all the items in the store.
    1.56 +  void RevokeAll();
    1.57 +
    1.58 +  // Returns true if there are no items in the store.
    1.59 +  bool empty() const { return count_ == 0; }
    1.60 +
    1.61 + private:
    1.62 +  friend class Revocable;
    1.63 +
    1.64 +  // Adds an item to the store.  To add an item to the store, construct it
    1.65 +  // with a pointer to the store.
    1.66 +  void Add(Revocable* item);
    1.67 +
    1.68 +  // This is the reference the unrevoked items in the store hold.
    1.69 +  scoped_refptr<StoreRef> owning_reference_;
    1.70 +
    1.71 +  // The number of unrevoked items in the store.
    1.72 +  int count_;
    1.73 +
    1.74 +  DISALLOW_EVIL_CONSTRUCTORS(RevocableStore);
    1.75 +};
    1.76 +
    1.77 +#endif  // BASE_REVOCABLE_STORE_H_

mercurial