blob: 356c97329b8d1da6ca10fa2b5628dd6a80194290 [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>
dcheng093de9b2016-04-04 21:25:519#include <memory>
[email protected]ea8e1812012-02-15 22:07:3410
11#include "base/base_export.h"
avi9b6f42932015-12-26 22:15:1412#include "base/macros.h"
[email protected]314c3e22012-02-21 03:57:4213#include "base/memory/ref_counted.h"
gab7b1d6772016-12-22 14:05:1714#include "base/sequence_checker.h"
15
16// TODO(gab): Removing this include causes IWYU failures in other headers,
17// remove it in a follow- up CL.
[email protected]7d2e5f7622012-09-10 19:18:5318#include "base/threading/thread_checker.h"
[email protected]ea8e1812012-02-15 22:07:3419
20namespace base {
21
22// This is a helper for classes that want to allow users to stash random data by
23// key. At destruction all the objects will be destructed.
24class BASE_EXPORT SupportsUserData {
25 public:
26 SupportsUserData();
[email protected]ea8e1812012-02-15 22:07:3427
28 // Derive from this class and add your own data members to associate extra
[email protected]5b9f0b52012-08-20 22:58:2129 // information with this object. Alternatively, add this as a public base
30 // class to any class with a virtual destructor.
[email protected]ea8e1812012-02-15 22:07:3431 class BASE_EXPORT Data {
32 public:
gab7b1d6772016-12-22 14:05:1733 virtual ~Data() = default;
[email protected]ea8e1812012-02-15 22:07:3434 };
35
36 // The user data allows the clients to associate data with this object.
37 // Multiple user data values can be stored under different keys.
38 // This object will TAKE OWNERSHIP of the given data pointer, and will
39 // delete the object if it is changed or the object is destroyed.
Randy Smith4cd4d36a2017-08-24 01:01:0940 // |key| must not be null--that value is too vulnerable for collision.
[email protected]ea8e1812012-02-15 22:07:3441 Data* GetUserData(const void* key) const;
sdefresneae5e2d22017-02-13 11:49:2642 void SetUserData(const void* key, std::unique_ptr<Data> data);
[email protected]27ee16f2012-08-12 02:25:1343 void RemoveUserData(const void* key);
[email protected]ea8e1812012-02-15 22:07:3444
[email protected]7d2e5f7622012-09-10 19:18:5345 // SupportsUserData is not thread-safe, and on debug build will assert it is
gab7b1d6772016-12-22 14:05:1746 // only used on one execution sequence. Calling this method allows the caller
47 // to hand the SupportsUserData instance across execution sequences. Use only
48 // if you are taking full control of the synchronization of that hand over.
49 void DetachFromSequence();
[email protected]7d2e5f7622012-09-10 19:18:5350
[email protected]cb932482012-06-26 06:23:0051 protected:
52 virtual ~SupportsUserData();
53
[email protected]ea8e1812012-02-15 22:07:3454 private:
dcheng093de9b2016-04-04 21:25:5155 using DataMap = std::map<const void*, std::unique_ptr<Data>>;
[email protected]ea8e1812012-02-15 22:07:3456
[email protected]5b9f0b52012-08-20 22:58:2157 // Externally-defined data accessible by key.
[email protected]ea8e1812012-02-15 22:07:3458 DataMap user_data_;
[email protected]7d2e5f7622012-09-10 19:18:5359 // Guards usage of |user_data_|
gab7b1d6772016-12-22 14:05:1760 SequenceChecker sequence_checker_;
[email protected]ea8e1812012-02-15 22:07:3461
62 DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
63};
64
[email protected]314c3e22012-02-21 03:57:4265// Adapter class that releases a refcounted object when the
66// SupportsUserData::Data object is deleted.
67template <typename T>
68class UserDataAdapter : public base::SupportsUserData::Data {
69 public:
horod5560432014-12-12 06:20:1370 static T* Get(const SupportsUserData* supports_user_data, const void* key) {
[email protected]55370992012-10-08 21:08:5471 UserDataAdapter* data =
[email protected]314c3e22012-02-21 03:57:4272 static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
[email protected]55370992012-10-08 21:08:5473 return data ? static_cast<T*>(data->object_.get()) : NULL;
[email protected]314c3e22012-02-21 03:57:4274 }
75
76 UserDataAdapter(T* object) : object_(object) {}
[email protected]c1fff072012-02-24 23:38:1277 T* release() { return object_.release(); }
[email protected]314c3e22012-02-21 03:57:4278
79 private:
80 scoped_refptr<T> object_;
81
82 DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
83};
84
[email protected]ea8e1812012-02-15 22:07:3485} // namespace base
86
87#endif // BASE_SUPPORTS_USER_DATA_H_