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. 4 5 #ifndef BASE_SUPPORTS_USER_DATA_H_ 6 #define BASE_SUPPORTS_USER_DATA_H_ 7 8 #include <map> 9 #include <memory> 10 11 #include "base/base_export.h" 12 #include "base/memory/scoped_refptr.h" 13 #include "base/sequence_checker.h" 14 #include "third_party/abseil-cpp/absl/container/flat_hash_map.h" 15 16 namespace base { 17 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 { 21 public: 22 SupportsUserData(); 23 SupportsUserData(SupportsUserData&&); 24 SupportsUserData& operator=(SupportsUserData&&); 25 SupportsUserData(const SupportsUserData&) = delete; 26 SupportsUserData& operator=(const SupportsUserData&) = delete; 27 28 // Derive from this class and add your own data members to associate extra 29 // information with this object. Alternatively, add this as a public base 30 // class to any class with a virtual destructor. 31 class BASE_EXPORT Data { 32 public: 33 virtual ~Data() = default; 34 35 // Returns a copy of |this|; null if copy is not supported. 36 virtual std::unique_ptr<Data> Clone(); 37 }; 38 39 // The user data allows the clients to associate data with this object. 40 // |key| must not be null--that value is too vulnerable for collision. 41 // NOTE: SetUserData() with an empty unique_ptr behaves the same as 42 // RemoveUserData(). 43 Data* GetUserData(const void* key) const; 44 [[nodiscard]] std::unique_ptr<Data> TakeUserData(const void* key); 45 void SetUserData(const void* key, std::unique_ptr<Data> data); 46 void RemoveUserData(const void* key); 47 48 // Adds all data from |other|, that is clonable, to |this|. That is, this 49 // iterates over the data in |other|, and any data that returns non-null from 50 // Clone() is added to |this|. 51 void CloneDataFrom(const SupportsUserData& other); 52 53 // SupportsUserData is not thread-safe, and on debug build will assert it is 54 // only used on one execution sequence. Calling this method allows the caller 55 // to hand the SupportsUserData instance across execution sequences. Use only 56 // if you are taking full control of the synchronization of that hand over. 57 void DetachFromSequence(); 58 59 protected: 60 virtual ~SupportsUserData(); 61 62 // Clear all user data from this object. This can be used if the subclass 63 // needs to provide reset functionality. 64 void ClearAllUserData(); 65 66 private: 67 // Externally-defined data accessible by key. 68 absl::flat_hash_map<const void*, std::unique_ptr<Data>> user_data_; 69 bool in_destructor_ = false; 70 // Guards usage of |user_data_| 71 SEQUENCE_CHECKER(sequence_checker_); 72 }; 73 74 // Adapter class that releases a refcounted object when the 75 // SupportsUserData::Data object is deleted. 76 template <typename T> 77 class UserDataAdapter : public SupportsUserData::Data { 78 public: Get(const SupportsUserData * supports_user_data,const void * key)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; 83 } 84 UserDataAdapter(T * object)85 explicit UserDataAdapter(T* object) : object_(object) {} 86 UserDataAdapter(const UserDataAdapter&) = delete; 87 UserDataAdapter& operator=(const UserDataAdapter&) = delete; 88 ~UserDataAdapter() override = default; 89 release()90 T* release() { return object_.release(); } 91 92 private: 93 scoped_refptr<T> const object_; 94 }; 95 96 } // namespace base 97 98 #endif // BASE_SUPPORTS_USER_DATA_H_ 99