1// Copyright 2013 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 IOS_PUBLIC_CONSUMER_BASE_SUPPORTS_USER_DATA_H_
6#define IOS_PUBLIC_CONSUMER_BASE_SUPPORTS_USER_DATA_H_
7
8namespace ios {
9
10class SupportsUserDataInternal;
11
12// This is a helper for classes that want to allow users to stash random data by
13// key. At destruction all the objects will be destructed.
14class SupportsUserData {
15 public:
16  SupportsUserData();
17
18  // Derive from this class and add your own data members to associate extra
19  // information with this object. Alternatively, add this as a public base
20  // class to any class with a virtual destructor.
21  class Data {
22   public:
23    virtual ~Data() {}
24  };
25
26  // The user data allows the clients to associate data with this object.
27  // Multiple user data values can be stored under different keys.
28  // This object will TAKE OWNERSHIP of the given data pointer, and will
29  // delete the object if it is changed or the object is destroyed.
30  Data* GetUserData(const void* key) const;
31  void SetUserData(const void* key, Data* data);
32  void RemoveUserData(const void* key);
33
34  // SupportsUserData is not thread-safe, and on debug build will assert it is
35  // only used on one thread. Calling this method allows the caller to hand
36  // the SupportsUserData instance across threads. Use only if you are taking
37  // full control of the synchronization of that handover.
38  void DetachUserDataThread();
39
40 protected:
41  virtual ~SupportsUserData();
42
43 private:
44  // Owned by this object and scoped to its lifetime.
45  SupportsUserDataInternal* internal_helper_;
46};
47
48}  // namespace ios
49
50#endif  // IOS_PUBLIC_CONSUMER_BASE_SUPPORTS_USER_DATA_H_
51