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