• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 HIVIEW_BASE_PLUGIN_FACTORY_H
16 #define HIVIEW_BASE_PLUGIN_FACTORY_H
17 
18 #include <memory>
19 #include <type_traits>
20 #include <unordered_map>
21 
22 #include "plugin.h"
23 #include "plugin_proxy.h"
24 
25 namespace OHOS {
26 namespace HiviewDFX {
27 struct DllExport PluginRegistInfo {
PluginRegistInfoPluginRegistInfo28     PluginRegistInfo(std::function<std::shared_ptr<Plugin>()> getPluginObject,
29         bool needCreateProxy, bool needStartupLoading)
30         : getPluginObject(getPluginObject),
31           needCreateProxy(needCreateProxy),
32           needStartupLoading(needStartupLoading) {};
33     std::function<std::shared_ptr<Plugin>()> getPluginObject;
34     bool needCreateProxy;
35     bool needStartupLoading;
36 };
37 
38 class DllExport PluginFactory {
39 public:
40     static void RegisterPlugin(const std::string& name, std::shared_ptr<PluginRegistInfo> func);
41     static void UnregisterPlugin(const std::string& name);
42     static std::shared_ptr<Plugin> GetPlugin(const std::string& name);
43     static std::shared_ptr<PluginRegistInfo> GetGlobalPluginInfo(const std::string& name);
44 
45 private:
46     static std::shared_ptr<std::unordered_map<std::string, std::shared_ptr<PluginRegistInfo>>>
47         GetGlobalPluginRegistryMap();
48 };
49 
50 class PluginRegister {
51 public:
PluginRegister(const std::string & name,std::shared_ptr<PluginRegistInfo> fp)52     PluginRegister(const std::string& name, std::shared_ptr<PluginRegistInfo> fp)
53     {
54         PluginFactory::RegisterPlugin(name, fp);
55     };
~PluginRegister()56     ~PluginRegister(){};
57 };
58 
59 #define REGISTER__(ClassName, needCreateProxy, needStartupLoading)              \
60 class Register##ClassName {                                                     \
61     public:                                                                     \
62         static std::shared_ptr<Plugin> GetObject()                              \
63         {                                                                       \
64             return std::make_shared<ClassName>();                               \
65         }                                                                       \
66     private:                                                                    \
67         static const PluginRegister g_staticPluginRegister;                     \
68 };                                                                              \
69 const PluginRegister Register##ClassName::g_staticPluginRegister(#ClassName,    \
70     std::make_shared<PluginRegistInfo>(Register##ClassName::GetObject,          \
71     needCreateProxy, needStartupLoading));
72 
73 #define PROXY_ASSERT(ClassName)                                                 \
74     class EventSource;                                                          \
75     static_assert(!(std::is_base_of<EventSource, ClassName>::value),            \
76         "EventSource cannot use Proxy");                                        \
77     class EventListener;                                                        \
78     static_assert(!(std::is_base_of<EventListener, ClassName>::value),          \
79         "EventListener cannot use Proxy");
80 
81 #define REGISTER(ClassName) REGISTER__(ClassName, false, true);
82 #define REGISTER_PROXY(ClassName)                                               \
83     PROXY_ASSERT(ClassName)                                                     \
84     REGISTER__(ClassName, true, false)
85 #define REGISTER_PROXY_WITH_LOADED(ClassName)                                   \
86     PROXY_ASSERT(ClassName)                                                     \
87     REGISTER__(ClassName, true, true)
88 } // namespace HiviewDFX
89 } // namespace OHOS
90 #endif // HIVIEW_BASE_PLUGIN_FACTORY_H
91