Logo ROOT  
Reference Guide
API.h
Go to the documentation of this file.
1#ifndef CPYCPPYY_TPYTHON
2#define CPYCPPYY_TPYTHON
3
4//////////////////////////////////////////////////////////////////////////////
5// //
6// TPython //
7// //
8// Access to the python interpreter and API onto CPyCppyy. //
9// //
10//////////////////////////////////////////////////////////////////////////////
11
12// Python
13#ifdef _WIN32
14#pragma warning (disable : 4275)
15#pragma warning (disable : 4251)
16#pragma warning (disable : 4800)
17#endif
18#if defined(linux)
19#include <stdio.h>
20#ifdef _POSIX_C_SOURCE
21#undef _POSIX_C_SOURCE
22#endif
23#ifdef _FILE_OFFSET_BITS
24#undef _FILE_OFFSET_BITS
25#endif
26#ifdef _XOPEN_SOURCE
27#undef _XOPEN_SOURCE
28#endif
29#endif
30#include "Python.h"
31
32// Cppyy types
33namespace Cppyy {
34 typedef size_t TCppScope_t;
35 typedef TCppScope_t TCppType_t;
36 typedef void* TCppEnum_t;
37 typedef void* TCppObject_t;
38 typedef intptr_t TCppMethod_t;
39
40 typedef size_t TCppIndex_t;
41 typedef void* TCppFuncAddr_t;
42} // namespace Cppyy
43
44// Bindings
45#include "CPyCppyy/PyResult.h"
46#include "CPyCppyy/CommonDefs.h"
47
48// Standard
49#include <string>
50#include <vector>
51
52
53namespace CPyCppyy {
54
55//- type conversion ---------------------------------------------------------
56
57#ifndef CPYCPPYY_PARAMETER
58#define CPYCPPYY_PARAMETER
59// generic function argument type
60struct Parameter {
61 union Value {
62 bool fBool;
63 int8_t fInt8;
65 short fShort;
66 unsigned short fUShort;
67 int fInt;
68 unsigned int fUInt;
69 long fLong;
70 intptr_t fIntPtr;
71 unsigned long fULong;
72 long long fLLong;
73 unsigned long long fULLong;
74 int64_t fInt64;
75 uint64_t fUInt64;
76 float fFloat;
77 double fDouble;
78 long double fLDouble;
79 void* fVoidp;
80 } fValue;
81 void* fRef;
82 char fTypeCode;
83};
84#endif // CPYCPPYY_PARAMETER
85
86// CallContext is not currently exposed
87struct CallContext;
88
89
90// type converter base class
92public:
93 virtual ~Converter();
94
95// convert the python object and add store it on the parameter
96 virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr) = 0;
97
98// convert a C++ object from memory to a Python object
99 virtual PyObject* FromMemory(void* address);
100
101// convert a Python object to a C++ object and store it on address
102 virtual bool ToMemory(PyObject* value, void* address);
103
104// if a converter has state, it will be unique per function, shared otherwise
105 virtual bool HasState() { return false; }
106};
107
108// create a converter based on its full type name and dimensions
109CPYCPPYY_EXTERN Converter* CreateConverter(const std::string& name, Py_ssize_t* dims = nullptr);
110
111// delete a previously created converter
113
114// register a custom converter
115typedef Converter* (*ConverterFactory_t)(Py_ssize_t* dims);
117
118// remove a custom converter
119CPYCPPYY_EXTERN bool UnregisterConverter(const std::string& name);
120
121
122// function executor base class
124public:
125 virtual ~Executor();
126
127// callback when executing a function from Python
130
131// if an executor has state, it will be unique per function, shared otherwise
132 virtual bool HasState() { return false; }
133};
134
135// create an executor based on its full type name
136CPYCPPYY_EXTERN Executor* CreateExecutor(const std::string& name);
137
138// delete a previously created executor
139CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
140
141// register a custom executor
142typedef Executor* (*ExecutorFactory_t)();
144
145// remove a custom executor
146CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string& name);
147
148// helper for calling into C++ from a custom executor
150
151
152//- C++ access to cppyy objects ---------------------------------------------
153
154// C++ Instance (python object proxy) to void* conversion
156
157// void* to C++ Instance (python object proxy) conversion, returns a new reference
159 void* addr, const std::string& classname, bool python_owns = false);
160
161// type verifiers for C++ Scope
162CPYCPPYY_EXTERN bool Scope_Check(PyObject* pyobject);
164
165// type verifiers for C++ Instance
168
169// helper to verify expected safety of moving an instance into C++
171
172// type verifiers for C++ Overload
175
176
177//- access to the python interpreter ----------------------------------------
178
179// import a python module, making its classes available to Cling
180CPYCPPYY_EXTERN bool Import(const std::string& name);
181
182// execute a python statement (e.g. "import sys")
183CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
184
185// evaluate a python expression (e.g. "1+1")
186CPYCPPYY_EXTERN const PyResult Eval(const std::string& expr);
187
188// execute a python stand-alone script, with argv CLI arguments
189CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
190
191// enter an interactive python session (exit with ^D)
193
194} // namespace CPyCppyy
195
196#endif // !CPYCPPYY_API_H
int Py_ssize_t
Definition: CPyCppyy.h:228
uint8_t
Definition: Converters.cxx:858
_object PyObject
Definition: PyMethodBase.h:41
char name[80]
Definition: TGX11.cxx:109
#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:105
virtual PyObject * Execute(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)=0
virtual bool HasState()
Definition: API.h:132
CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string &name)
Definition: Executors.cxx:858
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition: API.cxx:157
CPYCPPYY_EXTERN void Prompt()
Definition: API.cxx:407
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
Definition: API.cxx:183
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition: API.cxx:194
CPYCPPYY_EXTERN bool Import(const std::string &name)
Definition: API.cxx:206
Executor *(* ExecutorFactory_t)()
Definition: API.h:142
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition: API.cxx:265
CPYCPPYY_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition: API.cxx:168
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition: API.cxx:146
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:107
CPYCPPYY_EXTERN bool RegisterExecutor(const std::string &name, ExecutorFactory_t)
Definition: Executors.cxx:845
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition: API.cxx:136
CPYCPPYY_EXTERN Executor * CreateExecutor(const std::string &name)
Definition: Executors.cxx:748
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition: API.cxx:92
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
Definition: API.cxx:126
CPYCPPYY_EXTERN void * CallVoidP(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)
Definition: Executors.cxx:871
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition: API.cxx:331
CPYCPPYY_EXTERN void DestroyConverter(Converter *p)
Converter *(* ConverterFactory_t)(Py_ssize_t *dims)
Definition: API.h:115
CPYCPPYY_EXTERN const PyResult Eval(const std::string &expr)
Definition: API.cxx:352
CPYCPPYY_EXTERN bool UnregisterConverter(const std::string &name)
CPYCPPYY_EXTERN Converter * CreateConverter(const std::string &name, Py_ssize_t *dims=nullptr)
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
unsigned short fUShort
Definition: callcontext.h:19