OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/supports_user_data.h" | 5 #include "base/supports_user_data.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 SupportsUserData::SupportsUserData() { | 11 SupportsUserData::SupportsUserData() { |
12 // Harmless to construct on a different execution sequence to subsequent | 12 // Harmless to construct on a different execution sequence to subsequent |
13 // usage. | 13 // usage. |
14 sequence_checker_.DetachFromSequence(); | 14 sequence_checker_.DetachFromSequence(); |
15 } | 15 } |
16 | 16 |
17 SupportsUserData::Data* SupportsUserData::GetUserData(const void* key) const { | 17 SupportsUserData::Data* SupportsUserData::GetUserData(const void* key) const { |
18 DCHECK(sequence_checker_.CalledOnValidSequence()); | 18 DCHECK(sequence_checker_.CalledOnValidSequence()); |
19 DataMap::const_iterator found = user_data_.find(key); | 19 DataMap::const_iterator found = user_data_.find(key); |
20 if (found != user_data_.end()) | 20 if (found != user_data_.end()) |
21 return found->second.get(); | 21 return found->second.get(); |
22 return NULL; | 22 return NULL; |
23 } | 23 } |
24 | 24 |
25 void SupportsUserData::SetUserData(const void* key, Data* data) { | |
26 SetUserData(key, WrapUnique(data)); | |
27 } | |
28 | |
29 void SupportsUserData::SetUserData(const void* key, | 25 void SupportsUserData::SetUserData(const void* key, |
30 std::unique_ptr<Data> data) { | 26 std::unique_ptr<Data> data) { |
31 DCHECK(sequence_checker_.CalledOnValidSequence()); | 27 DCHECK(sequence_checker_.CalledOnValidSequence()); |
32 user_data_[key] = std::move(data); | 28 user_data_[key] = std::move(data); |
33 } | 29 } |
34 | 30 |
35 void SupportsUserData::RemoveUserData(const void* key) { | 31 void SupportsUserData::RemoveUserData(const void* key) { |
36 DCHECK(sequence_checker_.CalledOnValidSequence()); | 32 DCHECK(sequence_checker_.CalledOnValidSequence()); |
37 user_data_.erase(key); | 33 user_data_.erase(key); |
38 } | 34 } |
39 | 35 |
40 void SupportsUserData::DetachFromSequence() { | 36 void SupportsUserData::DetachFromSequence() { |
41 sequence_checker_.DetachFromSequence(); | 37 sequence_checker_.DetachFromSequence(); |
42 } | 38 } |
43 | 39 |
44 SupportsUserData::~SupportsUserData() { | 40 SupportsUserData::~SupportsUserData() { |
45 DCHECK(sequence_checker_.CalledOnValidSequence() || user_data_.empty()); | 41 DCHECK(sequence_checker_.CalledOnValidSequence() || user_data_.empty()); |
46 DataMap local_user_data; | 42 DataMap local_user_data; |
47 user_data_.swap(local_user_data); | 43 user_data_.swap(local_user_data); |
48 // Now this->user_data_ is empty, and any destructors called transitively from | 44 // Now this->user_data_ is empty, and any destructors called transitively from |
49 // the destruction of |local_user_data| will see it that way instead of | 45 // the destruction of |local_user_data| will see it that way instead of |
50 // examining a being-destroyed object. | 46 // examining a being-destroyed object. |
51 } | 47 } |
52 | 48 |
53 } // namespace base | 49 } // namespace base |
OLD | NEW |