RayZaler 0.1
The free opto-mechanical simulation framework
ScriptLoader.h
1//
2// Copyright (c) 2024 Gonzalo José Carracedo Carballal
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU Lesser General Public License as
6// published by the Free Software Foundation, either version 3 of the
7// License, or (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful, but
10// WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU Lesser General Public License for more details.
13//
14// You should have received a copy of the GNU Lesser General Public
15// License along with this program. If not, see
16// <http://www.gnu.org/licenses/>
17//
18
19#ifndef _SCRIPT_LOADER_H
20#define _SCRIPT_LOADER_H
21
22#define PY_SSIZE_T_CLEAN
23#include <Python.h>
24#include <string>
25#include <list>
26#include <map>
27#include "GenericCompositeModel.h"
28
29namespace RZ {
31 using GenericCustomFunction::GenericCustomFunction;
32 PyObject *pFunc = nullptr;
33
34 virtual Real evaluate(Real const *args, unsigned argc) override;
35 };
36
37 class ScriptLoader;
38
39 class Script {
40 std::string m_path;
41 PyObject *m_pModule = nullptr;
42
43 std::list<ScriptFunction> m_functions;
44 std::map<std::string, ScriptFunction *> m_nameToFunction;
45
46 Script(std::string const &);
47 ~Script();
48
49 bool load(std::string const &moduleName);
50
51 public:
52 friend class ScriptLoader;
53 bool registerFunction(ScriptFunction const &);
54 std::list<ScriptFunction> &customFunctions() const;
55 };
56
58 PyObject *m_pSys = nullptr;
59 PyObject *m_pSysPath = nullptr;
60 pthread_mutex_t m_registerMutex;
61 Script *m_currScript = nullptr;
62 std::map<std::string, Script *> m_pathToScript;
63 static ScriptLoader *m_instance;
64
65 bool enableScriptDirectory(std::string const &);
66 bool explodeScriptPath(
67 std::string const &,
68 std::string &,
69 std::string &);
70
71 bool pythonLibraryInit();
72
75
76 public:
77 static ScriptLoader *instance();
78 Script *load(std::string const &path);
79 Script *getCurrentScript();
80 };
81}
82
83#endif // _SCRIPT_LOADER_H
Definition: ScriptLoader.h:39
Definition: ScriptLoader.h:57
Definition: GenericCompositeModel.h:58
Definition: ScriptLoader.h:30