metadata_cache.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2019 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 UI_BASE_METADATA_METADATA_CACHE_H_
  5. #define UI_BASE_METADATA_METADATA_CACHE_H_
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/component_export.h"
  10. #include "ui/base/metadata/metadata_types.h"
  11. namespace ui {
  12. namespace metadata {
  13. class ClassMetaData;
  14. // The MetaDataCache speeds up frequent traversals over the meta data for
  15. // various classes by caching one instance of ClassMetaData for each class on
  16. // which metadata is required.
  17. // MetaDataCache is implemented as a singleton. This also implies that each
  18. // instance of ClassMetaData registered into the cache represents one and only
  19. // one class type.
  20. class COMPONENT_EXPORT(UI_BASE_METADATA) MetaDataCache {
  21. public:
  22. MetaDataCache();
  23. MetaDataCache(const MetaDataCache&) = delete;
  24. MetaDataCache& operator=(const MetaDataCache&) = delete;
  25. static MetaDataCache* GetInstance();
  26. void AddClassMetaData(std::unique_ptr<ClassMetaData> class_data);
  27. std::vector<ClassMetaData*>& GetCachedTypes();
  28. private:
  29. ~MetaDataCache();
  30. std::vector<ClassMetaData*> class_data_cache_;
  31. };
  32. // These functions are rarely called directly, rather they are called from
  33. // within the macros used to declare the metadata for a class. See the macros in
  34. // reflections_macros.h
  35. //
  36. // Registers the class metadata into the global cache. Will DCHECK if the
  37. // metadata for a class is already registered.
  38. COMPONENT_EXPORT(UI_BASE_METADATA)
  39. void RegisterClassInfo(std::unique_ptr<ClassMetaData> meta_data);
  40. // Help function for creating and registering the metadata container into the
  41. // global cache for a given class. The metadata information is owned by the
  42. // given class.
  43. template <typename TMetaData>
  44. ClassMetaData* MakeAndRegisterClassInfo() {
  45. std::unique_ptr<TMetaData> class_meta_data = std::make_unique<TMetaData>();
  46. TMetaData* const ret = class_meta_data.get();
  47. RegisterClassInfo(std::move(class_meta_data));
  48. return ret;
  49. }
  50. } // namespace metadata
  51. } // namespace ui
  52. #endif // UI_BASE_METADATA_METADATA_CACHE_H_