File indexing completed on 2024-05-19 05:42:15
0001 // ct_lvtplg_pythonlibrarydispatcher.h -*-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 #ifndef DIAGRAM_SERVER_CT_LVTPLG_PYTHONLIBRARYDISPATCHER_H 0021 #define DIAGRAM_SERVER_CT_LVTPLG_PYTHONLIBRARYDISPATCHER_H 0022 0023 #include <ct_lvtplg_abstractlibrarydispatcher.h> 0024 #include <ct_lvtplg_basicpluginhandlers.h> 0025 #include <ct_lvtplg_basicpluginhooks.h> 0026 0027 #include <lvtplg_export.h> 0028 0029 #include <QDebug> 0030 #include <QDir> 0031 #include <QFileInfo> 0032 #include <QLibrary> 0033 #include <QString> 0034 0035 #pragma push_macro("slots") 0036 #undef slots 0037 #include <pybind11/embed.h> 0038 #include <pybind11/functional.h> 0039 #include <pybind11/stl.h> 0040 #pragma pop_macro("slots") 0041 0042 #include <filesystem> 0043 #include <functional> 0044 #include <memory> 0045 #include <optional> 0046 #include <vector> 0047 0048 namespace py = pybind11; 0049 0050 namespace Codethink::lvtplg { 0051 0052 class LVTPLG_EXPORT PythonLibraryDispatcher : public AbstractLibraryDispatcher { 0053 public: 0054 class PyResolveContext : public AbstractLibraryDispatcher::ResolveContext { 0055 public: 0056 PyResolveContext(py::module_& pyModule, std::string const& functionName); 0057 PyResolveContext(PyResolveContext&) = delete; 0058 PyResolveContext(PyResolveContext&&) = delete; 0059 ~PyResolveContext(); 0060 0061 static py::module_ *activeModule; 0062 py::gil_scoped_acquire acquiredGil; 0063 }; 0064 0065 static constexpr auto name = "PythonLib"; 0066 static const std::string pluginDataId; 0067 0068 std::unique_ptr<ResolveContext> resolve(std::string const& functionName) override; 0069 std::string fileName() override; 0070 void reload() override; 0071 void unload() override; 0072 0073 static bool isValidPlugin(QDir const& pluginDir); 0074 static std::unique_ptr<AbstractLibraryDispatcher> loadSinglePlugin(QDir const& pluginDir); 0075 static bool reloadSinglePlugin(QDir const& pluginDir); 0076 0077 private: 0078 py::module_ pyModule; 0079 std::string pluginFolder; 0080 }; 0081 0082 } // namespace Codethink::lvtplg 0083 0084 #endif // DIAGRAM_SERVER_CT_LVTPLG_PYTHONLIBRARYDISPATCHER_H