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