Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
API.h
Go to the documentation of this file.
1#ifndef CPYRT_API_H
2#define CPYRT_API_H
3
4//
5// Access to the python interpreter and API onto cpyrt.
6//
7
8// Python
9#ifdef _WIN32
10#pragma warning(disable : 4275)
11#pragma warning(disable : 4251)
12#pragma warning(disable : 4800)
13#endif
14#if defined(linux)
15#include <stdio.h>
16#ifdef _POSIX_C_SOURCE
17#undef _POSIX_C_SOURCE
18#endif
19#ifdef _FILE_OFFSET_BITS
20#undef _FILE_OFFSET_BITS
21#endif
22#ifdef _XOPEN_SOURCE
23#undef _XOPEN_SOURCE
24#endif
25#endif
26#include "Python.h"
27
28#define CPYRT_VERSION_HEX 0x011200
29
30// cppjit_interop types
31#ifndef CPYRT_INTERNAL
32
33namespace Cpp {
34struct DeclRef;
35struct TypeRef;
36struct FuncRef;
37struct ObjectRef;
38} // namespace Cpp
39
40namespace cppjit::interop {
41typedef Cpp::DeclRef TCppScope_t;
42typedef Cpp::TypeRef TCppType_t;
43typedef Cpp::ObjectRef TCppObject_t;
44typedef Cpp::FuncRef TCppMethod_t;
45typedef size_t TCppIndex_t;
46typedef void* TCppFuncAddr_t;
47} // namespace cppjit::interop
48#endif
49
50// Bindings
51#include "cpyrt/CommonDefs.h"
52
53// Standard
54#include <string>
55#include <vector>
56
57namespace cppjit::cpyrt {
58
59//- type conversion ---------------------------------------------------------
60
61#ifndef CPYRT_PARAMETER
62#define CPYRT_PARAMETER
63// generic function argument type
64struct Parameter {
65 union Value {
66 bool fBool;
67 int8_t fInt8;
68 int16_t fInt16;
69 int32_t fInt32;
70 uint8_t fUInt8;
71 uint16_t fUInt16;
72 uint32_t fUInt32;
73 short fShort;
74 unsigned short fUShort;
75 int fInt;
76 unsigned int fUInt;
77 long fLong;
78 intptr_t fIntPtr;
79 unsigned long fULong;
80 long long fLLong;
81 unsigned long long fULLong;
82 int64_t fInt64;
83 uint64_t fUInt64;
84 float fFloat;
85 double fDouble;
86 long double fLDouble;
87 void* fVoidp;
89 void* fRef;
91};
92#endif // CPYRT_PARAMETER
93
94// CallContext is not currently exposed
95struct CallContext;
96
97// Dimensions class not currently exposed
98#ifndef CPYRT_DIMENSIONS_H
99#define CPYRT_DIMENSIONS_H
100typedef Py_ssize_t dim_t;
101
102class Dimensions { // Windows note: NOT exported/imported
104
105public:
106 Dimensions(dim_t /*ndim*/ = 0, dim_t* /*dims*/ = nullptr) : fDims(nullptr) {}
107 ~Dimensions() { delete[] fDims; }
108
109public:
110 operator bool() const { return (bool)fDims; }
111};
112
114typedef const dims_t& cdims_t;
115#endif // !CPYRT_DIMENSIONS_H
116
117// type converter base class
119public:
120 virtual ~Converter();
121
122 // convert the python object and add store it on the parameter
123 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr) = 0;
124
125 // convert a C++ object from memory to a Python object
126 virtual PyObject* FromMemory(void* address);
127
128 // convert a Python object to a C++ object and store it on address
129 virtual bool ToMemory(PyObject* value, void* address,
130 PyObject* ctxt = nullptr);
131
132 // if a converter has state, it will be unique per function, shared otherwise
133 virtual bool HasState() { return false; }
134};
135
136// create a converter based on its full type name and dimensions
137CPYRT_EXTERN Converter* CreateConverter(const std::string& name, cdims_t = 0);
139
140// delete a previously created converter
141CPYRT_EXTERN void DestroyConverter(Converter* p);
142
143// register a custom converter
144typedef Converter* (*ConverterFactory_t)(cdims_t);
145CPYRT_EXTERN bool RegisterConverter(const std::string& name,
147
148// remove a custom converter
149CPYRT_EXTERN bool UnregisterConverter(const std::string& name);
150
151// function executor base class
153public:
154 virtual ~Executor();
155
156 // callback when executing a function from Python
158 CallContext*) = 0;
159
160 // if an executor has state, it will be unique per function, shared otherwise
161 virtual bool HasState() { return false; }
162};
163
164// create an executor based on its full type name
165CPYRT_EXTERN Executor* CreateExecutor(const std::string& name, cdims_t = 0);
167
168// delete a previously created executor
169CPYRT_EXTERN void DestroyConverter(Converter* p);
170
171// register a custom executor
172typedef Executor* (*ExecutorFactory_t)(cdims_t);
173CPYRT_EXTERN bool RegisterExecutor(const std::string& name, ExecutorFactory_t);
174
175// remove a custom executor
176CPYRT_EXTERN bool UnregisterExecutor(const std::string& name);
177
178// helper for calling into C++ from a custom executor
180 CallContext*);
181
182//- C++ access to cppjit objects ---------------------------------------------
183
184// Get C++ Instance (python object proxy) name.
185// Sets a TypeError and returns an empty string if the pyobject is not a
186// CPPInstance.
188
189// C++ Instance (python object proxy) to void* conversion
191
192// void* to C++ Instance (python object proxy) conversion, returns a new
193// reference
195 const std::string& classname,
196 bool python_owns = false);
198 interop::TCppScope_t klass_scope,
199 bool python_owns = false);
200// type verifiers for C++ Scope
201CPYRT_EXTERN bool Scope_Check(PyObject* pyobject);
203
204// type verifiers for C++ Instance
205CPYRT_EXTERN bool Instance_Check(PyObject* pyobject);
207
208// memory management: ownership of the underlying C++ object
211
212// type verifier for sequences
213CPYRT_EXTERN bool Sequence_Check(PyObject* pyobject);
214
215// helper to verify expected safety of moving an instance into C++
217
218// type verifiers for C++ Overload
219CPYRT_EXTERN bool Overload_Check(PyObject* pyobject);
221
222// Sets the __reduce__ method for the CPPInstance class, which is by default not
223// implemented by cppjit but might make sense to implement by frameworks that
224// support IO of arbitrary C++ objects, like ROOT.
225CPYRT_EXTERN void Instance_SetReduceMethod(PyCFunction reduceMethod);
226
227//- access to the python interpreter ----------------------------------------
228
229// import a python module, making its classes available to Cling
230CPYRT_EXTERN bool Import(const std::string& name);
231
232// execute a python statement (e.g. "import sys")
233CPYRT_EXTERN bool Exec(const std::string& cmd);
234
235// execute a python stand-alone script, with argv CLI arguments
236CPYRT_EXTERN void ExecScript(const std::string& name,
237 const std::vector<std::string>& args);
238
239// enter an interactive python session (exit with ^D)
240CPYRT_EXTERN void Prompt();
241
242} // namespace cppjit::cpyrt
243
244#endif // !CPYRT_API_H
_object PyObject
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
char name[80]
Definition TGX11.cxx:142
#define CPYRT_CLASS_EXTERN
Definition CommonDefs.h:29
#define CPYRT_EXTERN
Definition CommonDefs.h:28
virtual bool HasState()
Definition API.h:133
virtual bool SetArg(PyObject *, Parameter &, CallContext *=nullptr)=0
Dimensions(dim_t=0, dim_t *=nullptr)
Definition API.h:106
virtual PyObject * Execute(interop::TCppMethod_t, interop::TCppObject_t, CallContext *)=0
virtual bool HasState()
Definition API.h:161
Definition API.h:33
CPYRT_EXTERN std::string Instance_GetScopedFinalName(PyObject *pyobject)
Definition API.cxx:92
CPYRT_EXTERN Converter * CreateConverter(const std::string &name, cdims_t=0)
CPYRT_EXTERN bool RegisterConverter(const std::string &name, ConverterFactory_t)
CPYRT_EXTERN void Instance_SetReduceMethod(PyCFunction reduceMethod)
Definition API.cxx:305
Converter *(* ConverterFactory_t)(cdims_t)
Definition API.h:144
CPYRT_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition API.cxx:178
Py_ssize_t dim_t
Definition API.h:100
CPYRT_EXTERN void DestroyConverter(Converter *p)
CPYRT_EXTERN bool Sequence_Check(PyObject *pyobject)
Definition API.cxx:234
CPYRT_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:447
CPYRT_EXTERN Executor * CreateExecutor(const std::string &name, cdims_t=0)
CPYRT_EXTERN bool UnregisterConverter(const std::string &name)
CPYRT_EXTERN bool Scope_Check(PyObject *pyobject)
Definition API.cxx:168
CPYRT_EXTERN bool Overload_Check(PyObject *pyobject)
Definition API.cxx:283
CPYRT_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:188
Dimensions dims_t
Definition API.h:113
CPYRT_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition API.cxx:367
CPYRT_EXTERN void Prompt()
Definition API.cxx:468
CPYRT_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:105
CPYRT_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition API.cxx:267
CPYRT_EXTERN void Instance_SetCppOwns(PyObject *pyobject)
Definition API.cxx:222
CPYRT_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition API.cxx:294
PyObject * Instance_FromVoidPtr(void *addr, const char *classname, int python_owns)
Definition API.cxx:161
CPYRT_EXTERN void Instance_SetPythonOwns(PyObject *pyobject)
Definition API.cxx:210
CPYRT_EXTERN bool UnregisterExecutor(const std::string &name)
Executor *(* ExecutorFactory_t)(cdims_t)
Definition API.h:172
const dims_t & cdims_t
Definition API.h:114
CPYRT_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition API.cxx:199
CPYRT_EXTERN bool Import(const std::string &name)
Definition API.cxx:310
CPYRT_EXTERN void * CallVoidP(interop::TCppMethod_t, interop::TCppObject_t, CallContext *)
CPYRT_EXTERN bool RegisterExecutor(const std::string &name, ExecutorFactory_t)
Cpp::TypeRef TCppType_t
Definition API.h:42
Cpp::ObjectRef TCppObject_t
Definition API.h:43
Cpp::FuncRef TCppMethod_t
Definition API.h:44
size_t TCppIndex_t
Definition API.h:45
void * TCppFuncAddr_t
Definition API.h:46
Cpp::DeclRef TCppScope_t
Definition API.h:41
union cppjit::cpyrt::Parameter::Value fValue
unsigned short fUShort
Definition API.h:74
unsigned long long fULLong
Definition API.h:81