supports_user_data.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // 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. #ifndef BASE_SUPPORTS_USER_DATA_H_
  5. #define BASE_SUPPORTS_USER_DATA_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/base_export.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/sequence_checker.h"
  11. namespace base {
  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.
  14. class BASE_EXPORT SupportsUserData {
  15. public:
  16. SupportsUserData();
  17. SupportsUserData(SupportsUserData&&);
  18. SupportsUserData& operator=(SupportsUserData&&);
  19. SupportsUserData(const SupportsUserData&) = delete;
  20. SupportsUserData& operator=(const SupportsUserData&) = delete;
  21. // Derive from this class and add your own data members to associate extra
  22. // information with this object. Alternatively, add this as a public base
  23. // class to any class with a virtual destructor.
  24. class BASE_EXPORT Data {
  25. public:
  26. virtual ~Data() = default;
  27. // Returns a copy of |this|; null if copy is not supported.
  28. virtual std::unique_ptr<Data> Clone();
  29. };
  30. // The user data allows the clients to associate data with this object.
  31. // |key| must not be null--that value is too vulnerable for collision.
  32. // NOTE: SetUserData() with an empty unique_ptr behaves the same as
  33. // RemoveUserData().
  34. Data* GetUserData(const void* key) const;
  35. void SetUserData(const void* key, std::unique_ptr<Data> data);
  36. void RemoveUserData(const void* key);
  37. // Adds all data from |other|, that is clonable, to |this|. That is, this
  38. // iterates over the data in |other|, and any data that returns non-null from
  39. // Clone() is added to |this|.
  40. void CloneDataFrom(const SupportsUserData& other);
  41. // SupportsUserData is not thread-safe, and on debug build will assert it is
  42. // only used on one execution sequence. Calling this method allows the caller
  43. // to hand the SupportsUserData instance across execution sequences. Use only
  44. // if you are taking full control of the synchronization of that hand over.
  45. void DetachFromSequence();
  46. protected:
  47. virtual ~SupportsUserData();
  48. // Clear all user data from this object. This can be used if the subclass
  49. // needs to provide reset functionality.
  50. void ClearAllUserData();
  51. private:
  52. using DataMap = std::map<const void*, std::unique_ptr<Data>>;
  53. // Externally-defined data accessible by key.
  54. DataMap user_data_;
  55. // Guards usage of |user_data_|
  56. SequenceChecker sequence_checker_;
  57. };
  58. // Adapter class that releases a refcounted object when the
  59. // SupportsUserData::Data object is deleted.
  60. template <typename T>
  61. class UserDataAdapter : public SupportsUserData::Data {
  62. public:
  63. static T* Get(const SupportsUserData* supports_user_data, const void* key) {
  64. UserDataAdapter* data =
  65. static_cast<UserDataAdapter*>(supports_user_data->GetUserData(key));
  66. return data ? static_cast<T*>(data->object_.get()) : nullptr;
  67. }
  68. explicit UserDataAdapter(T* object) : object_(object) {}
  69. UserDataAdapter(const UserDataAdapter&) = delete;
  70. UserDataAdapter& operator=(const UserDataAdapter&) = delete;
  71. ~UserDataAdapter() override = default;
  72. T* release() { return object_.release(); }
  73. private:
  74. scoped_refptr<T> const object_;
  75. };
  76. } // namespace base
  77. #endif // BASE_SUPPORTS_USER_DATA_H_