blob: 6d515d7341ebc60c641dae9a145bdd9b389e3bbf [file] [log] [blame]
jam@chromium.org533df722012-02-16 07:07:34 +09001// 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
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
10#include "base/base_export.h"
11#include "base/memory/linked_ptr.h"
jam@chromium.org5c8ca512012-02-21 12:57:42 +090012#include "base/memory/ref_counted.h"
joth@chromium.orgdb2811b2012-09-11 04:18:53 +090013#include "base/threading/thread_checker.h"
jam@chromium.org533df722012-02-16 07:07:34 +090014
15namespace 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.
19class BASE_EXPORT SupportsUserData {
20 public:
21 SupportsUserData();
jam@chromium.org533df722012-02-16 07:07:34 +090022
23 // Derive from this class and add your own data members to associate extra
avi@chromium.org09840d32012-08-21 07:58:21 +090024 // information with this object. Alternatively, add this as a public base
25 // class to any class with a virtual destructor.
jam@chromium.org533df722012-02-16 07:07:34 +090026 class BASE_EXPORT Data {
27 public:
28 virtual ~Data() {}
29 };
30
31 // The user data allows the clients to associate data with this object.
32 // Multiple user data values can be stored under different keys.
33 // This object will TAKE OWNERSHIP of the given data pointer, and will
34 // delete the object if it is changed or the object is destroyed.
35 Data* GetUserData(const void* key) const;
36 void SetUserData(const void* key, Data* data);
avi@chromium.orgb2d507b2012-08-12 11:25:13 +090037 void RemoveUserData(const void* key);
jam@chromium.org533df722012-02-16 07:07:34 +090038
joth@chromium.orgdb2811b2012-09-11 04:18:53 +090039 // 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
rsleevi@chromium.orgd55e9a42012-06-26 15:23:00 +090045 protected:
46 virtual ~SupportsUserData();
47
jam@chromium.org533df722012-02-16 07:07:34 +090048 private:
49 typedef std::map<const void*, linked_ptr<Data> > DataMap;
50
avi@chromium.org09840d32012-08-21 07:58:21 +090051 // Externally-defined data accessible by key.
jam@chromium.org533df722012-02-16 07:07:34 +090052 DataMap user_data_;
joth@chromium.orgdb2811b2012-09-11 04:18:53 +090053 // Guards usage of |user_data_|
54 ThreadChecker thread_checker_;
jam@chromium.org533df722012-02-16 07:07:34 +090055
56 DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
57};
58
jam@chromium.org5c8ca512012-02-21 12:57:42 +090059// Adapter class that releases a refcounted object when the
60// SupportsUserData::Data object is deleted.
61template <typename T>
62class UserDataAdapter : public base::SupportsUserData::Data {
63 public:
grunell@chromium.org8781fe42013-10-04 03:44:06 +090064 static T* Get(SupportsUserData* supports_user_data, const void* key) {
avi@chromium.org9592b542012-10-09 06:08:54 +090065 UserDataAdapter* data =
jam@chromium.org5c8ca512012-02-21 12:57:42 +090066 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
avi@chromium.org9592b542012-10-09 06:08:54 +090067 return data ? static_cast<T*>(data->object_.get()) : NULL;
jam@chromium.org5c8ca512012-02-21 12:57:42 +090068 }
69
70 UserDataAdapter(T* object) : object_(object) {}
jam@chromium.org14eaa3f2012-02-25 08:38:12 +090071 T* release() { return object_.release(); }
jam@chromium.org5c8ca512012-02-21 12:57:42 +090072
73 private:
74 scoped_refptr<T> object_;
75
76 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
77};
78
jam@chromium.org533df722012-02-16 07:07:34 +090079} // namespace base
80
81#endif // BASE_SUPPORTS_USER_DATA_H_