supports_user_data.h 2.95 KB
Newer Older
1 2 3 4 5 6 7 8
// 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.

#ifndef BASE_SUPPORTS_USER_DATA_H_
#define BASE_SUPPORTS_USER_DATA_H_

#include <map>
9
#include <memory>
10 11

#include "base/base_export.h"
12
#include "base/macros.h"
13
#include "base/memory/ref_counted.h"
14 15 16 17
#include "base/sequence_checker.h"

// TODO(gab): Removing this include causes IWYU failures in other headers,
// remove it in a follow- up CL.
18
#include "base/threading/thread_checker.h"
19 20 21 22 23 24 25 26 27 28

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 {
 public:
  SupportsUserData();

  // Derive from this class and add your own data members to associate extra
avi@chromium.org's avatar
avi@chromium.org committed
29 30
  // information with this object. Alternatively, add this as a public base
  // class to any class with a virtual destructor.
31 32
  class BASE_EXPORT Data {
   public:
33
    virtual ~Data() = default;
34 35 36 37 38 39
  };

  // 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.
40 41
  // TODO: remove the raw ptr version of SetUserData once everything uses
  // the unique_ptr version, see crbug.com/690937.
42 43
  Data* GetUserData(const void* key) const;
  void SetUserData(const void* key, Data* data);
44
  void SetUserData(const void* key, std::unique_ptr<Data> data);
45
  void RemoveUserData(const void* key);
46

47
  // SupportsUserData is not thread-safe, and on debug build will assert it is
48 49 50 51
  // 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();
52

53 54 55
 protected:
  virtual ~SupportsUserData();

56
 private:
57
  using DataMap = std::map<const void*, std::unique_ptr<Data>>;
58

avi@chromium.org's avatar
avi@chromium.org committed
59
  // Externally-defined data accessible by key.
60
  DataMap user_data_;
61
  // Guards usage of |user_data_|
62
  SequenceChecker sequence_checker_;
63 64 65 66

  DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
};

67 68 69 70 71
// Adapter class that releases a refcounted object when the
// SupportsUserData::Data object is deleted.
template <typename T>
class UserDataAdapter : public base::SupportsUserData::Data {
 public:
72
  static T* Get(const SupportsUserData* supports_user_data, const void* key) {
73
    UserDataAdapter* data =
74
      static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
75
    return data ? static_cast<T*>(data->object_.get()) : NULL;
76 77 78
  }

  UserDataAdapter(T* object) : object_(object) {}
79
  T* release() { return object_.release(); }
80 81 82 83 84 85 86

 private:
  scoped_refptr<T> object_;

  DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
};

87 88 89
}  // namespace base

#endif  // BASE_SUPPORTS_USER_DATA_H_