supports_user_data.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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/macros.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "base/sequence_checker.h"
  12. namespace base {
  13. // This is a helper for classes that want to allow users to stash random data by
  14. // key. At destruction all the objects will be destructed.
  15. class BASE_EXPORT SupportsUserData {
  16. public:
  17. SupportsUserData();
  18. SupportsUserData(SupportsUserData&&);
  19. SupportsUserData& operator=(SupportsUserData&&);
  20. // Derive from this class and add your own data members to associate extra
  21. // information with this object. Alternatively, add this as a public base
  22. // class to any class with a virtual destructor.
  23. class BASE_EXPORT Data {
  24. public:
  25. virtual ~Data() = default;
  26. // Returns a copy of |this|; null if copy is not supported.
  27. virtual std::unique_ptr<Data> Clone();
  28. };
  29. // The user data allows the clients to associate data with this object.
  30. // |key| must not be null--that value is too vulnerable for collision.
  31. // NOTE: SetUserData() with an empty unique_ptr behaves the same as
  32. // RemoveUserData().
  33. Data* GetUserData(const void* key) const;
  34. void SetUserData(const void* key, std::unique_ptr<Data> data);
  35. void RemoveUserData(const void* key);
  36. // Adds all data from |other|, that is clonable, to |this|. That is, this
  37. // iterates over the data in |other|, and any data that returns non-null from
  38. // Clone() is added to |this|.
  39. void CloneDataFrom(const SupportsUserData& other);
  40. // SupportsUserData is not thread-safe, and on debug build will assert it is
  41. // only used on one execution sequence. Calling this method allows the caller
  42. // to hand the SupportsUserData instance across execution sequences. Use only
  43. // if you are taking full control of the synchronization of that hand over.
  44. void DetachFromSequence();
  45. protected:
  46. virtual ~SupportsUserData();
  47. // Clear all user data from this object. This can be used if the subclass
  48. // needs to provide reset functionality.
  49. void ClearAllUserData();
  50. private:
  51. using DataMap = std::map<const void*, std::unique_ptr<Data>>;
  52. // Externally-defined data accessible by key.
  53. DataMap user_data_;
  54. // Guards usage of |user_data_|
  55. SequenceChecker sequence_checker_;
  56. DISALLOW_COPY_AND_ASSIGN(SupportsUserData);
  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() override = default;
  70. T* release() { return object_.release(); }
  71. private:
  72. scoped_refptr<T> const object_;
  73. DISALLOW_COPY_AND_ASSIGN(UserDataAdapter);
  74. };
  75. } // namespace base
  76. #endif // BASE_SUPPORTS_USER_DATA_H_