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