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.
5 #ifndef TOOLS_ANDROID_FORWARDER2_UTIL_H_
6 #define TOOLS_ANDROID_FORWARDER2_UTIL_H_
8 #include "base/logging.h"
10 namespace forwarder2
{
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
17 template <typename Map
, typename K
>
18 bool DeleteRefCountedValueInMap(const K
& key
, Map
* map
) {
19 const typename
Map::iterator it
= map
->find(key
);
22 DeleteRefCountedValueInMapFromIterator(it
, map
);
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
;
34 } // namespace forwarder2
36 #endif // TOOLS_ANDROID_FORWARDER2_UTIL_H_