MeshLib C++ Docs
Loading...
Searching...
No Matches
MREmbeddedPython.h
Go to the documentation of this file.
1#pragma once
2
3#include "exports.h"
4
5#include <atomic>
6#include <condition_variable>
7#include <filesystem>
8#include <functional>
9#include <mutex>
10#include <string>
11#include <thread>
12#include <vector>
13
14namespace MR
15{
18
19
20class MREMBEDDEDPYTHON_CLASS EmbeddedPython
21{
22public:
23 struct Config
24 {
25 bool siteImport{ true };
26 std::string home;
27 std::vector<std::string> argv;
28 };
29 MREMBEDDEDPYTHON_API static Config pythonConfig;
30
31 MREMBEDDEDPYTHON_API static bool isAvailable();
32
35 MREMBEDDEDPYTHON_API static void shutdown();
36
39 [[nodiscard]] static bool nowRunning() { return instance_().state_.load() != State::idle; }
40
44 MREMBEDDEDPYTHON_API static bool runString( std::string pythonString, std::function<void( bool success )> onDoneAsync = nullptr );
45
46 MREMBEDDEDPYTHON_API static bool runScript( const std::filesystem::path& path );
47
48 MREMBEDDEDPYTHON_API static bool isPythonScript( const std::filesystem::path& path );
49private:
51 EmbeddedPython( const EmbeddedPython& ) = delete;
52 EmbeddedPython& operator=( const EmbeddedPython& ) = delete;
54
55 bool init_();
56 void ensureInterpreterThreadIsRunning_();
57
58 MREMBEDDEDPYTHON_API static EmbeddedPython& instance_();
59 bool available_ = false;
60 bool shutdownCalled_ = false;
61
62 enum class State
63 {
64 idle,
65 running,
66 finishing,
67 };
68
69 std::atomic<State> state_ = State::idle;
70 std::string queuedSource_;
71 bool lastRunSuccessful_ = false;
72 std::function<void( bool success )> onDoneAsync_;
73 std::mutex cvMutex_;
74 std::condition_variable cv_;
75
81
82 std::thread interpreterThread_;
83
84 bool stopInterpreterThread_ = false;
85};
86
87}
Definition MREmbeddedPython.h:21
std::string home
Definition MREmbeddedPython.h:26
static MREMBEDDEDPYTHON_API Config pythonConfig
Definition MREmbeddedPython.h:29
static MREMBEDDEDPYTHON_API bool runScript(const std::filesystem::path &path)
std::vector< std::string > argv
Definition MREmbeddedPython.h:27
static MREMBEDDEDPYTHON_API bool isPythonScript(const std::filesystem::path &path)
static MREMBEDDEDPYTHON_API bool runString(std::string pythonString, std::function< void(bool success)> onDoneAsync=nullptr)
static MREMBEDDEDPYTHON_API void shutdown()
static bool nowRunning()
Definition MREmbeddedPython.h:39
static MREMBEDDEDPYTHON_API bool isAvailable()
Set this once before running anything.
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
Definition MREmbeddedPython.h:24