| 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 | 13 |
| 14 namespace base { | 14 namespace base { |
| 15 | 15 |
| 16 // This is a helper for classes that want to allow users to stash random data by | 16 // 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. | 17 // key. At destruction all the objects will be destructed. |
| 18 class BASE_EXPORT SupportsUserData { | 18 class BASE_EXPORT SupportsUserData { |
| 19 public: | 19 public: |
| 20 SupportsUserData(); | 20 SupportsUserData(); |
| 21 virtual ~SupportsUserData(); | |
| 22 | 21 |
| 23 // Derive from this class and add your own data members to associate extra | 22 // Derive from this class and add your own data members to associate extra |
| 24 // information with this object. Use GetUserData(key) and SetUserData() | 23 // information with this object. Use GetUserData(key) and SetUserData() |
| 25 class BASE_EXPORT Data { | 24 class BASE_EXPORT Data { |
| 26 public: | 25 public: |
| 27 virtual ~Data() {} | 26 virtual ~Data() {} |
| 28 }; | 27 }; |
| 29 | 28 |
| 30 // The user data allows the clients to associate data with this object. | 29 // The user data allows the clients to associate data with this object. |
| 31 // Multiple user data values can be stored under different keys. | 30 // Multiple user data values can be stored under different keys. |
| 32 // This object will TAKE OWNERSHIP of the given data pointer, and will | 31 // 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. | 32 // delete the object if it is changed or the object is destroyed. |
| 34 Data* GetUserData(const void* key) const; | 33 Data* GetUserData(const void* key) const; |
| 35 void SetUserData(const void* key, Data* data); | 34 void SetUserData(const void* key, Data* data); |
| 36 | 35 |
| 36 protected: |
| 37 virtual ~SupportsUserData(); |
| 38 |
| 37 private: | 39 private: |
| 38 typedef std::map<const void*, linked_ptr<Data> > DataMap; | 40 typedef std::map<const void*, linked_ptr<Data> > DataMap; |
| 39 | 41 |
| 40 // Externally-defined data accessible by key | 42 // Externally-defined data accessible by key |
| 41 DataMap user_data_; | 43 DataMap user_data_; |
| 42 | 44 |
| 43 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); | 45 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); |
| 44 }; | 46 }; |
| 45 | 47 |
| 46 // Adapter class that releases a refcounted object when the | 48 // Adapter class that releases a refcounted object when the |
| (...skipping 12 matching lines...) Expand all Loading... |
| 59 | 61 |
| 60 private: | 62 private: |
| 61 scoped_refptr<T> object_; | 63 scoped_refptr<T> object_; |
| 62 | 64 |
| 63 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); | 65 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); |
| 64 }; | 66 }; |
| 65 | 67 |
| 66 } // namespace base | 68 } // namespace base |
| 67 | 69 |
| 68 #endif // BASE_SUPPORTS_USER_DATA_H_ | 70 #endif // BASE_SUPPORTS_USER_DATA_H_ |
| OLD | NEW |