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"
16 // TODO(gab): Removing this include causes IWYU failures in other headers,
17 // remove it in a follow- up CL.
18 #include "base/threading/thread_checker.h"
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 {
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 {
33 virtual ~Data() = default;
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.
40 // |key| must not be null--that value is too vulnerable for collision.
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 // SupportsUserData is not thread-safe, and on debug build will assert it is
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();
52 virtual ~SupportsUserData();
55 using DataMap = std::map<const void*, std::unique_ptr<Data>>;
57 // Externally-defined data accessible by key.
59 // Guards usage of |user_data_|
60 SequenceChecker sequence_checker_;
62 DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
65 // Adapter class that releases a refcounted object when the
66 // SupportsUserData::Data object is deleted.
68 class UserDataAdapter : public base::SupportsUserData::Data {
70 static T* Get(const SupportsUserData* supports_user_data, const void* key) {
71 UserDataAdapter* data =
72 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
73 return data ? static_cast<T*>(data->object_.get()) : NULL;
76 UserDataAdapter(T* object) : object_(object) {}
77 T* release() { return object_.release(); }
80 scoped_refptr<T> object_;
82 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
87 #endif // BASE_SUPPORTS_USER_DATA_H_