blob: 356c97329b8d1da6ca10fa2b5628dd6a80194290 [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <memory>
#include "base/base_export.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/sequence_checker.h"
// TODO(gab): Removing this include causes IWYU failures in other headers,
// remove it in a follow- up CL.
#include "base/threading/thread_checker.h"
namespace base {
// This is a helper for classes that want to allow users to stash random data by
// key. At destruction all the objects will be destructed.
class BASE_EXPORT SupportsUserData {
// Derive from this class and add your own data members to associate extra
// information with this object. Alternatively, add this as a public base
// class to any class with a virtual destructor.
class BASE_EXPORT Data {
virtual ~Data() = default;
// The user data allows the clients to associate data with this object.
// Multiple user data values can be stored under different keys.
// This object will TAKE OWNERSHIP of the given data pointer, and will
// delete the object if it is changed or the object is destroyed.
// |key| must not be null--that value is too vulnerable for collision.
Data* GetUserData(const void* key) const;
void SetUserData(const void* key, std::unique_ptr<Data> data);
void RemoveUserData(const void* key);
// SupportsUserData is not thread-safe, and on debug build will assert it is
// only used on one execution sequence. Calling this method allows the caller
// to hand the SupportsUserData instance across execution sequences. Use only
// if you are taking full control of the synchronization of that hand over.
void DetachFromSequence();
virtual ~SupportsUserData();
using DataMap = std::map<const void*, std::unique_ptr<Data>>;
// Externally-defined data accessible by key.
DataMap user_data_;
// Guards usage of |user_data_|
SequenceChecker sequence_checker_;
// Adapter class that releases a refcounted object when the
// SupportsUserData::Data object is deleted.
template <typename T>
class UserDataAdapter : public base::SupportsUserData::Data {
static T* Get(const SupportsUserData* supports_user_data, const void* key) {
UserDataAdapter* data =
return data ? static_cast<T*>(data->object_.get()) : NULL;
UserDataAdapter(T* object) : object_(object) {}
T* release() { return object_.release(); }
scoped_refptr<T> object_;
} // namespace base