CVB++ 15.0
Loading...
Searching...
No Matches
decl_context.hpp
1#pragma once
2
3#include "../../string.hpp"
4#include "../../utilities/system_info.hpp"
5
6#include "../py_script.hpp"
7
8#include <memory>
9
10namespace Cvb
11{
12 CVB_BEGIN_INLINE_NS
13
14 template <>
15 inline HandleGuard<PyScript::Context>::HandleGuard(void *handle) noexcept
16 : HandleGuard<PyScript::Context>(handle, [](void *handle) { CVB_CALL_CAPI(ReleaseObject(handle)); })
17 {
18 }
19
20 namespace PyScript
21 {
23
31 class Context final : public std::enable_shared_from_this<Context>
32 {
33
34 struct PrivateTag
35 {
36 };
37
38 public:
40 /*
41 * \return A pointer to the context.
42 *
43 * The context automatically attaches to the interpreter found on the command line.
44 * This may be a system package or pyenv. If a virtual environment is activated,
45 * the context will try to attach to this environment, and falls back to the base python
46 * if there is an error.
47 *
48 * On Linux systems a shared object (libpython3.x.so) must be available.
49 */
51 {
52 static ContextPtr context = std::make_shared<Context>(PrivateTag{});
53 return context;
54 }
55
56 explicit Context(PrivateTag)
57 : handle_(nullptr)
58 {
59 CExports::CVPYSCONTEXT context = nullptr;
60 CVB_CALL_CAPI_CHECKED(CVPYSCreateOrGetContext(
61 *reinterpret_cast<CExports::CVPYSInterpreterVersion *>(&interpreterVersion_), context));
62 handle_ = Cvb::HandleGuard<Context>(context);
63 }
64
65 Context(const Context &other) = delete;
66 Context &operator=(const Context &other) = delete;
67 Context(Context &&other) = delete;
68 Context &operator=(Context &&other) = delete;
69 ~Context() = default;
70
72 /*
73 * \param [in] fileName The file name to use for the evaluated code.
74 * \param [in] code The code to compile and evaluate.
75 * \return A pointer to a code object - usually not needed.
76 *
77 * Function and type objects from the evaluated code are available
78 * as items from this context.
79 */
80 ObjectPtr Eval(const Cvb::String &fileName, const Cvb::String &code);
81
83 /*
84 * \param [in] moduleName The module name.
85 * \return A pointer to a module object .
86 *
87 * Function and type objects from the module code are available
88 * as attributes from returned module object.
89 */
90 ObjectPtr Import(const Cvb::String &moduleName);
91
93 /*
94 * \param [in] itemName The item name.
95 * \return A pointer to an object.
96 *
97 * Use this for function and type objects from evaluated code.
98 */
99 ObjectPtr Item(const Cvb::String &itemName);
100
102 /*
103 * \param [in] path Additional items in the python path (sys.path).
104 *
105 */
106 void AppendToSysPath(const Cvb::String &path)
107 {
108 CVB_CALL_CAPI(CVPYSContextSysPathAppendTyped(Handle(), path.c_str()));
109 }
110
112 /*
113 * \return The interpreter version in use.
114 *
115 */
117 {
118 return interpreterVersion_;
119 }
120
122
127 void *Handle() const noexcept
128 {
129 return handle_.Handle();
130 }
131
132 private:
133 HandleGuard<Context> handle_;
134
135 struct InterpreterVersion interpreterVersion_ = {};
136 };
137 } // namespace PyScript
138
139 CVB_END_INLINE_NS
140} // namespace Cvb
Global python scripting context.
Definition decl_context.hpp:32
struct InterpreterVersion InterpreterVersion() const noexcept
Get the found interpreter version.
Definition decl_context.hpp:116
void AppendToSysPath(const Cvb::String &path)
Adds additional items to the python search path.
Definition decl_context.hpp:106
ObjectPtr Eval(const Cvb::String &fileName, const Cvb::String &code)
Compiles and evaluates python code-.
Definition detail_context.hpp:13
static ContextPtr CreateOrGet()
Creates a context.
Definition decl_context.hpp:50
ObjectPtr Item(const Cvb::String &itemName)
Get an item from this context.
Definition detail_context.hpp:27
ObjectPtr Import(const Cvb::String &moduleName)
Imports a python module.
Definition detail_context.hpp:20
void * Handle() const noexcept
Classic API buffer handle.
Definition decl_context.hpp:127
cvbbool_t ReleaseObject(OBJ &Object)
T make_shared(T... args)
Namespace for the python scripting package.
Definition decl_context.hpp:21
std::shared_ptr< Context > ContextPtr
Convenience shared pointer for Context.
Definition py_script.hpp:123
std::shared_ptr< Object > ObjectPtr
Convenience shared pointer for Object.
Definition py_script.hpp:127
Root namespace for the Image Manager interface.
Definition version.hpp:11
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
Active interpreter version.
Definition py_script.hpp:138