1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file. 4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef TOOLS_ANDROID_FORWARDER2_UTIL_H_ 6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define TOOLS_ANDROID_FORWARDER2_UTIL_H_ 7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 8f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/logging.h" 9f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace forwarder2 { 11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Safely deletes a ref-counted value in a provided map by unlinking the object 13f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// from the map before deleting it in case its destructor would access the map. 14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Deletion will only happen by definition if the object's refcount is set to 1 15f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// before this function gets called. Returns whether the element could be found 16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// in the map. 17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)template <typename Map, typename K> 18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool DeleteRefCountedValueInMap(const K& key, Map* map) { 19f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const typename Map::iterator it = map->find(key); 20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) if (it == map->end()) 21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return false; 22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) DeleteRefCountedValueInMapFromIterator(it, map); 23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) return true; 24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// See DeleteRefCountedValuetInMap() above. 27f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)template <typename Map, typename Iterator> 28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)void DeleteRefCountedValueInMapFromIterator(Iterator it, Map* map) { 29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) DCHECK(it != map->end()); 30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) const typename Map::value_type::second_type shared_ptr_copy = it->second; 31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) map->erase(it); 32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} 33f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 34f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)} // namespace forwarder2 35f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) 36f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif // TOOLS_ANDROID_FORWARDER2_UTIL_H_ 37