Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: base/supports_user_data.h

Issue 10824342: Attached data cleanup. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_service.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 21
22 // 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
23 // information with this object. Use GetUserData(key) and SetUserData() 23 // information with this object. Alternatively, add this as a public base
24 // class to any class with a virtual destructor.
24 class BASE_EXPORT Data { 25 class BASE_EXPORT Data {
25 public: 26 public:
26 virtual ~Data() {} 27 virtual ~Data() {}
27 }; 28 };
28 29
29 // The user data allows the clients to associate data with this object. 30 // The user data allows the clients to associate data with this object.
30 // Multiple user data values can be stored under different keys. 31 // Multiple user data values can be stored under different keys.
31 // This object will TAKE OWNERSHIP of the given data pointer, and will 32 // This object will TAKE OWNERSHIP of the given data pointer, and will
32 // delete the object if it is changed or the object is destroyed. 33 // delete the object if it is changed or the object is destroyed.
33 Data* GetUserData(const void* key) const; 34 Data* GetUserData(const void* key) const;
34 void SetUserData(const void* key, Data* data); 35 void SetUserData(const void* key, Data* data);
35 void RemoveUserData(const void* key); 36 void RemoveUserData(const void* key);
36 37
37 protected: 38 protected:
38 virtual ~SupportsUserData(); 39 virtual ~SupportsUserData();
39 40
40 private: 41 private:
41 typedef std::map<const void*, linked_ptr<Data> > DataMap; 42 typedef std::map<const void*, linked_ptr<Data> > DataMap;
42 43
43 // Externally-defined data accessible by key 44 // Externally-defined data accessible by key.
44 DataMap user_data_; 45 DataMap user_data_;
45 46
46 DISALLOW_COPY_AND_ASSIGN(SupportsUserData); 47 DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
47 }; 48 };
48 49
49 // Adapter class that releases a refcounted object when the 50 // Adapter class that releases a refcounted object when the
50 // SupportsUserData::Data object is deleted. 51 // SupportsUserData::Data object is deleted.
51 template <typename T> 52 template <typename T>
52 class UserDataAdapter : public base::SupportsUserData::Data { 53 class UserDataAdapter : public base::SupportsUserData::Data {
53 public: 54 public:
54 static T* Get(SupportsUserData* supports_user_data, const char* key) { 55 static T* Get(SupportsUserData* supports_user_data, const char* key) {
55 UserDataAdapter* data = 56 UserDataAdapter* data =
56 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key)); 57 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
57 return static_cast<T*>(data->object_.get()); 58 return static_cast<T*>(data->object_.get());
58 } 59 }
59 60
60 UserDataAdapter(T* object) : object_(object) {} 61 UserDataAdapter(T* object) : object_(object) {}
61 T* release() { return object_.release(); } 62 T* release() { return object_.release(); }
62 63
63 private: 64 private:
64 scoped_refptr<T> object_; 65 scoped_refptr<T> object_;
65 66
66 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter); 67 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
67 }; 68 };
68 69
69 } // namespace base 70 } // namespace base
70 71
71 #endif // BASE_SUPPORTS_USER_DATA_H_ 72 #endif // BASE_SUPPORTS_USER_DATA_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698