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 #ifndef BASE_SUPPORTS_USER_DATA_H_ | 5 #ifndef BASE_SUPPORTS_USER_DATA_H_ |
6 #define BASE_SUPPORTS_USER_DATA_H_ | 6 #define BASE_SUPPORTS_USER_DATA_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/ref_counted.h" |
12 | 13 |
13 namespace base { | 14 namespace base { |
14 | 15 |
15 // This is a helper for classes that want to allow users to stash random data by | 16 // This is a helper for classes that want to allow users to stash random data by |
16 // key. At destruction all the objects will be destructed. | 17 // key. At destruction all the objects will be destructed. |
17 class BASE_EXPORT SupportsUserData { | 18 class BASE_EXPORT SupportsUserData { |
18 public: | 19 public: |
19 SupportsUserData(); | 20 SupportsUserData(); |
20 virtual ~SupportsUserData(); | 21 virtual ~SupportsUserData(); |
21 | 22 |
(...skipping 13 matching lines...) Expand all Loading... |
35 | 36 |
36 private: | 37 private: |
37 typedef std::map<const void*, linked_ptr<Data> > DataMap; | 38 typedef std::map<const void*, linked_ptr<Data> > DataMap; |
38 | 39 |
39 // Externally-defined data accessible by key | 40 // Externally-defined data accessible by key |
40 DataMap user_data_; | 41 DataMap user_data_; |
41 | 42 |
42 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); | 43 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); |
43 }; | 44 }; |
44 | 45 |
| 46 // Adapter class that releases a refcounted object when the |
| 47 // SupportsUserData::Data object is deleted. |
| 48 template <typename T> |
| 49 class UserDataAdapter : public base::SupportsUserData::Data { |
| 50 public: |
| 51 static T* Get(SupportsUserData* supports_user_data, const char* key) { |
| 52 UserDataAdapter* data = |
| 53 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); |
| 54 return static_cast<T*>(data->object_.get()); |
| 55 } |
| 56 |
| 57 UserDataAdapter(T* object) : object_(object) {} |
| 58 |
| 59 private: |
| 60 scoped_refptr<T> object_; |
| 61 |
| 62 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); |
| 63 }; |
| 64 |
45 } // namespace base | 65 } // namespace base |
46 | 66 |
47 #endif // BASE_SUPPORTS_USER_DATA_H_ | 67 #endif // BASE_SUPPORTS_USER_DATA_H_ |
OLD | NEW |