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// remove a custom converter
136CPYCPPYY_EXTERN bool UnregisterConverter(const std::string& name);
137
138
139// function executor base class
141public:
142 virtual ~Executor();
143
144// callback when executing a function from Python
147
148// if an executor has state, it will be unique per function, shared otherwise
149 virtual bool HasState() { return false; }
150};
151
152// create an executor based on its full type name
153CPYCPPYY_EXTERN Executor* CreateExecutor(const std::string& name, cdims_t = 0);
154
155// delete a previously created executor
156CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
157
158// register a custom executor
159typedef Executor* (*ExecutorFactory_t)(cdims_t);
161
162// remove a custom executor
163CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string& name);
164
165// helper for calling into C++ from a custom executor
167
168
169//- C++ access to cppyy objects ---------------------------------------------
170
171// C++ Instance (python object proxy) to void* conversion
173
174// void* to C++ Instance (python object proxy) conversion, returns a new reference
176 void* addr, const std::string& classname, bool python_owns = false);
177
178// type verifiers for C++ Scope
179CPYCPPYY_EXTERN bool Scope_Check(PyObject* pyobject);
181
182// type verifiers for C++ Instance
185
186// helper to verify expected safety of moving an instance into C++
188
189// type verifiers for C++ Overload
192
193
194//- access to the python interpreter ----------------------------------------
195
196// import a python module, making its classes available to Cling
197CPYCPPYY_EXTERN bool Import(const std::string& name);
198
199// execute a python statement (e.g. "import sys")
200CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
201
202// evaluate a python expression (e.g. "1+1")
203CPYCPPYY_EXTERN const PyResult Eval(const std::string& expr);
204
205// execute a python stand-alone script, with argv CLI arguments
206CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
207
208// enter an interactive python session (exit with ^D)
210
211} // namespace CPyCppyy
212
213#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 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:149
CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string &name)
CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject *pyobject)
Definition API.cxx:174
CPYCPPYY_EXTERN void Prompt()
Definition API.cxx:424
CPYCPPYY_EXTERN bool Overload_Check(PyObject *pyobject)
Definition API.cxx:200
Py_ssize_t dim_t
Definition API.h:90
CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject *pyobject)
Definition API.cxx:211
CPYCPPYY_EXTERN bool Import(const std::string &name)
Definition API.cxx:223
Dimensions dims_t
Definition API.h:103
CPYCPPYY_EXTERN void ExecScript(const std::string &name, const std::vector< std::string > &args)
Definition API.cxx:282
CPYCPPYY_EXTERN bool Instance_IsLively(PyObject *pyobject)
Definition API.cxx:185
Executor *(* ExecutorFactory_t)(cdims_t)
Definition API.h:159
CPYCPPYY_EXTERN bool Instance_Check(PyObject *pyobject)
Definition API.cxx:163
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:118
CPYCPPYY_EXTERN bool RegisterExecutor(const std::string &name, ExecutorFactory_t)
CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject *pyobject)
Definition API.cxx:153
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 Converter * CreateConverter(const std::string &name, cdims_t=0)
CPYCPPYY_EXTERN void * Instance_AsVoidPtr(PyObject *pyobject)
Definition API.cxx:103
CPYCPPYY_EXTERN bool Scope_Check(PyObject *pyobject)
Definition API.cxx:143
CPYCPPYY_EXTERN void * CallVoidP(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext *)
CPYCPPYY_EXTERN bool Exec(const std::string &cmd)
Definition API.cxx:348
CPYCPPYY_EXTERN void DestroyConverter(Converter *p)
CPYCPPYY_EXTERN const PyResult Eval(const std::string &expr)
Definition API.cxx:369
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