• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FACTORY_H
17 #define API_CORE_PLUGIN_ICLASS_FACTORY_H
18 
19 #include <base/util/uid.h>
20 #include <core/namespace.h>
21 #include <core/plugin/intf_class_register.h>
22 #include <core/plugin/intf_interface.h>
23 #include <core/plugin/intf_plugin.h>
24 
CORE_BEGIN_NAMESPACE()25 CORE_BEGIN_NAMESPACE()
26 class IClassFactory : public IInterface {
27 public:
28     static constexpr BASE_NS::Uid UID { "3a4cad5c-0e16-4708-bd83-626d136a7215" };
29 
30     /** Create a new interface instance by UID */
31     virtual IInterface::Ptr CreateInstance(const BASE_NS::Uid& uid) = 0;
32 };
33 
34 /** Create interface from specified interface registry */
35 template<class T>
CreateInstance(IClassFactory & factory,const BASE_NS::Uid & uid)36 auto CreateInstance(IClassFactory& factory, const BASE_NS::Uid& uid)
37 {
38     // Create the instance
39     if (auto res = factory.CreateInstance(uid)) {
40         // Ask for the interface.
41         return typename T::Ptr(res->GetInterface<T>());
42     }
43     return typename T::Ptr();
44 }
45 
46 /** Creates a class instance by using a specific IClassFactory instance from global registry.
47     and tries to get the requested interface from it.
48     Returns empty T::Ptr (null) if the class does not support the interface (or the class could not be created).
49 */
50 template<class T>
CreateInstance(const BASE_NS::Uid & factory_id,const BASE_NS::Uid & class_id)51 typename T::Ptr CreateInstance(const BASE_NS::Uid& factory_id, const BASE_NS::Uid& class_id)
52 {
53     if (auto factory = GetInstance<IClassFactory>(factory_id)) {
54         return CreateInstance<T>(*factory, class_id);
55     }
56     return typename T::Ptr();
57 }
58 
59 /** Creates a class instance by using global registrys class factory.
60     and tries to get the requested interface from it.
61     Returns empty T::Ptr (null) if the class does not support the interface (or the class could not be created).
62 */
63 template<class T>
CreateInstance(const BASE_NS::Uid & class_id)64 typename T::Ptr CreateInstance(const BASE_NS::Uid& class_id)
65 {
66     auto factory = GetPluginRegister().GetClassRegister().GetInterface<IClassFactory>();
67     if (factory) {
68         return CreateInstance<T>(*factory, class_id);
69     }
70     return typename T::Ptr();
71 }
72 
73 /** @} */
74 CORE_END_NAMESPACE()
75 
76 #endif // API_CORE_PLUGIN_ICLASS_FACTORY_H
77