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 
16 #ifndef PLUGIN_UTILS_H
17 #define PLUGIN_UTILS_H
18 
19 #include <map>
20 #include <string>
21 #include "plugin_class_base.h"
22 
23 namespace OHOS {
24 namespace MultimediaPlugin {
25 template<class ImplClassType>
CreatePluginObject()26 PluginClassBase *CreatePluginObject()
27 {
28     return static_cast<PluginClassBase *>(new (std::nothrow) ImplClassType());  // upward conversion.
29 }
30 } // namespace MultimediaPlugin
31 } // namespace OHOS
32 
33 #define PLUGIN_OBJECT_CREATOR(ImplClassType) OHOS::MultimediaPlugin::CreatePluginObject<ImplClassType>
34 
35 #define IMPL_CLASS_NAME_STRING(ImplClassType) (#ImplClassType)
36 
37 using PluginObjectCreatorFunc = OHOS::MultimediaPlugin::PluginClassBase *(*)();
38 
39 // --------- a set of code fragments that helps define a simple plugin_export.cpp file ----------
40 #define PLUGIN_EXPORT_REGISTER_PACKAGE(packageName) \
41 namespace { \
42     const std::string PACKAGE_NAME = (packageName); \
43 }
44 
45 #define PLUGIN_EXPORT_REGISTER_CLASS_BEGIN \
46 using ImplClassMap = std::map<const std::string, PluginObjectCreatorFunc>; \
47 static ImplClassMap implClassMap = {
48 
49 #define PLUGIN_EXPORT_REGISTER_CLASS(ImplClassType) \
50 { IMPL_CLASS_NAME_STRING(ImplClassType), PLUGIN_OBJECT_CREATOR(ImplClassType) },
51 
52 #define PLUGIN_EXPORT_REGISTER_CLASS_END \
53 };
54 
55 #define PLUGIN_EXPORT_DEFAULT_EXTERNAL_START() \
56 bool PluginExternalStart() \
57 {                                                               \
58     IMAGE_LOGD("call PluginExternalStart() in package: %{public}s.", PACKAGE_NAME.c_str()); \
59     return true;                                             \
60 }
61 
62 #define PLUGIN_EXPORT_DEFAULT_EXTERNAL_STOP() \
63 void PluginExternalStop() \
64 {                                                              \
65     IMAGE_LOGD("call PluginExternalStop() in package: %{public}s.", PACKAGE_NAME.c_str()); \
66     return;                                                    \
67 }
68 #endif // PLUGIN_UTILS_H
69