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 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/sequence_checker.h" | |
15 | |
16 // TODO(gab): Removing this include causes IWYU failures in other headers, | |
17 // remove it in a follow- up CL. | |
dcheng
2016/12/22 04:57:55
sob
| |
14 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
15 | 19 |
16 namespace base { | 20 namespace base { |
17 | 21 |
18 // This is a helper for classes that want to allow users to stash random data by | 22 // This is a helper for classes that want to allow users to stash random data by |
19 // key. At destruction all the objects will be destructed. | 23 // key. At destruction all the objects will be destructed. |
20 class BASE_EXPORT SupportsUserData { | 24 class BASE_EXPORT SupportsUserData { |
21 public: | 25 public: |
22 SupportsUserData(); | 26 SupportsUserData(); |
23 | 27 |
24 // Derive from this class and add your own data members to associate extra | 28 // Derive from this class and add your own data members to associate extra |
25 // information with this object. Alternatively, add this as a public base | 29 // information with this object. Alternatively, add this as a public base |
26 // class to any class with a virtual destructor. | 30 // class to any class with a virtual destructor. |
27 class BASE_EXPORT Data { | 31 class BASE_EXPORT Data { |
28 public: | 32 public: |
29 virtual ~Data() {} | 33 virtual ~Data() = default; |
30 }; | 34 }; |
31 | 35 |
32 // The user data allows the clients to associate data with this object. | 36 // The user data allows the clients to associate data with this object. |
33 // Multiple user data values can be stored under different keys. | 37 // Multiple user data values can be stored under different keys. |
34 // This object will TAKE OWNERSHIP of the given data pointer, and will | 38 // This object will TAKE OWNERSHIP of the given data pointer, and will |
35 // delete the object if it is changed or the object is destroyed. | 39 // delete the object if it is changed or the object is destroyed. |
36 Data* GetUserData(const void* key) const; | 40 Data* GetUserData(const void* key) const; |
37 void SetUserData(const void* key, Data* data); | 41 void SetUserData(const void* key, Data* data); |
38 void RemoveUserData(const void* key); | 42 void RemoveUserData(const void* key); |
39 | 43 |
40 // SupportsUserData is not thread-safe, and on debug build will assert it is | 44 // SupportsUserData is not thread-safe, and on debug build will assert it is |
41 // only used on one thread. Calling this method allows the caller to hand | 45 // only used on one execution sequence. Calling this method allows the caller |
42 // the SupportsUserData instance across threads. Use only if you are taking | 46 // to hand the SupportsUserData instance across execution sequences. Use only |
43 // full control of the synchronization of that hand over. | 47 // if you are taking full control of the synchronization of that hand over. |
44 void DetachUserDataThread(); | 48 void DetachFromSequence(); |
45 | 49 |
46 protected: | 50 protected: |
47 virtual ~SupportsUserData(); | 51 virtual ~SupportsUserData(); |
48 | 52 |
49 private: | 53 private: |
50 using DataMap = std::map<const void*, std::unique_ptr<Data>>; | 54 using DataMap = std::map<const void*, std::unique_ptr<Data>>; |
51 | 55 |
52 // Externally-defined data accessible by key. | 56 // Externally-defined data accessible by key. |
53 DataMap user_data_; | 57 DataMap user_data_; |
54 // Guards usage of |user_data_| | 58 // Guards usage of |user_data_| |
55 ThreadChecker thread_checker_; | 59 SequenceChecker sequence_checker_; |
56 | 60 |
57 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); | 61 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); |
58 }; | 62 }; |
59 | 63 |
60 // Adapter class that releases a refcounted object when the | 64 // Adapter class that releases a refcounted object when the |
61 // SupportsUserData::Data object is deleted. | 65 // SupportsUserData::Data object is deleted. |
62 template <typename T> | 66 template <typename T> |
63 class UserDataAdapter : public base::SupportsUserData::Data { | 67 class UserDataAdapter : public base::SupportsUserData::Data { |
64 public: | 68 public: |
65 static T* Get(const SupportsUserData* supports_user_data, const void* key) { | 69 static T* Get(const SupportsUserData* supports_user_data, const void* key) { |
66 UserDataAdapter* data = | 70 UserDataAdapter* data = |
67 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); | 71 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); |
68 return data ? static_cast<T*>(data->object_.get()) : NULL; | 72 return data ? static_cast<T*>(data->object_.get()) : NULL; |
69 } | 73 } |
70 | 74 |
71 UserDataAdapter(T* object) : object_(object) {} | 75 UserDataAdapter(T* object) : object_(object) {} |
72 T* release() { return object_.release(); } | 76 T* release() { return object_.release(); } |
73 | 77 |
74 private: | 78 private: |
75 scoped_refptr<T> object_; | 79 scoped_refptr<T> object_; |
76 | 80 |
77 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); | 81 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); |
78 }; | 82 }; |
79 | 83 |
80 } // namespace base | 84 } // namespace base |
81 | 85 |
82 #endif // BASE_SUPPORTS_USER_DATA_H_ | 86 #endif // BASE_SUPPORTS_USER_DATA_H_ |
OLD | NEW |