[M108 Migration][VD] Support set time and time zone offset
[platform/framework/web/chromium-efl.git] / base / supports_user_data.h
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   void SetUserData(const void* key, std::unique_ptr<Data> data);
44   void RemoveUserData(const void* key);
45
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);
50
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();
56
57  protected:
58   virtual ~SupportsUserData();
59
60   // Clear all user data from this object. This can be used if the subclass
61   // needs to provide reset functionality.
62   void ClearAllUserData();
63
64  private:
65   using DataMap = std::map<const void*, std::unique_ptr<Data>>;
66
67   // Externally-defined data accessible by key.
68   DataMap user_data_;
69   // Guards usage of |user_data_|
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:
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
84   explicit UserDataAdapter(T* object) : object_(object) {}
85   UserDataAdapter(const UserDataAdapter&) = delete;
86   UserDataAdapter& operator=(const UserDataAdapter&) = delete;
87   ~UserDataAdapter() override = default;
88
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_