1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef META_SRC_CLASS_REGISTRY_H
16 #define META_SRC_CLASS_REGISTRY_H
17
18 #include <mutex>
19 #include <shared_mutex>
20
21 #include <base/containers/unordered_map.h>
22
23 #include <meta/base/namespace.h>
24 #include <meta/ext/event_impl.h>
25 #include <meta/ext/implementation_macros.h>
26 #include <meta/ext/object_factory.h>
27 #include <meta/interface/intf_class_registry.h>
28 #include <meta/interface/intf_object_factory.h>
29 #include <meta/interface/object_type_info.h>
30
META_BEGIN_NAMESPACE()31 META_BEGIN_NAMESPACE()
32
33 /** If strict=true, return true if all bits from bitmask are set in compareTo.
34 * If strict=false, return true if any bits from bitmask are set in compareTo. */
35 constexpr inline bool CheckCategoryBits(ObjectCategoryBits compareTo, ObjectCategoryBits bitmask, bool strict)
36 {
37 return strict ? (compareTo & bitmask) == bitmask : (compareTo & bitmask) != 0;
38 }
39
40 class ClassRegistry final : public IntroduceInterfaces<IClassRegistry> {
41 public:
42 void Clear();
43 bool Register(const IObjectFactory::Ptr& fac);
44 bool Unregister(const IObjectFactory::Ptr& fac);
45
46 BASE_NS::string GetClassName(BASE_NS::Uid uid) const;
47 IObjectFactory::ConstPtr GetObjectFactory(const BASE_NS::Uid& uid) const;
48 BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes(
49 ObjectCategoryBits category, bool strict, bool excludeDeprecated) const;
50
EventOnClassRegistered(META_NS::MetadataQuery q)51 BASE_NS::shared_ptr<IEvent> EventOnClassRegistered(META_NS::MetadataQuery q) const override
52 {
53 std::unique_lock lock(mutex_);
54 if (!onRegistered_ && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
55 onRegistered_ = CreateShared<EventImpl<IOnClassRegistrationChanged>>("OnClassRegistered");
56 }
57 return onRegistered_;
58 }
EventOnClassUnregistered(META_NS::MetadataQuery q)59 BASE_NS::shared_ptr<IEvent> EventOnClassUnregistered(META_NS::MetadataQuery q) const override
60 {
61 std::unique_lock lock(mutex_);
62 if (!onUnregistered_ && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
63 onUnregistered_ = CreateShared<EventImpl<IOnClassRegistrationChanged>>("OnClassUnregistered");
64 }
65 return onUnregistered_;
66 }
67
68 public: // IClassRegistry
69 BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes(
70 const BASE_NS::vector<BASE_NS::Uid>& interfaceUids, bool strict, bool excludeDeprecated) const override;
71
72 private:
73 mutable std::shared_mutex mutex_;
74 mutable BASE_NS::unordered_map<ObjectId, IObjectFactory::Ptr> objectFactories_;
75 mutable BASE_NS::shared_ptr<EventImpl<IOnClassRegistrationChanged>> onRegistered_;
76 mutable BASE_NS::shared_ptr<EventImpl<IOnClassRegistrationChanged>> onUnregistered_;
77 };
78
79 META_END_NAMESPACE()
80
81 #endif // META_SRC_CLASS_REGISTRY_H
82