[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 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 | ||||
3 | // found in the LICENSE file. | ||||
4 | |||||
5 | #ifndef BASE_SUPPORTS_USER_DATA_H_ | ||||
6 | #define BASE_SUPPORTS_USER_DATA_H_ | ||||
7 | |||||
8 | #include <map> | ||||
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 9 | #include <memory> |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 10 | |
11 | #include "base/base_export.h" | ||||
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
gab | 7b1d677 | 2016-12-22 14:05:17 | [diff] [blame] | 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. | ||||
[email protected] | 7d2e5f762 | 2012-09-10 19:18:53 | [diff] [blame] | 18 | #include "base/threading/thread_checker.h" |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 19 | |
20 | namespace base { | ||||
21 | |||||
22 | // This is a helper for classes that want to allow users to stash random data by | ||||
23 | // key. At destruction all the objects will be destructed. | ||||
24 | class BASE_EXPORT SupportsUserData { | ||||
25 | public: | ||||
26 | SupportsUserData(); | ||||
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 27 | |
28 | // Derive from this class and add your own data members to associate extra | ||||
[email protected] | 5b9f0b5 | 2012-08-20 22:58:21 | [diff] [blame] | 29 | // information with this object. Alternatively, add this as a public base |
30 | // class to any class with a virtual destructor. | ||||
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 31 | class BASE_EXPORT Data { |
32 | public: | ||||
gab | 7b1d677 | 2016-12-22 14:05:17 | [diff] [blame] | 33 | virtual ~Data() = default; |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 34 | }; |
35 | |||||
36 | // The user data allows the clients to associate data with this object. | ||||
37 | // Multiple user data values can be stored under different keys. | ||||
38 | // This object will TAKE OWNERSHIP of the given data pointer, and will | ||||
39 | // delete the object if it is changed or the object is destroyed. | ||||
Randy Smith | 4cd4d36a | 2017-08-24 01:01:09 | [diff] [blame] | 40 | // |key| must not be null--that value is too vulnerable for collision. |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 41 | Data* GetUserData(const void* key) const; |
sdefresne | ae5e2d2 | 2017-02-13 11:49:26 | [diff] [blame] | 42 | void SetUserData(const void* key, std::unique_ptr<Data> data); |
[email protected] | 27ee16f | 2012-08-12 02:25:13 | [diff] [blame] | 43 | void RemoveUserData(const void* key); |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 44 | |
[email protected] | 7d2e5f762 | 2012-09-10 19:18:53 | [diff] [blame] | 45 | // SupportsUserData is not thread-safe, and on debug build will assert it is |
gab | 7b1d677 | 2016-12-22 14:05:17 | [diff] [blame] | 46 | // only used on one execution sequence. Calling this method allows the caller |
47 | // to hand the SupportsUserData instance across execution sequences. Use only | ||||
48 | // if you are taking full control of the synchronization of that hand over. | ||||
49 | void DetachFromSequence(); | ||||
[email protected] | 7d2e5f762 | 2012-09-10 19:18:53 | [diff] [blame] | 50 | |
[email protected] | cb93248 | 2012-06-26 06:23:00 | [diff] [blame] | 51 | protected: |
52 | virtual ~SupportsUserData(); | ||||
53 | |||||
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 54 | private: |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 55 | using DataMap = std::map<const void*, std::unique_ptr<Data>>; |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 56 | |
[email protected] | 5b9f0b5 | 2012-08-20 22:58:21 | [diff] [blame] | 57 | // Externally-defined data accessible by key. |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 58 | DataMap user_data_; |
[email protected] | 7d2e5f762 | 2012-09-10 19:18:53 | [diff] [blame] | 59 | // Guards usage of |user_data_| |
gab | 7b1d677 | 2016-12-22 14:05:17 | [diff] [blame] | 60 | SequenceChecker sequence_checker_; |
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 61 | |
62 | DISALLOW_COPY_AND_ASSIGN(SupportsUserData); | ||||
63 | }; | ||||
64 | |||||
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 65 | // Adapter class that releases a refcounted object when the |
66 | // SupportsUserData::Data object is deleted. | ||||
67 | template <typename T> | ||||
68 | class UserDataAdapter : public base::SupportsUserData::Data { | ||||
69 | public: | ||||
horo | d556043 | 2014-12-12 06:20:13 | [diff] [blame] | 70 | static T* Get(const SupportsUserData* supports_user_data, const void* key) { |
[email protected] | 5537099 | 2012-10-08 21:08:54 | [diff] [blame] | 71 | UserDataAdapter* data = |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 72 | static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); |
[email protected] | 5537099 | 2012-10-08 21:08:54 | [diff] [blame] | 73 | return data ? static_cast<T*>(data->object_.get()) : NULL; |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 74 | } |
75 | |||||
76 | UserDataAdapter(T* object) : object_(object) {} | ||||
[email protected] | c1fff07 | 2012-02-24 23:38:12 | [diff] [blame] | 77 | T* release() { return object_.release(); } |
[email protected] | 314c3e2 | 2012-02-21 03:57:42 | [diff] [blame] | 78 | |
79 | private: | ||||
80 | scoped_refptr<T> object_; | ||||
81 | |||||
82 | DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); | ||||
83 | }; | ||||
84 | |||||
[email protected] | ea8e181 | 2012-02-15 22:07:34 | [diff] [blame] | 85 | } // namespace base |
86 | |||||
87 | #endif // BASE_SUPPORTS_USER_DATA_H_ |