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