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 #include "base/memory/ref_counted.h" |
13 #include "base/threading/thread_checker.h" | 13 #include "base/threading/thread_checker.h" |
14 | 14 |
15 namespace base { | 15 namespace base { |
16 | 16 |
17 // This is a helper for classes that want to allow users to stash random data by | 17 // This is a helper for classes that want to allow users to stash random data by |
18 // key. At destruction all the objects will be destructed. | 18 // key. At destruction all the objects will be destructed. |
19 class BASE_EXPORT SupportsUserData { | 19 class BASE_EXPORT SupportsUserData { |
20 public: | 20 public: |
21 SupportsUserData(); | 21 SupportsUserData(); |
| 22 virtual ~SupportsUserData(); |
22 | 23 |
23 // Derive from this class and add your own data members to associate extra | 24 // Derive from this class and add your own data members to associate extra |
24 // information with this object. Alternatively, add this as a public base | 25 // information with this object. Alternatively, add this as a public base |
25 // class to any class with a virtual destructor. | 26 // class to any class with a virtual destructor. |
26 class BASE_EXPORT Data { | 27 class BASE_EXPORT Data { |
27 public: | 28 public: |
28 virtual ~Data() {} | 29 virtual ~Data() {} |
29 }; | 30 }; |
30 | 31 |
31 // The user data allows the clients to associate data with this object. | 32 // The user data allows the clients to associate data with this object. |
32 // Multiple user data values can be stored under different keys. | 33 // Multiple user data values can be stored under different keys. |
33 // This object will TAKE OWNERSHIP of the given data pointer, and will | 34 // This object will TAKE OWNERSHIP of the given data pointer, and will |
34 // delete the object if it is changed or the object is destroyed. | 35 // delete the object if it is changed or the object is destroyed. |
35 Data* GetUserData(const void* key) const; | 36 Data* GetUserData(const void* key) const; |
36 void SetUserData(const void* key, Data* data); | 37 void SetUserData(const void* key, Data* data); |
37 void RemoveUserData(const void* key); | 38 void RemoveUserData(const void* key); |
38 | 39 |
39 // SupportsUserData is not thread-safe, and on debug build will assert it is | 40 // SupportsUserData is not thread-safe, and on debug build will assert it is |
40 // only used on one thread. Calling this method allows the caller to hand | 41 // only used on one thread. Calling this method allows the caller to hand |
41 // the SupportsUserData instance across threads. Use only if you are taking | 42 // the SupportsUserData instance across threads. Use only if you are taking |
42 // full control of the synchronization of that hand over. | 43 // full control of the synchronization of that hand over. |
43 void DetachUserDataThread(); | 44 void DetachUserDataThread(); |
44 | 45 |
45 protected: | |
46 virtual ~SupportsUserData(); | |
47 | |
48 private: | 46 private: |
49 typedef std::map<const void*, linked_ptr<Data> > DataMap; | 47 typedef std::map<const void*, linked_ptr<Data> > DataMap; |
50 | 48 |
51 // Externally-defined data accessible by key. | 49 // Externally-defined data accessible by key. |
52 DataMap user_data_; | 50 DataMap user_data_; |
53 // Guards usage of |user_data_| | 51 // Guards usage of |user_data_| |
54 ThreadChecker thread_checker_; | 52 ThreadChecker thread_checker_; |
55 | 53 |
56 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); | 54 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); |
57 }; | 55 }; |
(...skipping 14 matching lines...) Expand all Loading... |
72 | 70 |
73 private: | 71 private: |
74 scoped_refptr<T> object_; | 72 scoped_refptr<T> object_; |
75 | 73 |
76 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); | 74 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); |
77 }; | 75 }; |
78 | 76 |
79 } // namespace base | 77 } // namespace base |
80 | 78 |
81 #endif // BASE_SUPPORTS_USER_DATA_H_ | 79 #endif // BASE_SUPPORTS_USER_DATA_H_ |
OLD | NEW |