blob: 77367553daf5b405d5fffa138614d6a64b0234f0 [file] [log] [blame]
[email protected]ea8e1812012-02-15 22:07:341// 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"
[email protected]314c3e22012-02-21 03:57:4212#include "base/memory/ref_counted.h"
[email protected]7d2e5f7622012-09-10 19:18:5313#include "base/threading/thread_checker.h"
[email protected]ea8e1812012-02-15 22:07:3414
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();
[email protected]ea8e1812012-02-15 22:07:3422
23 // Derive from this class and add your own data members to associate extra
[email protected]5b9f0b52012-08-20 22:58:2124 // information with this object. Alternatively, add this as a public base
25 // class to any class with a virtual destructor.
[email protected]ea8e1812012-02-15 22:07:3426 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);
[email protected]27ee16f2012-08-12 02:25:1337 void RemoveUserData(const void* key);
[email protected]ea8e1812012-02-15 22:07:3438
[email protected]7d2e5f7622012-09-10 19:18:5339 // 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
[email protected]cb932482012-06-26 06:23:0045 protected:
46 virtual ~SupportsUserData();
47
[email protected]ea8e1812012-02-15 22:07:3448 private:
49 typedef std::map<const void*, linked_ptr<Data> > DataMap;
50
[email protected]5b9f0b52012-08-20 22:58:2151 // Externally-defined data accessible by key.
[email protected]ea8e1812012-02-15 22:07:3452 DataMap user_data_;
[email protected]7d2e5f7622012-09-10 19:18:5353 // Guards usage of |user_data_|
54 ThreadChecker thread_checker_;
[email protected]ea8e1812012-02-15 22:07:3455
56 DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
57};
58
[email protected]314c3e22012-02-21 03:57:4259// 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:
64 static T* Get(SupportsUserData* supports_user_data, const char* key) {
[email protected]55370992012-10-08 21:08:5465 UserDataAdapter* data =
[email protected]314c3e22012-02-21 03:57:4266 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
[email protected]55370992012-10-08 21:08:5467 return data ? static_cast<T*>(data->object_.get()) : NULL;
[email protected]314c3e22012-02-21 03:57:4268 }
69
70 UserDataAdapter(T* object) : object_(object) {}
[email protected]c1fff072012-02-24 23:38:1271 T* release() { return object_.release(); }
[email protected]314c3e22012-02-21 03:57:4272
73 private:
74 scoped_refptr<T> object_;
75
76 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
77};
78
[email protected]ea8e1812012-02-15 22:07:3479} // namespace base
80
81#endif // BASE_SUPPORTS_USER_DATA_H_