Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:21:53

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/PyResult.h"
0044 #include "CPyCppyy/CommonDefs.h"
0045 
0046 // Standard
0047 #include <string>
0048 #include <vector>
0049 
0050 
0051 namespace CPyCppyy {
0052 
0053 //- type conversion ---------------------------------------------------------
0054 
0055 #ifndef CPYCPPYY_PARAMETER
0056 #define CPYCPPYY_PARAMETER
0057 // generic function argument type
0058 struct Parameter {
0059     union Value {
0060         bool                 fBool;
0061         int8_t               fInt8;
0062         uint8_t              fUInt8;
0063         short                fShort;
0064         unsigned short       fUShort;
0065         int                  fInt;
0066         unsigned int         fUInt;
0067         long                 fLong;
0068         intptr_t             fIntPtr;
0069         unsigned long        fULong;
0070         long long            fLLong;
0071         unsigned long long   fULLong;
0072         int64_t              fInt64;
0073         uint64_t             fUInt64;
0074         float                fFloat;
0075         double               fDouble;
0076         long double          fLDouble;
0077         void*                fVoidp;
0078     } fValue;
0079     void* fRef;
0080     char  fTypeCode;
0081 };
0082 #endif // CPYCPPYY_PARAMETER
0083 
0084 // CallContext is not currently exposed
0085 struct CallContext;
0086 
0087 // Dimensions class not currently exposed
0088 #ifndef CPYCPPYY_DIMENSIONS_H
0089 #define CPYCPPYY_DIMENSIONS_H
0090 typedef Py_ssize_t dim_t;
0091 
0092 class Dimensions {      // Windows note: NOT exported/imported
0093     dim_t* fDims;
0094 
0095 public:
0096     Dimensions(dim_t /*ndim*/ = 0, dim_t* /*dims*/ = nullptr) : fDims(nullptr) {}
0097     ~Dimensions() { delete [] fDims; }
0098 
0099 public:
0100     operator bool() const { return (bool)fDims; }
0101 };
0102 
0103 typedef Dimensions dims_t;
0104 typedef const dims_t& cdims_t;
0105 #endif // !CPYCPPYY_DIMENSIONS_H
0106 
0107 // type converter base class
0108 class CPYCPPYY_CLASS_EXTERN Converter {
0109 public:
0110     virtual ~Converter();
0111 
0112 // convert the python object and add store it on the parameter
0113     virtual bool SetArg(PyObject*, Parameter&, CallContext* = nullptr) = 0;
0114 
0115 // convert a C++ object from memory to a Python object
0116     virtual PyObject* FromMemory(void* address);
0117 
0118 // convert a Python object to a C++ object and store it on address
0119     virtual bool ToMemory(PyObject* value, void* address, PyObject* ctxt = nullptr);
0120 
0121 // if a converter has state, it will be unique per function, shared otherwise
0122     virtual bool HasState() { return false; }
0123 };
0124 
0125 // create a converter based on its full type name and dimensions
0126 CPYCPPYY_EXTERN Converter* CreateConverter(const std::string& name, cdims_t = 0);
0127 
0128 // delete a previously created converter
0129 CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
0130 
0131 // register a custom converter
0132 typedef Converter* (*ConverterFactory_t)(cdims_t);
0133 CPYCPPYY_EXTERN bool RegisterConverter(const std::string& name, ConverterFactory_t);
0134 
0135 // register a custom converter that is a reference to an existing converter
0136 CPYCPPYY_EXTERN bool RegisterConverterAlias(const std::string& name, const std::string& target);
0137 
0138 // remove a custom converter
0139 CPYCPPYY_EXTERN bool UnregisterConverter(const std::string& name);
0140 
0141 
0142 // function executor base class
0143 class CPYCPPYY_CLASS_EXTERN Executor {
0144 public:
0145     virtual ~Executor();
0146 
0147 // callback when executing a function from Python
0148     virtual PyObject* Execute(
0149         Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext*) = 0;
0150 
0151 // if an executor has state, it will be unique per function, shared otherwise
0152     virtual bool HasState() { return false; }
0153 };
0154 
0155 // create an executor based on its full type name
0156 CPYCPPYY_EXTERN Executor* CreateExecutor(const std::string& name, cdims_t = 0);
0157 
0158 // delete a previously created executor
0159 CPYCPPYY_EXTERN void DestroyConverter(Converter* p);
0160 
0161 // register a custom executor
0162 typedef Executor* (*ExecutorFactory_t)(cdims_t);
0163 CPYCPPYY_EXTERN bool RegisterExecutor(const std::string& name, ExecutorFactory_t);
0164 
0165 // register a custom executor that is a reference to an existing converter
0166 CPYCPPYY_EXTERN bool RegisterExecutorAlias(const std::string& name, const std::string& target);
0167 
0168 // remove a custom executor
0169 CPYCPPYY_EXTERN bool UnregisterExecutor(const std::string& name);
0170 
0171 // helper for calling into C++ from a custom executor
0172 CPYCPPYY_EXTERN void* CallVoidP(Cppyy::TCppMethod_t, Cppyy::TCppObject_t, CallContext*);
0173 
0174 
0175 //- C++ access to cppyy objects ---------------------------------------------
0176 
0177 // C++ Instance (python object proxy) to void* conversion
0178 CPYCPPYY_EXTERN void* Instance_AsVoidPtr(PyObject* pyobject);
0179 
0180 // void* to C++ Instance (python object proxy) conversion, returns a new reference
0181 CPYCPPYY_EXTERN PyObject* Instance_FromVoidPtr(
0182     void* addr, const std::string& classname, bool python_owns = false);
0183 
0184 // type verifiers for C++ Scope
0185 CPYCPPYY_EXTERN bool Scope_Check(PyObject* pyobject);
0186 CPYCPPYY_EXTERN bool Scope_CheckExact(PyObject* pyobject);
0187 
0188 // type verifiers for C++ Instance
0189 CPYCPPYY_EXTERN bool Instance_Check(PyObject* pyobject);
0190 CPYCPPYY_EXTERN bool Instance_CheckExact(PyObject* pyobject);
0191 
0192 // type verifier for sequences
0193 CPYCPPYY_EXTERN bool Sequence_Check(PyObject* pyobject);
0194 
0195 // helper to verify expected safety of moving an instance into C++
0196 CPYCPPYY_EXTERN bool Instance_IsLively(PyObject* pyobject);
0197 
0198 // type verifiers for C++ Overload
0199 CPYCPPYY_EXTERN bool Overload_Check(PyObject* pyobject);
0200 CPYCPPYY_EXTERN bool Overload_CheckExact(PyObject* pyobject);
0201 
0202 
0203 //- access to the python interpreter ----------------------------------------
0204 
0205 // import a python module, making its classes available to Cling
0206 CPYCPPYY_EXTERN bool Import(const std::string& name);
0207 
0208 // execute a python statement (e.g. "import sys")
0209 CPYCPPYY_EXTERN bool Exec(const std::string& cmd);
0210 
0211 // evaluate a python expression (e.g. "1+1")
0212 CPYCPPYY_EXTERN const PyResult Eval(const std::string& expr);
0213 
0214 // execute a python stand-alone script, with argv CLI arguments
0215 CPYCPPYY_EXTERN void ExecScript(const std::string& name, const std::vector<std::string>& args);
0216 
0217 // enter an interactive python session (exit with ^D)
0218 CPYCPPYY_EXTERN void Prompt();
0219 
0220 } // namespace CPyCppyy
0221 
0222 #endif // !CPYCPPYY_API_H