From: Anthony Geay Date: Mon, 21 Jun 2021 19:39:33 +0000 (+0200) Subject: Agressive Windows porting X-Git-Tag: V9_9_0a1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8ec958c52e0bf4253411c87627073b5df66812ce;p=tools%2Fpy2cpp.git Agressive Windows porting --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9022030..38854a1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,6 +36,7 @@ SET(_py2cpp_sources ) SET(_py2cpp_headers + py2cppExports.hxx TypeConversions.hxx PyFunction.hxx Errors.hxx diff --git a/src/Errors.hxx b/src/Errors.hxx index a325820..8400767 100644 --- a/src/Errors.hxx +++ b/src/Errors.hxx @@ -19,6 +19,8 @@ #ifndef PY2CPP_ERRORS_HXX #define PY2CPP_ERRORS_HXX +#include "py2cppExports.hxx" + #include #include #include @@ -31,12 +33,12 @@ namespace py2cpp * After the call of this function, the python error indicator is cleared. * see PyErr_Fetch. */ -std::string getLastPyError(); +PY2CPP_EXPORT std::string getLastPyError(); /*! * ConversionCheck class gathers the errors within fromPy functions. */ -class ConversionCheck +class PY2CPP_EXPORT ConversionCheck { public: ConversionCheck(); @@ -52,7 +54,7 @@ private: std::string _message; }; -class Exception:public std::exception +class PY2CPP_EXPORT Exception:public std::exception { public: Exception(const std::string& message); @@ -61,19 +63,19 @@ private: std::string _message; }; -class ConversionException:public Exception +class PY2CPP_EXPORT ConversionException:public Exception { public: ConversionException(const std::string& message); }; -class ExecutionException:public Exception +class PY2CPP_EXPORT ExecutionException:public Exception { public: ExecutionException(const std::string& message); }; -class AttributeException:public Exception +class PY2CPP_EXPORT AttributeException:public Exception { public: AttributeException(const std::string& message); diff --git a/src/PyFunction.hxx b/src/PyFunction.hxx index b110c24..39a6f12 100644 --- a/src/PyFunction.hxx +++ b/src/PyFunction.hxx @@ -18,6 +18,8 @@ // #ifndef PY2CPP_FUNCTIONCALLS_HXX #define PY2CPP_FUNCTIONCALLS_HXX + +#include "py2cppExports.hxx" #include #include #include "TypeConversions.hxx" @@ -25,7 +27,7 @@ namespace py2cpp { -class PyFunction : public PyPtr +class PY2CPP_EXPORT PyFunction : public PyPtr { public: /*! Load a callable object from a python module.*/ diff --git a/src/PyPtr.hxx b/src/PyPtr.hxx index dad9bab..d118d3b 100644 --- a/src/PyPtr.hxx +++ b/src/PyPtr.hxx @@ -18,13 +18,14 @@ // #ifndef PY2CPP_PYPTR_HXX #define PY2CPP_PYPTR_HXX +#include "py2cppExports.hxx" #include #include #include namespace py2cpp { -class PyPtrDeleter +class PY2CPP_EXPORT PyPtrDeleter { public: void operator()(PyObject * po){Py_DECREF(po);} @@ -32,7 +33,7 @@ public: typedef std::unique_ptr _PyPtr; -class PyPtr: public _PyPtr +class PY2CPP_EXPORT PyPtr: public _PyPtr { public: PyPtr(); @@ -49,7 +50,7 @@ public: std::string repr()const; }; -class AutoGIL +class PY2CPP_EXPORT AutoGIL { public: AutoGIL():_gstate(PyGILState_Ensure()) { } diff --git a/src/Result.hxx b/src/Result.hxx index 301d795..f2f9c6f 100644 --- a/src/Result.hxx +++ b/src/Result.hxx @@ -18,6 +18,7 @@ // #ifndef PY2CPP_RESULT_HXX #define PY2CPP_RESULT_HXX +#include "py2cppExports.hxx" #include #include #include "TypeConversions.hxx" @@ -27,7 +28,7 @@ namespace py2cpp { template -class Result; +class PY2CPP_EXPORT Result; /*! class Result is used by pyResult function for syntax sugar purpose. * You can write this: @@ -57,7 +58,7 @@ class Result; * std::cerr << py2cpp::getLastPyError(); **/ template<> -class Result<> +class PY2CPP_EXPORT Result<> { public: void operator=(PyObject * po) @@ -69,7 +70,7 @@ public: }; template -class Result +class PY2CPP_EXPORT Result { public: Result() = delete; @@ -91,7 +92,7 @@ private: }; template -class Result +class PY2CPP_EXPORT Result { public: Result() = delete; @@ -111,7 +112,7 @@ private: }; template -Result pyResult(Ts&... args) +PY2CPP_EXPORT Result pyResult(Ts&... args) { return Result(args...); } diff --git a/src/TypeConversions.hxx b/src/TypeConversions.hxx index 7e8dab2..8575ce6 100644 --- a/src/TypeConversions.hxx +++ b/src/TypeConversions.hxx @@ -18,6 +18,7 @@ // #ifndef PY2CPP_TYPECONVERSIONS_HXX #define PY2CPP_TYPECONVERSIONS_HXX +#include "py2cppExports.hxx" #include #include @@ -34,59 +35,59 @@ namespace py2cpp * toPy functions return a new python object built from a c++ object. * The conversion is always possible and it does not throw exceptions. */ -PyObject * toPy(int); -PyObject * toPy(unsigned int); -PyObject * toPy(bool); -PyObject * toPy(double); -PyObject * toPy(const std::string&); -PyObject * toPy(const char*); -PyObject * toPy(PyObject *); -PyObject * toPy(const PyPtr&); +PY2CPP_EXPORT PyObject * toPy(int); +PY2CPP_EXPORT PyObject * toPy(unsigned int); +PY2CPP_EXPORT PyObject * toPy(bool); +PY2CPP_EXPORT PyObject * toPy(double); +PY2CPP_EXPORT PyObject * toPy(const std::string&); +PY2CPP_EXPORT PyObject * toPy(const char*); +PY2CPP_EXPORT PyObject * toPy(PyObject *); +PY2CPP_EXPORT PyObject * toPy(const PyPtr&); template -PyObject * toPy(const std::vector& values); +PY2CPP_EXPORT PyObject * toPy(const std::vector& values); template -PyObject * toPy(const std::list& values); +PY2CPP_EXPORT PyObject * toPy(const std::list& values); template -PyObject * toPy(const std::map& values); +PY2CPP_EXPORT PyObject * toPy(const std::map& values); template -PyObject * toPy(const std::tuple& vars ); +PY2CPP_EXPORT PyObject * toPy(const std::tuple& vars ); /*! * fromPy functions convert a python object to a c++ object. * If the convertion fails, the ConversionCheck object returned contains the * error message. No exception is thrown. */ -ConversionCheck fromPy( PyObject *, int&); -ConversionCheck fromPy( PyObject *, unsigned int&); -ConversionCheck fromPy( PyObject *, bool&); -ConversionCheck fromPy( PyObject *, double&); -ConversionCheck fromPy( PyObject *, std::string&); -ConversionCheck fromPy( PyObject *, PyObject *&); -ConversionCheck fromPy( PyObject *, PyPtr&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, int&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, unsigned int&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, bool&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, double&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, std::string&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, PyObject *&); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *, PyPtr&); template -ConversionCheck fromPy(PyObject * obj, std::tuple& vars); +PY2CPP_EXPORT ConversionCheck fromPy(PyObject * obj, std::tuple& vars); template -ConversionCheck fromPy( PyObject *obj, std::vector& result); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *obj, std::vector& result); template -ConversionCheck fromPy( PyObject *obj, std::list& result); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *obj, std::list& result); template -ConversionCheck fromPy( PyObject *obj, std::map& result); +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *obj, std::map& result); /*! This templated version of fromPy throws ConversionException if the * conversion fails. */ template -T fromPy( PyObject *po); +PY2CPP_EXPORT T fromPy( PyObject *po); // These versions of fromPy and toPy use PyPtr instead of PyObject * template -T fromPyPtr( const PyPtr& py); +PY2CPP_EXPORT T fromPyPtr( const PyPtr& py); template -ConversionCheck fromPyPtr( const PyPtr& py, T& var); +PY2CPP_EXPORT ConversionCheck fromPyPtr( const PyPtr& py, T& var); template -PyPtr toPyPtr(const T& v); +PY2CPP_EXPORT PyPtr toPyPtr(const T& v); template<> -PyPtr fromPy( PyObject *po); +PY2CPP_EXPORT PyPtr fromPy( PyObject *po); //////////////////////////////////////////////////////////////////////////////// // Template implementations @@ -109,7 +110,7 @@ addInPyTuple(PyObject * result, const std::tuple& vars ) } template -PyObject * toPy(const std::tuple& vars ) +PY2CPP_EXPORT PyObject * toPy(const std::tuple& vars ) { PyObject * result = PyTuple_New(sizeof...(Ts)); addInPyTuple<0, Ts... >(result, vars); @@ -118,7 +119,7 @@ PyObject * toPy(const std::tuple& vars ) // std containers template -PyObject * toPy(const std::vector& values) +PY2CPP_EXPORT PyObject * toPy(const std::vector& values) { PyObject * result = PyList_New(values.size()); for(std::size_t i = 0; i < values.size(); ++i) @@ -127,7 +128,7 @@ PyObject * toPy(const std::vector& values) } template -PyObject * toPy(const std::list& values) +PY2CPP_EXPORT PyObject * toPy(const std::list& values) { PyObject * result = PyList_New(values.size()); std::size_t i = 0; @@ -140,7 +141,7 @@ PyObject * toPy(const std::list& values) } template -PyObject * toPy(const std::map& values) +PY2CPP_EXPORT PyObject * toPy(const std::map& values) { PyObject * result = PyDict_New(); for(const auto& it: values) @@ -173,7 +174,7 @@ getFromPyTuple(PyObject * tup, std::tuple& vars ) } template -ConversionCheck fromPy(PyObject * obj, std::tuple& vars) +PY2CPP_EXPORT ConversionCheck fromPy(PyObject * obj, std::tuple& vars) { ConversionCheck check; if(obj) @@ -197,7 +198,7 @@ ConversionCheck fromPy(PyObject * obj, std::tuple& vars) } template -ConversionCheck fromPy( PyObject *obj, std::vector& result) +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *obj, std::vector& result) { ConversionCheck check; if(PyList_Check(obj)) @@ -226,7 +227,7 @@ ConversionCheck fromPy( PyObject *obj, std::vector& result) } template -ConversionCheck fromPy( PyObject *obj, std::list& result) +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *obj, std::list& result) { ConversionCheck check; if(PyList_Check(obj)) @@ -269,7 +270,7 @@ ConversionCheck fromPy( PyObject *obj, std::list& result) } template -ConversionCheck fromPy( PyObject *obj, std::map& result) +PY2CPP_EXPORT ConversionCheck fromPy( PyObject *obj, std::map& result) { ConversionCheck check; if(PyDict_Check(obj)) @@ -298,7 +299,7 @@ ConversionCheck fromPy( PyObject *obj, std::map& result) // PyPtr template -T fromPyPtr( const PyPtr& py) +PY2CPP_EXPORT T fromPyPtr( const PyPtr& py) { T result; fromPy(py.get(), result); @@ -306,19 +307,19 @@ T fromPyPtr( const PyPtr& py) } template -ConversionCheck fromPyPtr( const PyPtr& py, T& var) +PY2CPP_EXPORT ConversionCheck fromPyPtr( const PyPtr& py, T& var) { return fromPy(py.get(), var); } template -PyPtr toPyPtr(const T& v) +PY2CPP_EXPORT PY2CPP_EXPORT PyPtr toPyPtr(const T& v) { return PyPtr(toPy(v)); } template -T fromPy( PyObject *po) +PY2CPP_EXPORT T fromPy( PyObject *po) { T result; ConversionCheck check; diff --git a/src/py2cppExports.hxx b/src/py2cppExports.hxx new file mode 100644 index 0000000..cc55549 --- /dev/null +++ b/src/py2cppExports.hxx @@ -0,0 +1,30 @@ +// Copyright (C) 2021 EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#pragma once + +#ifdef WIN32 +# if defined(py2cpp_EXPORTS) || defined(py2cpp_EXPORTS) +# define PY2CPP_EXPORT __declspec( dllexport ) +# else +# define PY2CPP_EXPORT __declspec( dllimport ) +# endif +#else +# define PY2CPP_EXPORT +#endif