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