It was just too hard trying to make it work with separate libs. The main issue was getting the python md and execution modules to recognize PySecurityList and other types defined in the core module.
94 lines
2.5 KiB
C++
94 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include "python_interop/python_activator_impl.h"
|
|
|
|
#include <pybind11/embed.h>
|
|
#include <pybind11/pybind11.h>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace python_interop {
|
|
|
|
class PythonActivator {
|
|
public:
|
|
PythonActivator(std::string target, std::vector<std::string> module_paths)
|
|
: me_(new PythonActivator_Impl(
|
|
std::move(target), std::move(module_paths))) {}
|
|
|
|
PythonActivator() {
|
|
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
|
|
delete me;
|
|
}
|
|
|
|
pybind11::object &get_target_obj() {
|
|
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
|
|
return me->get_target_obj();
|
|
}
|
|
|
|
pybind11::object &get_target_obj(
|
|
std::function<void(pybind11::object &)> init) {
|
|
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
|
|
return me->get_target_obj(init);
|
|
}
|
|
|
|
template <typename... Args>
|
|
pybind11::object &get_target_obj(
|
|
std::function<void(pybind11::object &)> init, Args &&...args) {
|
|
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
|
|
return me->get_target_obj<Args...>(init, std::forward<Args>(args)...);
|
|
}
|
|
|
|
void reset() {
|
|
auto *me = static_cast<PythonActivator_Impl *>(this->me_);
|
|
me->reset();
|
|
}
|
|
|
|
private:
|
|
void *me_;
|
|
};
|
|
|
|
template <typename Target>
|
|
class PythonActivatorT {
|
|
public:
|
|
PythonActivatorT(std::string target, std::vector<std::string> module_paths)
|
|
: activator_(std::move(target), std::move(module_paths)) {}
|
|
|
|
pybind11::object &get_target_obj() { return activator_.get_target_obj(); }
|
|
|
|
Target *get_target() {
|
|
if (target_ == nullptr) {
|
|
target_ = activator_.get_target_obj().cast<Target *>();
|
|
}
|
|
return target_;
|
|
}
|
|
|
|
Target *get_target(std::function<void(Target &)> init) {
|
|
if (target_ == nullptr) {
|
|
activator_.get_target_obj([&](pybind11::object &obj) {
|
|
target_ = obj.cast<Target *>();
|
|
init(*target_);
|
|
});
|
|
}
|
|
return target_;
|
|
}
|
|
|
|
template <typename... Args>
|
|
Target *get_target(std::function<void(Target &)> init, Args &&...args) {
|
|
if (target_ == nullptr) {
|
|
activator_.get_target_obj<Args...>(
|
|
[&](pybind11::object &obj) {
|
|
target_ = obj.cast<Target *>();
|
|
init(*target_);
|
|
},
|
|
std::forward<Args>(args)...);
|
|
}
|
|
return target_;
|
|
}
|
|
|
|
private:
|
|
PythonActivator activator_;
|
|
Target *target_ = nullptr;
|
|
};
|
|
|
|
} // namespace python_interop
|