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.
5 #ifndef BASE_SUPPORTS_USER_DATA_H_
6 #define BASE_SUPPORTS_USER_DATA_H_
11 #include "base/base_export.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/sequence_checker.h"
18 // 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.
20 class BASE_EXPORT SupportsUserData {
23 SupportsUserData(SupportsUserData&&);
24 SupportsUserData& operator=(SupportsUserData&&);
26 // Derive from this class and add your own data members to associate extra
27 // information with this object. Alternatively, add this as a public base
28 // class to any class with a virtual destructor.
29 class BASE_EXPORT Data {
31 virtual ~Data() = default;
33 // Returns a copy of |this|; null if copy is not supported.
34 virtual std::unique_ptr<Data> Clone();
37 // The user data allows the clients to associate data with this object.
38 // |key| must not be null--that value is too vulnerable for collision.
39 // NOTE: SetUserData() with an empty unique_ptr behaves the same as
41 Data* GetUserData(const void* key) const;
42 void SetUserData(const void* key, std::unique_ptr<Data> data);
43 void RemoveUserData(const void* key);
45 // Adds all data from |other|, that is clonable, to |this|. That is, this
46 // iterates over the data in |other|, and any data that returns non-null from
47 // Clone() is added to |this|.
48 void CloneDataFrom(const SupportsUserData& other);
50 // SupportsUserData is not thread-safe, and on debug build will assert it is
51 // only used on one execution sequence. Calling this method allows the caller
52 // to hand the SupportsUserData instance across execution sequences. Use only
53 // if you are taking full control of the synchronization of that hand over.
54 void DetachFromSequence();
57 virtual ~SupportsUserData();
59 // Clear all user data from this object. This can be used if the subclass
60 // needs to provide reset functionality.
61 void ClearAllUserData();
64 using DataMap = std::map<const void*, std::unique_ptr<Data>>;
66 // Externally-defined data accessible by key.
68 // Guards usage of |user_data_|
69 SequenceChecker sequence_checker_;
71 DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
74 // Adapter class that releases a refcounted object when the
75 // SupportsUserData::Data object is deleted.
77 class UserDataAdapter : public SupportsUserData::Data {
79 static T* Get(const SupportsUserData* supports_user_data, const void* key) {
80 UserDataAdapter* data =
81 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
82 return data ? static_cast<T*>(data->object_.get()) : nullptr;
85 explicit UserDataAdapter(T* object) : object_(object) {}
86 ~UserDataAdapter() override = default;
88 T* release() { return object_.release(); }
91 scoped_refptr<T> const object_;
93 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
98 #endif // BASE_SUPPORTS_USER_DATA_H_