| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_SUPPORTS_USER_DATA_H_ | 5 #ifndef BASE_SUPPORTS_USER_DATA_H_ |
| 6 #define BASE_SUPPORTS_USER_DATA_H_ | 6 #define BASE_SUPPORTS_USER_DATA_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/base_export.h" | 10 #include "base/base_export.h" |
| 11 #include "base/memory/linked_ptr.h" | 11 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/threading/thread_checker.h" |
| 13 | 14 |
| 14 namespace base { | 15 namespace base { |
| 15 | 16 |
| 16 // This is a helper for classes that want to allow users to stash random data by | 17 // This is a helper for classes that want to allow users to stash random data by |
| 17 // key. At destruction all the objects will be destructed. | 18 // key. At destruction all the objects will be destructed. |
| 18 class BASE_EXPORT SupportsUserData { | 19 class BASE_EXPORT SupportsUserData { |
| 19 public: | 20 public: |
| 20 SupportsUserData(); | 21 SupportsUserData(); |
| 21 | 22 |
| 22 // Derive from this class and add your own data members to associate extra | 23 // Derive from this class and add your own data members to associate extra |
| 23 // information with this object. Alternatively, add this as a public base | 24 // information with this object. Alternatively, add this as a public base |
| 24 // class to any class with a virtual destructor. | 25 // class to any class with a virtual destructor. |
| 25 class BASE_EXPORT Data { | 26 class BASE_EXPORT Data { |
| 26 public: | 27 public: |
| 27 virtual ~Data() {} | 28 virtual ~Data() {} |
| 28 }; | 29 }; |
| 29 | 30 |
| 30 // The user data allows the clients to associate data with this object. | 31 // The user data allows the clients to associate data with this object. |
| 31 // Multiple user data values can be stored under different keys. | 32 // Multiple user data values can be stored under different keys. |
| 32 // This object will TAKE OWNERSHIP of the given data pointer, and will | 33 // This object will TAKE OWNERSHIP of the given data pointer, and will |
| 33 // delete the object if it is changed or the object is destroyed. | 34 // delete the object if it is changed or the object is destroyed. |
| 34 Data* GetUserData(const void* key) const; | 35 Data* GetUserData(const void* key) const; |
| 35 void SetUserData(const void* key, Data* data); | 36 void SetUserData(const void* key, Data* data); |
| 36 void RemoveUserData(const void* key); | 37 void RemoveUserData(const void* key); |
| 37 | 38 |
| 39 // SupportsUserData is not thread-safe, and on debug build will assert it is |
| 40 // only used on one thread. Calling this method allows the caller to hand |
| 41 // the SupportsUserData instance across threads. Use only if you are taking |
| 42 // full control of the synchronization of that hand over. |
| 43 void DetachUserDataThread(); |
| 44 |
| 38 protected: | 45 protected: |
| 39 virtual ~SupportsUserData(); | 46 virtual ~SupportsUserData(); |
| 40 | 47 |
| 41 private: | 48 private: |
| 42 typedef std::map<const void*, linked_ptr<Data> > DataMap; | 49 typedef std::map<const void*, linked_ptr<Data> > DataMap; |
| 43 | 50 |
| 44 // Externally-defined data accessible by key. | 51 // Externally-defined data accessible by key. |
| 45 DataMap user_data_; | 52 DataMap user_data_; |
| 53 // Guards usage of |user_data_| |
| 54 ThreadChecker thread_checker_; |
| 46 | 55 |
| 47 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); | 56 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); |
| 48 }; | 57 }; |
| 49 | 58 |
| 50 // Adapter class that releases a refcounted object when the | 59 // Adapter class that releases a refcounted object when the |
| 51 // SupportsUserData::Data object is deleted. | 60 // SupportsUserData::Data object is deleted. |
| 52 template <typename T> | 61 template <typename T> |
| 53 class UserDataAdapter : public base::SupportsUserData::Data { | 62 class UserDataAdapter : public base::SupportsUserData::Data { |
| 54 public: | 63 public: |
| 55 static T* Get(SupportsUserData* supports_user_data, const char* key) { | 64 static T* Get(SupportsUserData* supports_user_data, const char* key) { |
| 56 UserDataAdapter* data = | 65 UserDataAdapter* data = |
| 57 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); | 66 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); |
| 58 return static_cast<T*>(data->object_.get()); | 67 return static_cast<T*>(data->object_.get()); |
| 59 } | 68 } |
| 60 | 69 |
| 61 UserDataAdapter(T* object) : object_(object) {} | 70 UserDataAdapter(T* object) : object_(object) {} |
| 62 T* release() { return object_.release(); } | 71 T* release() { return object_.release(); } |
| 63 | 72 |
| 64 private: | 73 private: |
| 65 scoped_refptr<T> object_; | 74 scoped_refptr<T> object_; |
| 66 | 75 |
| 67 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); | 76 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); |
| 68 }; | 77 }; |
| 69 | 78 |
| 70 } // namespace base | 79 } // namespace base |
| 71 | 80 |
| 72 #endif // BASE_SUPPORTS_USER_DATA_H_ | 81 #endif // BASE_SUPPORTS_USER_DATA_H_ |
| OLD | NEW |