File indexing completed on 2024-05-19 05:42:15

0001 // ct_lvtplg_sharedlibrarydispatcher.cpp                             -*-C++-*-
0002 
0003 /*
0004 // Copyright 2023 Codethink Ltd <codethink@codethink.co.uk>
0005 // SPDX-License-Identifier: Apache-2.0
0006 //
0007 // Licensed under the Apache License, Version 2.0 (the "License");
0008 // you may not use this file except in compliance with the License.
0009 // You may obtain a copy of the License at
0010 //
0011 //     http://www.apache.org/licenses/LICENSE-2.0
0012 //
0013 // Unless required by applicable law or agreed to in writing, software
0014 // distributed under the License is distributed on an "AS IS" BASIS,
0015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0016 // See the License for the specific language governing permissions and
0017 // limitations under the License.
0018 */
0019 
0020 #include <QDebug>
0021 #include <ct_lvtplg_sharedlibrarydispatcher.h>
0022 
0023 namespace Codethink::lvtplg {
0024 
0025 const std::string SharedLibraryDispatcher::pluginDataId = "private::soLibData";
0026 
0027 SharedLibraryDispatcher::SharedLibraryDispatcher(QString const& fileName): library(fileName)
0028 {
0029 }
0030 
0031 std::unique_ptr<AbstractLibraryDispatcher::ResolveContext>
0032 SharedLibraryDispatcher::resolve(std::string const& functionName)
0033 {
0034     return std::make_unique<AbstractLibraryDispatcher::ResolveContext>(library.resolve(functionName.c_str()));
0035 }
0036 
0037 std::string SharedLibraryDispatcher::fileName()
0038 {
0039     return library.fileName().toStdString();
0040 }
0041 
0042 // TODO: Implement Shared Library Reload
0043 void SharedLibraryDispatcher::reload()
0044 {
0045     qWarning() << "Shared Library Reload: Not implemented yet";
0046 }
0047 
0048 // TODO: Implement Shared Library Unload
0049 void SharedLibraryDispatcher::unload()
0050 {
0051     qWarning() << "Shared Library Reload: Not implemented yet";
0052 }
0053 
0054 bool SharedLibraryDispatcher::isValidPlugin(QDir const& pluginDir)
0055 {
0056     const auto pluginName = pluginDir.dirName();
0057     // TODO: MacOS support
0058 #if defined(Q_OS_WINDOWS)
0059     const auto so_ext = QStringLiteral(".dll");
0060 #else
0061     const auto so_ext = QStringLiteral(".so");
0062 #endif
0063     const bool metadataExists = pluginDir.exists(QStringLiteral("metadata.json"));
0064     const bool readmeExists = pluginDir.exists(QStringLiteral("README.md"));
0065     const bool pluginExists = pluginDir.exists(pluginName + so_ext);
0066     return metadataExists && readmeExists && pluginExists;
0067 }
0068 
0069 std::unique_ptr<AbstractLibraryDispatcher> SharedLibraryDispatcher::loadSinglePlugin(QDir const& pluginDir)
0070 {
0071     auto pluginName = pluginDir.dirName();
0072     auto pluginSharedObjectBasename = pluginDir.path() + QDir::separator() + pluginName;
0073     auto lib = std::make_unique<SharedLibraryDispatcher>(pluginSharedObjectBasename);
0074     if (!lib->library.load()) {
0075         return nullptr;
0076     }
0077     return lib;
0078 }
0079 
0080 } // namespace Codethink::lvtplg