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

0001 // ct_lvtplg_pythonmodule.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 <ct_lvtplg_basicpluginhandlers.h>
0021 #include <ct_lvtplg_basicpluginhooks.h>
0022 
0023 #pragma push_macro("slots")
0024 #undef slots
0025 #include <pybind11/functional.h>
0026 #include <pybind11/pybind11.h>
0027 #include <pybind11/stl.h>
0028 #pragma pop_macro("slots")
0029 
0030 #include <optional>
0031 #include <string>
0032 
0033 #if defined AS_EMBEDDED
0034 #include <pybind11/embed.h>
0035 #define PYMODULE_TYPE PYBIND11_EMBEDDED_MODULE
0036 #elif defined AS_MODULE
0037 #define PYMODULE_TYPE PYBIND11_MODULE
0038 #else
0039 #error "Please select compilation macro option: AS_EMBEDDED or AS_MODULE"
0040 #endif
0041 
0042 namespace py = pybind11;
0043 
0044 namespace Codethink::lvtplg {
0045 
0046 template<typename T>
0047 auto pyRegisterPluginData(T const& self, std::string const& id, py::object pyObject)
0048 {
0049     auto *rawPtr = static_cast<void *>(pyObject.ptr());
0050     Py_XINCREF(rawPtr);
0051     return self.registerPluginData(id, rawPtr);
0052 }
0053 
0054 template<typename T>
0055 auto pyGetPluginData(T const& self, std::string const& id)
0056 {
0057     auto *rawPtr = static_cast<PyObject *>(self.getPluginData(id));
0058     return py::reinterpret_borrow<py::object>(rawPtr);
0059 }
0060 
0061 template<typename T>
0062 auto pyUnregisterPluginData(T const& self, std::string const& id)
0063 {
0064     auto *rawPtr = static_cast<PyObject *>(self.getPluginData(id));
0065     Py_XDECREF(rawPtr);
0066     return self.unregisterPluginData(id);
0067 }
0068 } // namespace Codethink::lvtplg
0069 
0070 // clang-format off
0071 template<typename ModuleType>
0072 void exportDataTypes(ModuleType& m)
0073 {
0074     {
0075         using T = EntityType;
0076         py::enum_<T>(m, "EntityType")
0077             .value("Unknown", T::Unknown)
0078             .value("PackageGroup", T::PackageGroup)
0079             .value("Package", T::Package)
0080             .value("Component", T::Component)
0081             .export_values();
0082     }
0083 
0084     {
0085         using T = Color;
0086         py::class_<T>(m, "Color")
0087             .def(py::init<int, int, int>())
0088             .def(py::init<int, int, int, int>())
0089             .def_readwrite("r", &T::r)
0090             .def_readwrite("g", &T::g)
0091             .def_readwrite("b", &T::b)
0092             .def_readwrite("a", &T::a);
0093     }
0094 
0095     {
0096         using T = Entity;
0097         py::class_<T>(m, "Entity")
0098             .def("getName", [](T const& self) { return self.getName(); })
0099             .def("getQualifiedName", [](T const& self) { return self.getQualifiedName(); })
0100             .def("getType", [](T const& self) { return self.getType(); })
0101             .def("setColor", [](T const& self, Color rgbColor) { return self.setColor(rgbColor); }, py::arg("rgbColor"))
0102             .def("addHoverInfo", [](T const& self, std::string info) { return self.addHoverInfo(std::move(info)); }, py::arg("info"))
0103             .def("getDependencies", [](T const& self) { return self.getDependencies(); })
0104             .def("unloadFromScene", [](T const& self) { return self.unloadFromScene(); })
0105             .def("getDbChildrenQualifiedNames", [](T const& self) { return self.getDbChildrenQualifiedNames(); })
0106             .def("getParent", [](T const& self) { return self.getParent(); })
0107             .def("setSelected", [](T const& self, bool v) { return self.setSelected(v); })
0108             .def("isSelected", [](T const& self) -> bool { return self.isSelected(); });
0109     }
0110 
0111     {
0112         using T = Edge;
0113         py::class_<T>(m, "Edge")
0114             .def("setColor", [](T const& self, Color rgbColor) { return self.setColor(rgbColor); }, py::arg("color"))
0115             .def("setStyle", [](T const& self, EdgeStyle style) { return self.setStyle(style); }, py::arg("style"));
0116     }
0117 
0118     {
0119         using T = ProjectData;
0120         py::class_<T>(m, "ProjectData").def("getSourceCodePath", [](T const& self) { self.getSourceCodePath(); });
0121     }
0122 }
0123 
0124 #include <ct_lvtplg_handlerbindings.inc.cpp>
0125 // clang-format on
0126 
0127 // NOLINTBEGIN
0128 PYMODULE_TYPE(pyLksPlugin, m)
0129 {
0130     m.doc() = "Codevis plugins module.";
0131     exportDataTypes(m);
0132     exportHandlers(m);
0133 }
0134 // NOLINTEND