Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
API.h
Go to the documentation of this file.
1#ifndef CPYCPPYY_API_H
2#define CPYCPPYY_API_H
3
4//
5// Access to the python interpreter and API onto CPyCppyy.
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 CPYCPPYY_VERSION_HEX 0x010c10
29
30// Cppyy types
31namespace Cppyy {
32 typedef size_t TCppScope_t;
33 typedef TCppScope_t TCppType_t;
34 typedef void* TCppEnum_t;
35 typedef void* TCppObject_t;
36 typedef intptr_t TCppMethod_t;
37
38 typedef size_t TCppIndex_t;
39 typedef void* TCppFuncAddr_t;
40} // namespace Cppyy
41
42// Bindings
43#include "CPyCppyy/CommonDefs.h"
44
45// Standard
46#include <string>
47#include <vector>
48
49
50namespace CPyCppyy {
51
52//- type conversion ---------------------------------------------------------
53
54#ifndef CPYCPPYY_PARAMETER
55#define CPYCPPYY_PARAMETER
56// generic function argument type
57struct Parameter {
58 union Value {
59 bool fBool;
60 int8_t fInt8;
61 uint8_t fUInt8;
62 short fShort;
63 unsigned short fUShort;
64 int fInt;
65 unsigned int fUInt;
66 long fLong;
67 intptr_t fIntPtr;
68 unsigned long fULong;
69 long long fLLong;
70 unsigned long long fULLong;
71 int64_t fInt64;
72 uint64_t fUInt64;
73 float fFloat;
74 double fDouble;
75 long double fLDouble;
76 void* fVoidp;
77 } fValue;
78 void* fRef;
79 char fTypeCode;
80};
81#endif // CPYCPPYY_PARAMETER
82
83// CallContext is not currently exposed
84struct CallContext;
85
86// Dimensions class not currently exposed
87#ifndef CPYCPPYY_DIMENSIONS_H
88#define CPYCPPYY_DIMENSIONS_H
90
91class Dimensions { // Windows note: NOT exported/imported
93
94public:
95 Dimensions(dim_t /*ndim*/ = 0, dim_t* /*dims*/ = nullptr) : fDims(nullptr) {}
96 ~Dimensions() { delete [] fDims; }
97
98public:
99 operator bool() const { return (bool)fDims; }
100};
101
103typedef const dims_t& cdims_t;
104#endif // !CPYCPPYY_DIMENSIONS_H
105
106// type converter base class
108public:
109 virtual ~Converter();
110
111// convert the python object and add store it on the parameter
112 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr) = 0;
113
114// convert a C++ object from memory to a Python object
115 virtual PyObject* FromMemory(void* address);
116
117// convert a Python object to a C++ object and store it on address
118 virtual bool ToMemory(PyObject* value, void* address, PyObject* ctxt = nullptr);
119
120// if a converter has state, it will be unique per function, shared otherwise
121 virtual bool HasState() { return false; }
122};
123
124// create a converter based on its full type name and dimensions
125CPYCPPYY_EXTERN Converter* CreateConverter(const std::string& name, cdims_t = 0);
126
127// delete a previously created converter
128CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
129
130// register a custom converter
131typedef Converter* (*ConverterFactory_t)(cdims_t);
133
134// register a custom converter that is a reference to an existing converter
135CPYCPPYY_EXTERN bool RegisterConverterAlias(const std::string& name, const std::string& target);
136
137// remove a custom converter
138CPYCPPYY_EXTERN bool UnregisterConverter(const std::string& name);
139
140
141// function executor base class
143public:
144 virtual ~Executor();
145
146// callback when executing a function from Python
149
150// if an executor has state, it will be unique per function, shared otherwise
151 virtual bool HasState() { return false; }
152};
153
154// create an executor based on its full type name
155CPYCPPYY_EXTERN Executor* CreateExecutor(const std::string& name, cdims_t = 0);
156
157// delete a previously created executor
158CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
159
160// register a custom executor
161typedef Executor* (*ExecutorFactory_t)(cdims_t);
163
164// register a custom executor that is a reference to an existing converter
165CPYCPPYY_EXTERN bool RegisterExecutorAlias(const std::string& name, const std::string& target);
166
167// remove a custom executor
168CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string& name);
169
170// helper for calling into C++ from a custom executor
172
173
174//- C++ access to cppyy objects ---------------------------------------------
175
176// Get C++ Instance (python object proxy) name.
177// Sets a TypeError and returns an empty string if the pyobject is not a CPPInstance.
179
180// C++ Instance (python object proxy) to void* conversion
182
183// void* to C++ Instance (python object proxy) conversion, returns a new reference
185 void* addr, const std::string& classname, bool python_owns = false);
186
187// type verifiers for C++ Scope
190
191// type verifiers for C++ Instance
194
195// memory management: ownership of the underlying C++ object
198
199// type verifier for sequences
201
202// helper to verify expected safety of moving an instance into C++
204
205// type verifiers for C++ Overload
208
209// Sets the __reduce__ method for the CPPInstance class, which is by default not
210// implemented by cppyy but might make sense to implement by frameworks that
211// support IO of arbitrary C++ objects, like ROOT.
213
214
215//- access to the python interpreter ----------------------------------------
216
217// import a python module, making its classes available to Cling
218CPYCPPYY_EXTERN bool Import(const std::string& name);
219
220// execute a python statement (e.g. "import sys")
221CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
222
223// execute a python stand-alone script, with argv CLI arguments
224CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
225
226// enter an interactive python session (exit with ^D)
228
229} // namespace CPyCppyy
230
231#endif // !CPYCPPYY_API_H
int Py_ssize_t
Definition CPyCppyy.h:215
_object PyObject
winID h TVirtualViewer3D TVirtualGLPainter p
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 target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
Definition TGX11.cxx:145
#define CPYCPPYY_CLASS_EXTERN
Definition CommonDefs.h:27
#define CPYCPPYY_EXTERN
Definition CommonDefs.h:26
virtual bool SetArg(PyObject *, Parameter &, CallContext *=nullptr)=0
virtual bool HasState()
Definition API.h:121
Dimensions(dim_t=0, dim_t *=nullptr)
Definition API.h:95
dim_t * fDims
Definition API.h:92
virtual PyObject * Execute(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)=0
virtual bool HasState()
Definition API.h:151
CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string &name)
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition API.cxx:189
CPYCPPYY_EXTERN void Prompt()
Definition API.cxx:456
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
Definition API.cxx:274
Py_ssize_t dim_t
Definition API.h:89
CPYCPPYY_EXTERN bool RegisterExecutorAlias(const std::string &name, const std::string &target)
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition API.cxx:285
CPYCPPYY_EXTERN bool Import(const std::string &name)
Definition API.cxx:302
Dimensions dims_t
Definition API.h:102
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition API.cxx:357
CPYCPPYY_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition API.cxx:259
CPYCPPYY_EXTERN bool Sequence_Check(PyObject *pyobject)
Definition API.cxx:226
Executor *(* ExecutorFactory_t)(cdims_t)
Definition API.h:161
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:178
CPYCPPYY_EXTERN bool RegisterConverter(const std::string &name, ConverterFactory_t)
CPYCPPYY_EXTERN PyObject * Instance_FromVoidPtr(void *addr, const std::string &classname, bool python_owns=false)
Definition API.cxx:133
CPYCPPYY_EXTERN bool RegisterExecutor(const std::string &name, ExecutorFactory_t)
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition API.cxx:168
CPYCPPYY_EXTERN Executor * CreateExecutor(const std::string &name, cdims_t=0)
const dims_t & cdims_t
Definition API.h:103
Converter *(* ConverterFactory_t)(cdims_t)
Definition API.h:131
CPYCPPYY_EXTERN void Instance_SetCppOwns(PyObject *pyobject)
Definition API.cxx:213
CPYCPPYY_EXTERN void Instance_SetPythonOwns(PyObject *pyobject)
Definition API.cxx:200
CPYCPPYY_EXTERN bool RegisterConverterAlias(const std::string &name, const std::string &target)
CPYCPPYY_EXTERN Converter * CreateConverter(const std::string &name, cdims_t=0)
CPYCPPYY_EXTERN void Instance_SetReduceMethod(PyCFunction reduceMethod)
Definition API.cxx:296
CPYCPPYY_EXTERN std::string Instance_GetScopedFinalName(PyObject *pyobject)
Definition API.cxx:106
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:118
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
Definition API.cxx:158
CPYCPPYY_EXTERN void * CallVoidP(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:435
CPYCPPYY_EXTERN void DestroyConverter(Converter *p)
CPYCPPYY_EXTERN bool UnregisterConverter(const std::string &name)
size_t TCppIndex_t
Definition cpp_cppyy.h:40
intptr_t TCppMethod_t
Definition cpp_cppyy.h:38
void * TCppObject_t
Definition cpp_cppyy.h:37
TCppScope_t TCppType_t
Definition cpp_cppyy.h:35
void * TCppEnum_t
Definition cpp_cppyy.h:36
size_t TCppScope_t
Definition cpp_cppyy.h:34
void * TCppFuncAddr_t
Definition cpp_cppyy.h:41
union CPyCppyy::Parameter::Value fValue
unsigned long long fULLong
Definition callcontext.h:29