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/PyResult.h"
44#include "CPyCppyy/CommonDefs.h"
45
46// Standard
47#include <string>
48#include <vector>
49
50
51namespace CPyCppyy {
52
53//- type conversion ---------------------------------------------------------
54
55#ifndef CPYCPPYY_PARAMETER
56#define CPYCPPYY_PARAMETER
57// generic function argument type
58struct Parameter {
59 union Value {
60 bool fBool;
61 int8_t fInt8;
62 uint8_t fUInt8;
63 short fShort;
64 unsigned short fUShort;
65 int fInt;
66 unsigned int fUInt;
67 long fLong;
68 intptr_t fIntPtr;
69 unsigned long fULong;
70 long long fLLong;
71 unsigned long long fULLong;
72 int64_t fInt64;
73 uint64_t fUInt64;
74 float fFloat;
75 double fDouble;
76 long double fLDouble;
77 void* fVoidp;
78 } fValue;
79 void* fRef;
80 char fTypeCode;
81};
82#endif // CPYCPPYY_PARAMETER
83
84// CallContext is not currently exposed
85struct CallContext;
86
87// Dimensions class not currently exposed
88#ifndef CPYCPPYY_DIMENSIONS_H
89#define CPYCPPYY_DIMENSIONS_H
91
92class Dimensions { // Windows note: NOT exported/imported
94
95public:
96 Dimensions(dim_t /*ndim*/ = 0, dim_t* /*dims*/ = nullptr) : fDims(nullptr) {}
97 ~Dimensions() { delete [] fDims; }
98
99public:
100 operator bool() const { return (bool)fDims; }
101};
102
104typedef const dims_t& cdims_t;
105#endif // !CPYCPPYY_DIMENSIONS_H
106
107// type converter base class
109public:
110 virtual ~Converter();
111
112// convert the python object and add store it on the parameter
113 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr) = 0;
114
115// convert a C++ object from memory to a Python object
116 virtual PyObject* FromMemory(void* address);
117
118// convert a Python object to a C++ object and store it on address
119 virtual bool ToMemory(PyObject* value, void* address, PyObject* ctxt = nullptr);
120
121// if a converter has state, it will be unique per function, shared otherwise
122 virtual bool HasState() { return false; }
123};
124
125// create a converter based on its full type name and dimensions
126CPYCPPYY_EXTERN Converter* CreateConverter(const std::string& name, cdims_t = 0);
127
128// delete a previously created converter
129CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
130
131// register a custom converter
132typedef Converter* (*ConverterFactory_t)(cdims_t);
134
135// register a custom converter that is a reference to an existing converter
136CPYCPPYY_EXTERN bool RegisterConverterAlias(const std::string& name, const std::string& target);
137
138// remove a custom converter
139CPYCPPYY_EXTERN bool UnregisterConverter(const std::string& name);
140
141
142// function executor base class
144public:
145 virtual ~Executor();
146
147// callback when executing a function from Python
150
151// if an executor has state, it will be unique per function, shared otherwise
152 virtual bool HasState() { return false; }
153};
154
155// create an executor based on its full type name
156CPYCPPYY_EXTERN Executor* CreateExecutor(const std::string& name, cdims_t = 0);
157
158// delete a previously created executor
159CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
160
161// register a custom executor
162typedef Executor* (*ExecutorFactory_t)(cdims_t);
164
165// register a custom executor that is a reference to an existing converter
166CPYCPPYY_EXTERN bool RegisterExecutorAlias(const std::string& name, const std::string& target);
167
168// remove a custom executor
169CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string& name);
170
171// helper for calling into C++ from a custom executor
173
174
175//- C++ access to cppyy objects ---------------------------------------------
176
177// C++ Instance (python object proxy) to void* conversion
179
180// void* to C++ Instance (python object proxy) conversion, returns a new reference
182 void* addr, const std::string& classname, bool python_owns = false);
183
184// type verifiers for C++ Scope
185CPYCPPYY_EXTERN bool Scope_Check(PyObject* pyobject);
187
188// type verifiers for C++ Instance
191
192// type verifier for sequences
194
195// helper to verify expected safety of moving an instance into C++
197
198// type verifiers for C++ Overload
201
202
203//- access to the python interpreter ----------------------------------------
204
205// import a python module, making its classes available to Cling
206CPYCPPYY_EXTERN bool Import(const std::string& name);
207
208// execute a python statement (e.g. "import sys")
209CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
210
211// evaluate a python expression (e.g. "1+1")
212CPYCPPYY_EXTERN const PyResult Eval(const std::string& expr);
213
214// execute a python stand-alone script, with argv CLI arguments
215CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
216
217// enter an interactive python session (exit with ^D)
219
220} // namespace CPyCppyy
221
222#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:110
#define CPYCPPYY_CLASS_EXTERN
Definition CommonDefs.h:29
#define CPYCPPYY_EXTERN
Definition CommonDefs.h:28
virtual bool SetArg(PyObject *, Parameter &, CallContext *=nullptr)=0
virtual bool HasState()
Definition API.h:122
Dimensions(dim_t=0, dim_t *=nullptr)
Definition API.h:96
dim_t * fDims
Definition API.h:93
virtual PyObject * Execute(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)=0
virtual bool HasState()
Definition API.h:152
CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string &name)
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition API.cxx:177
CPYCPPYY_EXTERN void Prompt()
Definition API.cxx:522
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
Definition API.cxx:236
Py_ssize_t dim_t
Definition API.h:90
CPYCPPYY_EXTERN bool RegisterExecutorAlias(const std::string &name, const std::string &target)
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition API.cxx:247
CPYCPPYY_EXTERN bool Import(const std::string &name)
Definition API.cxx:259
Dimensions dims_t
Definition API.h:103
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition API.cxx:318
CPYCPPYY_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition API.cxx:221
CPYCPPYY_EXTERN bool Sequence_Check(PyObject *pyobject)
Definition API.cxx:188
Executor *(* ExecutorFactory_t)(cdims_t)
Definition API.h:162
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:166
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:121
CPYCPPYY_EXTERN bool RegisterExecutor(const std::string &name, ExecutorFactory_t)
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition API.cxx:156
CPYCPPYY_EXTERN Executor * CreateExecutor(const std::string &name, cdims_t=0)
const dims_t & cdims_t
Definition API.h:104
Converter *(* ConverterFactory_t)(cdims_t)
Definition API.h:132
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_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:106
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
Definition API.cxx:146
CPYCPPYY_EXTERN void * CallVoidP(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:446
CPYCPPYY_EXTERN void DestroyConverter(Converter *p)
CPYCPPYY_EXTERN const PyResult Eval(const std::string &expr)
Definition API.cxx:467
CPYCPPYY_EXTERN bool UnregisterConverter(const std::string &name)
size_t TCppIndex_t
Definition cpp_cppyy.h:24
intptr_t TCppMethod_t
Definition cpp_cppyy.h:22
void * TCppObject_t
Definition cpp_cppyy.h:21
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19
void * TCppEnum_t
Definition cpp_cppyy.h:20
size_t TCppScope_t
Definition cpp_cppyy.h:18
void * TCppFuncAddr_t
Definition cpp_cppyy.h:25
union CPyCppyy::Parameter::Value fValue
unsigned long long fULLong
Definition callcontext.h:26