• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 namespace base {
16 
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 {
20  public:
21   SupportsUserData();
22   SupportsUserData(SupportsUserData&&);
23   SupportsUserData& operator=(SupportsUserData&&);
24   SupportsUserData(const SupportsUserData&) = delete;
25   SupportsUserData& operator=(const SupportsUserData&) = delete;
26 
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 {
31    public:
32     virtual ~Data() = default;
33 
34     // Returns a copy of |this|; null if copy is not supported.
35     virtual std::unique_ptr<Data> Clone();
36   };
37 
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
41   // RemoveUserData().
42   Data* GetUserData(const void* key) const;
43   [[nodiscard]] std::unique_ptr<Data> TakeUserData(const void* key);
44   void SetUserData(const void* key, std::unique_ptr<Data> data);
45   void RemoveUserData(const void* key);
46 
47   // Adds all data from |other|, that is clonable, to |this|. That is, this
48   // iterates over the data in |other|, and any data that returns non-null from
49   // Clone() is added to |this|.
50   void CloneDataFrom(const SupportsUserData& other);
51 
52   // SupportsUserData is not thread-safe, and on debug build will assert it is
53   // only used on one execution sequence. Calling this method allows the caller
54   // to hand the SupportsUserData instance across execution sequences. Use only
55   // if you are taking full control of the synchronization of that hand over.
56   void DetachFromSequence();
57 
58  protected:
59   virtual ~SupportsUserData();
60 
61   // Clear all user data from this object. This can be used if the subclass
62   // needs to provide reset functionality.
63   void ClearAllUserData();
64 
65  private:
66   struct Impl;
67   std::unique_ptr<Impl> impl_;
68   bool in_clear_ = false;
69   // Guards usage of |impl_|
70   SEQUENCE_CHECKER(sequence_checker_);
71 };
72 
73 // Adapter class that releases a refcounted object when the
74 // SupportsUserData::Data object is deleted.
75 template <typename T>
76 class UserDataAdapter : public SupportsUserData::Data {
77  public:
Get(const SupportsUserData * supports_user_data,const void * key)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;
82   }
83 
UserDataAdapter(T * object)84   explicit UserDataAdapter(T* object) : object_(object) {}
85   UserDataAdapter(const UserDataAdapter&) = delete;
86   UserDataAdapter& operator=(const UserDataAdapter&) = delete;
87   ~UserDataAdapter() override = default;
88 
release()89   T* release() { return object_.release(); }
90 
91  private:
92   scoped_refptr<T> const object_;
93 };
94 
95 }  // namespace base
96 
97 #endif  // BASE_SUPPORTS_USER_DATA_H_
98