performance_manager_registered.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2020 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 COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_PERFORMANCE_MANAGER_REGISTERED_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_PERFORMANCE_MANAGER_REGISTERED_H_
  6. #include "components/performance_manager/public/performance_manager.h"
  7. namespace performance_manager {
  8. // This provides functionality that allows an instance of a PM-associated
  9. // object to be looked up by type, allowing the PM to act as a rendezvous
  10. // point for main thread objects. It enforces singleton semantics, so there may
  11. // be no more than one instance of an object of a given type registered with the
  12. // PM at the same time. All registration and unregistration must happen on the
  13. // main thread. It is illegal to register more than one object of the same class
  14. // at a time, and all registered objects must be unregistered prior to PM tear-
  15. // down.
  16. template <typename SelfType>
  17. class PerformanceManagerRegisteredImpl;
  18. // Interface that PerformanceManager registered objects must implement. Should
  19. // only be implemented via PerformanceManagerRegisteredImpl.
  20. class PerformanceManagerRegistered {
  21. public:
  22. PerformanceManagerRegistered(const PerformanceManagerRegistered&) = delete;
  23. PerformanceManagerRegistered& operator=(const PerformanceManagerRegistered&) =
  24. delete;
  25. virtual ~PerformanceManagerRegistered() = default;
  26. // Returns the unique type of the object. Implemented by
  27. // PerformanceManagerRegisteredImpl.
  28. virtual uintptr_t GetTypeId() const = 0;
  29. protected:
  30. template <typename SelfType>
  31. friend class PerformanceManagerRegisteredImpl;
  32. PerformanceManagerRegistered() = default;
  33. };
  34. // Fully implements PerformanceManagerRegistered. Clients should derive from
  35. // this class.
  36. template <typename SelfType>
  37. class PerformanceManagerRegisteredImpl : public PerformanceManagerRegistered {
  38. public:
  39. PerformanceManagerRegisteredImpl() = default;
  40. ~PerformanceManagerRegisteredImpl() override = default;
  41. // The static TypeId associated with this class.
  42. static uintptr_t TypeId() {
  43. // The pointer to this object acts as a unique key that identifies the type
  44. // at runtime. Note that the address of this should be taken only from a
  45. // single library, as a different address will be returned from each library
  46. // into which a given data type is linked. Note that if base/type_id ever
  47. // becomes a thing, this should use that!
  48. static constexpr int kTypeId = 0;
  49. return reinterpret_cast<uintptr_t>(&kTypeId);
  50. }
  51. // PerformanceManagerRegistered implementation:
  52. uintptr_t GetTypeId() const override { return TypeId(); }
  53. // Helper function for looking up the registered object of this type from the
  54. // PM. Syntactic sugar for "PerformanceManager::GetRegisteredObjectAs".
  55. static SelfType* GetFromPM() {
  56. return PerformanceManager::GetRegisteredObjectAs<SelfType>();
  57. }
  58. // Returns true if this object is the registered object in the PM, false
  59. // otherwise. Useful for DCHECKing contract conditions.
  60. bool IsRegisteredInPM() const { return GetFromPM() == this; }
  61. // Returns true if no object of this type is registered in the PM, false
  62. // otherwise. Useful for DCHECKing contract conditions.
  63. static bool NothingRegisteredInPM() { return GetFromPM() == nullptr; }
  64. };
  65. } // namespace performance_manager
  66. #endif // COMPONENTS_PERFORMANCE_MANAGER_PUBLIC_PERFORMANCE_MANAGER_REGISTERED_H_