1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_SUPPORTS_USER_DATA_H_
6 #define BASE_SUPPORTS_USER_DATA_H_
11 #include "base/base_export.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/sequence_checker.h"
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.
19 class BASE_EXPORT SupportsUserData {
22 SupportsUserData(SupportsUserData&&);
23 SupportsUserData& operator=(SupportsUserData&&);
24 SupportsUserData(const SupportsUserData&) = delete;
25 SupportsUserData& operator=(const SupportsUserData&) = delete;
27 // Derive from this class and add your own data members to associate extra
28 // information with this object. Alternatively, add this as a public base
29 // class to any class with a virtual destructor.
30 class BASE_EXPORT Data {
32 virtual ~Data() = default;
34 // Returns a copy of |this|; null if copy is not supported.
35 virtual std::unique_ptr<Data> Clone();
38 // The user data allows the clients to associate data with this object.
39 // |key| must not be null--that value is too vulnerable for collision.
40 // NOTE: SetUserData() with an empty unique_ptr behaves the same as
42 Data* GetUserData(const void* key) const;
43 void SetUserData(const void* key, std::unique_ptr<Data> data);
44 void RemoveUserData(const void* key);
46 // Adds all data from |other|, that is clonable, to |this|. That is, this
47 // iterates over the data in |other|, and any data that returns non-null from
48 // Clone() is added to |this|.
49 void CloneDataFrom(const SupportsUserData& other);
51 // SupportsUserData is not thread-safe, and on debug build will assert it is
52 // only used on one execution sequence. Calling this method allows the caller
53 // to hand the SupportsUserData instance across execution sequences. Use only
54 // if you are taking full control of the synchronization of that hand over.
55 void DetachFromSequence();
58 virtual ~SupportsUserData();
60 // Clear all user data from this object. This can be used if the subclass
61 // needs to provide reset functionality.
62 void ClearAllUserData();
65 using DataMap = std::map<const void*, std::unique_ptr<Data>>;
67 // Externally-defined data accessible by key.
69 // Guards usage of |user_data_|
70 SEQUENCE_CHECKER(sequence_checker_);
73 // Adapter class that releases a refcounted object when the
74 // SupportsUserData::Data object is deleted.
76 class UserDataAdapter : public SupportsUserData::Data {
78 static T* Get(const SupportsUserData* supports_user_data, const void* key) {
79 UserDataAdapter* data =
80 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
81 return data ? static_cast<T*>(data->object_.get()) : nullptr;
84 explicit UserDataAdapter(T* object) : object_(object) {}
85 UserDataAdapter(const UserDataAdapter&) = delete;
86 UserDataAdapter& operator=(const UserDataAdapter&) = delete;
87 ~UserDataAdapter() override = default;
89 T* release() { return object_.release(); }
92 scoped_refptr<T> const object_;
97 #endif // BASE_SUPPORTS_USER_DATA_H_