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#include <core/intf_engine.h>
17#include <core/plugin/intf_plugin.h>
18#include <core/plugin/intf_plugin_register.h>
19
20#include <meta/base/plugin.h>
21
22#include "meta_object_lib.h"
23
24namespace {
25static CORE_NS::IPluginRegister* g_pluginRegistry { nullptr };
26} // namespace
27
28CORE_BEGIN_NAMESPACE()
29IPluginRegister& GetPluginRegister()
30{
31    return *g_pluginRegistry;
32}
33CORE_END_NAMESPACE()
34
35using namespace CORE_NS;
36using namespace META_NS;
37
38META_BEGIN_NAMESPACE()
39
40namespace {
41
42MetaObjectLib* g_state = nullptr;
43
44// Register our interface to the GLOBAL registry.
45constexpr CORE_NS::InterfaceTypeInfo INTERFACE = { &g_state, IMetaObjectLib::UID, "Meta Object Lib", nullptr,
46    [](CORE_NS::IClassRegister& registry, CORE_NS::PluginToken token) -> CORE_NS::IInterface* {
47        return *static_cast<IMetaObjectLib**>(token);
48    } };
49
50PluginToken RegisterInterfaces(IPluginRegister& pluginRegistry)
51{
52    // Initializing dynamic plugin.
53    // Pluginregistry access via the provided registry instance which is saved here.
54    g_pluginRegistry = &pluginRegistry;
55    if (g_state == nullptr) {
56        g_state = new MetaObjectLib;
57
58        auto& classRegister = CORE_NS::GetPluginRegister().GetClassRegister();
59        classRegister.RegisterInterfaceType(INTERFACE);
60
61        g_state->Initialize();
62    }
63    return g_state;
64}
65void UnregisterInterfaces(PluginToken token)
66{
67    if (token != g_state) {
68        return;
69    }
70    if (g_state) {
71        g_state->Uninitialize();
72
73        delete g_state;
74        g_state = nullptr;
75
76        auto& classRegister = CORE_NS::GetPluginRegister().GetClassRegister();
77        classRegister.UnregisterInterfaceType(INTERFACE);
78    }
79
80    g_pluginRegistry = nullptr;
81}
82const char* VersionString()
83{
84    return META_VERSION_STRING;
85}
86
87// const BASE_NS::Uid plugin_deps[] {};
88} // namespace
89
90META_END_NAMESPACE()
91
92extern "C" {
93#if defined(_MSC_VER)
94_declspec(dllexport)
95#else
96__attribute__((visibility("default")))
97#endif
98    // NOLINTNEXTLINE(readability-identifier-naming) to keep LumeEngine convention
99    CORE_NS::IPlugin gPluginData { { IPlugin::UID }, "MetaObject",
100        /** Version information of the plugin. */
101        CORE_NS::Version { META_NS::META_OBJECT_PLUGIN_UID, META_NS::VersionString }, META_NS::RegisterInterfaces,
102        META_NS::UnregisterInterfaces, {}};
103}
104