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 <shared_mutex>
19
20 #include <base/containers/unordered_map.h>
21
22 #include <meta/base/namespace.h>
23 #include <meta/ext/event_impl.h>
24 #include <meta/ext/implementation_macros.h>
25 #include <meta/ext/object_factory.h>
26 #include <meta/interface/intf_class_registry.h>
27 #include <meta/interface/intf_object_factory.h>
28 #include <meta/interface/object_type_info.h>
29
META_BEGIN_NAMESPACE()30 META_BEGIN_NAMESPACE()
31
32 /** If strict=true, return true if all bits from bitmask are set in compareTo.
33 * If strict=false, return true if any bits from bitmask are set in compareTo. */
34 constexpr inline bool CheckCategoryBits(ObjectCategoryBits compareTo, ObjectCategoryBits bitmask, bool strict)
35 {
36 return strict ? (compareTo & bitmask) == bitmask : (compareTo & bitmask) != 0;
37 }
38
39 class ClassRegistry final : public IntroduceInterfaces<IClassRegistry> {
40 public:
41 void Clear();
42 bool Register(const IObjectFactory::Ptr& fac);
43 bool Unregister(const IObjectFactory::Ptr& fac);
44
45 BASE_NS::string GetClassName(BASE_NS::Uid uid) const;
46 IObjectFactory::ConstPtr GetObjectFactory(const BASE_NS::Uid& uid) const;
47 BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes(
48 ObjectCategoryBits category, bool strict, bool excludeDeprecated) const;
49
EventOnClassRegistered(META_NS::MetadataQuery q)50 BASE_NS::shared_ptr<IEvent> EventOnClassRegistered(META_NS::MetadataQuery q) const override
51 {
52 std::unique_lock lock(mutex_);
53 if (!onRegistered_ && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
54 onRegistered_ = CreateShared<EventImpl<IOnClassRegistrationChanged>>("OnClassRegistered");
55 }
56 return onRegistered_;
57 }
EventOnClassUnregistered(META_NS::MetadataQuery q)58 BASE_NS::shared_ptr<IEvent> EventOnClassUnregistered(META_NS::MetadataQuery q) const override
59 {
60 std::unique_lock lock(mutex_);
61 if (!onUnregistered_ && q == MetadataQuery::CONSTRUCT_ON_REQUEST) {
62 onUnregistered_ = CreateShared<EventImpl<IOnClassRegistrationChanged>>("OnClassUnregistered");
63 }
64 return onUnregistered_;
65 }
66
67 public: // IClassRegistry
68 BASE_NS::vector<IClassInfo::ConstPtr> GetAllTypes(
69 const BASE_NS::vector<BASE_NS::Uid>& interfaceUids, bool strict, bool excludeDeprecated) const override;
70
71 private:
72 mutable std::shared_mutex mutex_;
73 mutable BASE_NS::unordered_map<ObjectId, IObjectFactory::Ptr> objectFactories_;
74 mutable BASE_NS::shared_ptr<EventImpl<IOnClassRegistrationChanged>> onRegistered_;
75 mutable BASE_NS::shared_ptr<EventImpl<IOnClassRegistrationChanged>> onUnregistered_;
76 };
77
78 META_END_NAMESPACE()
79
80 #endif // META_SRC_CLASS_REGISTRY_H
81