1 /*
2 * Copyright (C) 2023 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
16 #ifndef API_CORE_PLUGIN_ICLASS_REGISTER_H
17 #define API_CORE_PLUGIN_ICLASS_REGISTER_H
18
19 #include <base/containers/array_view.h>
20 #include <base/util/uid.h>
21 #include <core/namespace.h>
22 #include <core/plugin/intf_interface.h>
23 #include <core/plugin/intf_plugin.h>
24 #include <core/plugin/intf_plugin_register.h>
25
CORE_BEGIN_NAMESPACE()26 CORE_BEGIN_NAMESPACE()
27 class IClassRegister : public IInterface {
28 public:
29 static constexpr BASE_NS::Uid UID { "fcdce31c-1208-4a4e-9ccf-292f87c9dbe0" };
30
31 /** Register interface type
32 * @param interfaceInfo InterfaceTypeInfo to be registered
33 */
34 virtual void RegisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) = 0;
35
36 /** Unregister interface type
37 * @param interfaceInfo InterfaceTypeInfo to unregister.
38 */
39 virtual void UnregisterInterfaceType(const InterfaceTypeInfo& interfaceInfo) = 0;
40
41 /** Returns interface metadata */
42 virtual BASE_NS::array_view<const InterfaceTypeInfo* const> GetInterfaceMetadata() const = 0;
43
44 /** Get interface info by UID */
45 virtual const InterfaceTypeInfo& GetInterfaceMetadata(const BASE_NS::Uid& uid) const = 0;
46
47 /** Get interface singleton instance by UID */
48 virtual IInterface* GetInstance(const BASE_NS::Uid& uid) const = 0;
49
50 protected:
51 IClassRegister() = default;
52 virtual ~IClassRegister() = default;
53 };
54
55 /** Get class instance from specified class registry */
56 template<class T>
GetInstance(IClassRegister & registry,const BASE_NS::Uid & uid)57 auto GetInstance(IClassRegister& registry, const BASE_NS::Uid& uid)
58 {
59 IInterface* instance = static_cast<T*>(registry.GetInstance(uid));
60 if (instance) {
61 return static_cast<T*>(instance->GetInterface(T::UID));
62 }
63 return static_cast<T*>(nullptr);
64 }
65
66 /** Get class instance from specified class registry */
67 template<class T>
GetInstance(const IClassRegister & registry,const BASE_NS::Uid & uid)68 auto GetInstance(const IClassRegister& registry, const BASE_NS::Uid& uid)
69 {
70 return GetInstance<T>(const_cast<IClassRegister&>(registry), uid);
71 }
72
73 /** Get interface from global registry */
74 template<class T>
GetInstance(const BASE_NS::Uid & uid)75 auto GetInstance(const BASE_NS::Uid& uid)
76 {
77 return GetInstance<T>(CORE_NS::GetPluginRegister().GetClassRegister(), uid);
78 }
79
80 /** @} */
81 CORE_END_NAMESPACE()
82
83 #endif // API_CORE_PLUGIN_ICLASS_REGISTER_H
84