Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-12 09:25:09

0001 #ifndef CPYCPPYY_API_H
0002 #define CPYCPPYY_API_H
0003 
0004 //
0005 // Access to the python interpreter and API onto CPyCppyy.
0006 //
0007 
0008 // Python
0009 #ifdef _WIN32
0010 #pragma warning (disable : 4275)
0011 #pragma warning (disable : 4251)
0012 #pragma warning (disable : 4800)
0013 #endif
0014 #if defined(linux)
0015 #include <stdio.h>
0016 #ifdef _POSIX_C_SOURCE
0017 #undef _POSIX_C_SOURCE
0018 #endif
0019 #ifdef _FILE_OFFSET_BITS
0020 #undef _FILE_OFFSET_BITS
0021 #endif
0022 #ifdef _XOPEN_SOURCE
0023 #undef _XOPEN_SOURCE
0024 #endif
0025 #endif
0026 #include "Python.h"
0027 
0028 #define CPYCPPYY_VERSION_HEX 0x010c10
0029 
0030 // Cppyy types
0031 namespace Cppyy {
0032     typedef size_t      TCppScope_t;
0033     typedef TCppScope_t TCppType_t;
0034     typedef void*       TCppEnum_t;
0035     typedef void*       TCppObject_t;
0036     typedef intptr_t    TCppMethod_t;
0037 
0038     typedef size_t      TCppIndex_t;
0039     typedef void*       TCppFuncAddr_t;
0040 } // namespace Cppyy
0041 
0042 // Bindings
0043 #include "CPyCppyy/CommonDefs.h"
0044 
0045 // Standard
0046 #include <string>
0047 #include <vector>
0048 
0049 
0050 namespace CPyCppyy {
0051 
0052 //- type conversion ---------------------------------------------------------
0053 
0054 #ifndef CPYCPPYY_PARAMETER
0055 #define CPYCPPYY_PARAMETER
0056 // generic function argument type
0057 struct Parameter {
0058     union Value {
0059         bool                 fBool;
0060         int8_t               fInt8;
0061         uint8_t              fUInt8;
0062         short                fShort;
0063         unsigned short       fUShort;
0064         int                  fInt;
0065         unsigned int         fUInt;
0066         long                 fLong;
0067         intptr_t             fIntPtr;
0068         unsigned long        fULong;
0069         long long            fLLong;
0070         unsigned long long   fULLong;
0071         int64_t              fInt64;
0072         uint64_t             fUInt64;
0073         float                fFloat;
0074         double               fDouble;
0075         long double          fLDouble;
0076         void*                fVoidp;
0077     } fValue;
0078     void* fRef;
0079     char  fTypeCode;
0080 };
0081 #endif // CPYCPPYY_PARAMETER
0082 
0083 // CallContext is not currently exposed
0084 struct CallContext;
0085 
0086 // Dimensions class not currently exposed
0087 #ifndef CPYCPPYY_DIMENSIONS_H
0088 #define CPYCPPYY_DIMENSIONS_H
0089 typedef Py_ssize_t dim_t;
0090 
0091 class Dimensions {      // Windows note: NOT exported/imported
0092     dim_t* fDims;
0093 
0094 public:
0095     Dimensions(dim_t /*ndim*/ = 0, dim_t* /*dims*/ = nullptr) : fDims(nullptr) {}
0096     ~Dimensions() { delete [] fDims; }
0097 
0098 public:
0099     operator bool() const { return (bool)fDims; }
0100 };
0101 
0102 typedef Dimensions dims_t;
0103 typedef const dims_t& cdims_t;
0104 #endif // !CPYCPPYY_DIMENSIONS_H
0105 
0106 // type converter base class
0107 class CPYCPPYY_CLASS_EXTERN Converter {
0108 public:
0109     virtual ~Converter();
0110 
0111 // convert the python object and add store it on the parameter
0112     virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr) = 0;
0113 
0114 // convert a C++ object from memory to a Python object
0115     virtual PyObject* FromMemory(void* address);
0116 
0117 // convert a Python object to a C++ object and store it on address
0118     virtual bool ToMemory(PyObject* value, void* address, PyObject* ctxt = nullptr);
0119 
0120 // if a converter has state, it will be unique per function, shared otherwise
0121     virtual bool HasState() { return false; }
0122 };
0123 
0124 // create a converter based on its full type name and dimensions
0125 CPYCPPYY_EXTERN Converter* CreateConverter(const std::string& name, cdims_t = 0);
0126 
0127 // delete a previously created converter
0128 CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
0129 
0130 // register a custom converter
0131 typedef Converter* (*ConverterFactory_t)(cdims_t);
0132 CPYCPPYY_EXTERN bool RegisterConverter(const std::string& name, ConverterFactory_t);
0133 
0134 // register a custom converter that is a reference to an existing converter
0135 CPYCPPYY_EXTERN bool RegisterConverterAlias(const std::string& name, const std::string& target);
0136 
0137 // remove a custom converter
0138 CPYCPPYY_EXTERN bool UnregisterConverter(const std::string& name);
0139 
0140 
0141 // function executor base class
0142 class CPYCPPYY_CLASS_EXTERN Executor {
0143 public:
0144     virtual ~Executor();
0145 
0146 // callback when executing a function from Python
0147     virtual PyObject* Execute(
0148         Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext*) = 0;
0149 
0150 // if an executor has state, it will be unique per function, shared otherwise
0151     virtual bool HasState() { return false; }
0152 };
0153 
0154 // create an executor based on its full type name
0155 CPYCPPYY_EXTERN Executor* CreateExecutor(const std::string& name, cdims_t = 0);
0156 
0157 // delete a previously created executor
0158 CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
0159 
0160 // register a custom executor
0161 typedef Executor* (*ExecutorFactory_t)(cdims_t);
0162 CPYCPPYY_EXTERN bool RegisterExecutor(const std::string& name, ExecutorFactory_t);
0163 
0164 // register a custom executor that is a reference to an existing converter
0165 CPYCPPYY_EXTERN bool RegisterExecutorAlias(const std::string& name, const std::string& target);
0166 
0167 // remove a custom executor
0168 CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string& name);
0169 
0170 // helper for calling into C++ from a custom executor
0171 CPYCPPYY_EXTERN void* CallVoidP(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext*);
0172 
0173 
0174 //- C++ access to cppyy objects ---------------------------------------------
0175 
0176 // Get C++ Instance (python object proxy) name.
0177 // Sets a TypeError and returns an empty string if the pyobject is not a CPPInstance.
0178 CPYCPPYY_EXTERN std::string Instance_GetScopedFinalName(PyObject* pyobject);
0179 
0180 // C++ Instance (python object proxy) to void* conversion
0181 CPYCPPYY_EXTERN void* Instance_AsVoidPtr(PyObject* pyobject);
0182 
0183 // void* to C++ Instance (python object proxy) conversion, returns a new reference
0184 CPYCPPYY_EXTERN PyObject* Instance_FromVoidPtr(
0185     void* addr, const std::string& classname, bool python_owns = false);
0186 
0187 // type verifiers for C++ Scope
0188 CPYCPPYY_EXTERN bool Scope_Check(PyObject* pyobject);
0189 CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject* pyobject);
0190 
0191 // type verifiers for C++ Instance
0192 CPYCPPYY_EXTERN bool Instance_Check(PyObject* pyobject);
0193 CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject* pyobject);
0194 
0195 // memory management: ownership of the underlying C++ object
0196 CPYCPPYY_EXTERN void Instance_SetPythonOwns(PyObject* pyobject);
0197 CPYCPPYY_EXTERN void Instance_SetCppOwns(PyObject* pyobject);
0198 
0199 // type verifier for sequences
0200 CPYCPPYY_EXTERN bool Sequence_Check(PyObject* pyobject);
0201 
0202 // helper to verify expected safety of moving an instance into C++
0203 CPYCPPYY_EXTERN bool Instance_IsLively(PyObject* pyobject);
0204 
0205 // type verifiers for C++ Overload
0206 CPYCPPYY_EXTERN bool Overload_Check(PyObject* pyobject);
0207 CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject* pyobject);
0208 
0209 // Sets the __reduce__ method for the CPPInstance class, which is by default not
0210 // implemented by cppyy but might make sense to implement by frameworks that
0211 // support IO of arbitrary C++ objects, like ROOT.
0212 CPYCPPYY_EXTERN void Instance_SetReduceMethod(PyCFunction reduceMethod);
0213 
0214 
0215 //- access to the python interpreter ----------------------------------------
0216 
0217 // import a python module, making its classes available to Cling
0218 CPYCPPYY_EXTERN bool Import(const std::string& name);
0219 
0220 // execute a python statement (e.g. "import sys")
0221 CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
0222 
0223 // execute a python stand-alone script, with argv CLI arguments
0224 CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
0225 
0226 // enter an interactive python session (exit with ^D)
0227 CPYCPPYY_EXTERN void Prompt();
0228 
0229 } // namespace CPyCppyy
0230 
0231 #endif // !CPYCPPYY_API_H