revocable_store.cc (1302B)
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. 6 7 #include "base/revocable_store.h" 8 9 #include "base/logging.h" 10 11 RevocableStore::Revocable::Revocable(RevocableStore* store) 12 : store_reference_(store->owning_reference_) { 13 // We AddRef() the owning reference. 14 DCHECK(store_reference_->store()); 15 store_reference_->store()->Add(this); 16 } 17 18 RevocableStore::RevocableStore() { 19 // Create a new owning reference. 20 owning_reference_ = new StoreRef(this); 21 } 22 23 RevocableStore::~RevocableStore() { 24 // Revoke all the items in the store. 25 owning_reference_->set_store(NULL); 26 } 27 28 void RevocableStore::Add(Revocable* item) { DCHECK(!item->revoked()); } 29 30 void RevocableStore::RevokeAll() { 31 // We revoke all the existing items in the store and reset our count. 32 owning_reference_->set_store(NULL); 33 34 // Then we create a new owning reference for new items that get added. 35 // This Release()s the old owning reference, allowing it to be freed after 36 // all the items that were in the store are eventually destroyed. 37 owning_reference_ = new StoreRef(this); 38 }