// (C) Copyright David Abrahams 2000. Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // // The author gratefully acknowleges the support of Dragon Systems, Inc., in // producing this work. #ifndef METHOD_DWA122899_H_ # define METHOD_DWA122899_H_ # include "pyconfig.h" # include "wrap_python.h" # include "none.h" # include "signatures.h" # include # include "errors.h" # include BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // this is a gcc 2.95.2 bug workaround // This can be instantiated on an enum to provide the to_python/from_python // conversions, provided the values can fit in a long. template class py_enum_as_int_converters { friend EnumType from_python(PyObject* x, python::type) { return static_cast( from_python(x, python::type())); } friend EnumType from_python(PyObject* x, python::type) { return static_cast( from_python(x, python::type())); } friend PyObject* to_python(EnumType x) { return to_python(static_cast(x)); } }; BOOST_PYTHON_END_CONVERSION_NAMESPACE namespace python { template class enum_as_int_converters : public BOOST_PYTHON_CONVERSION::py_enum_as_int_converters {}; template class wrapped_pointer; //#pragma warn_possunwant off inline void decref_impl(PyObject* p) { Py_DECREF(p); } inline void xdecref_impl(PyObject* p) { Py_XDECREF(p); } //#pragma warn_possunwant reset template inline void decref(T* p) { char* const raw_p = reinterpret_cast(p); char* const p_base = raw_p - offsetof(PyObject, ob_refcnt); decref_impl(reinterpret_cast(p_base)); } template inline void xdecref(T* p) { char* const raw_p = reinterpret_cast(p); char* const p_base = raw_p - offsetof(PyObject, ob_refcnt); xdecref_impl(reinterpret_cast(p_base)); } } // namespace python BOOST_PYTHON_BEGIN_CONVERSION_NAMESPACE // // Converters // PyObject* to_python(long); long from_python(PyObject* p, python::type); long from_python(PyObject* p, python::type); PyObject* to_python(unsigned long); unsigned long from_python(PyObject* p, python::type); unsigned long from_python(PyObject* p, python::type); PyObject* to_python(int); int from_python(PyObject*, python::type); int from_python(PyObject*, python::type); PyObject* to_python(unsigned int); unsigned int from_python(PyObject*, python::type); unsigned int from_python(PyObject*, python::type); PyObject* to_python(short); short from_python(PyObject*, python::type); short from_python(PyObject*, python::type); PyObject* to_python(unsigned short); unsigned short from_python(PyObject*, python::type); unsigned short from_python(PyObject*, python::type); PyObject* to_python(signed char); signed char from_python(PyObject*, python::type); signed char from_python(PyObject*, python::type); PyObject* to_python(unsigned char); unsigned char from_python(PyObject*, python::type); unsigned char from_python(PyObject*, python::type); PyObject* to_python(float); float from_python(PyObject*, python::type); float from_python(PyObject*, python::type); PyObject* to_python(double); double from_python(PyObject*, python::type); double from_python(PyObject*, python::type); PyObject* to_python(bool); bool from_python(PyObject*, python::type); bool from_python(PyObject*, python::type); PyObject* to_python(void); void from_python(PyObject*, python::type); PyObject* to_python(const char* s); const char* from_python(PyObject*, python::type); PyObject* to_python(const std::string& s); std::string from_python(PyObject*, python::type); std::string from_python(PyObject*, python::type); // For when your C++ function really wants to pass/return a PyObject* PyObject* to_python(PyObject*); PyObject* from_python(PyObject*, python::type); // Some standard conversions to/from smart pointer types. You can add your own // from these examples. These are not generated using the friend technique from // wrapped_pointer because: // // 1. We want to be able to extend conversion to/from WrappedPointers using // arbitrary smart pointer types. // // 2. It helps with compilation independence. This way, code which creates // wrappers for functions accepting and returning smart_ptr does not // have to have already seen the invocation of wrapped_type. // // Unfortunately, MSVC6 is so incredibly lame that we have to rely on the friend // technique to auto_generate standard pointer conversions for wrapped // types. This means that you need to write a non-templated function for each // specific smart_ptr which you want to convert from_python. For example, // // namespace python { // #ifdef MUST_SUPPORT_MSVC // // MyPtr from_python(PyObject*p, type >) // { return smart_ptr_from_python(p, type >(), type());} // } // // MyPtr from_python(PyObject*p, type >) // { return smart_ptr_from_python(p, type >(), type());} // // ... // definitions for MyPtr, MyPtr, etc. // // #else // // // Just once for all MyPtr // template // MyPtr from_python(PyObject*p, type >) // { // return smart_ptr_from_python(p, type >(), type()); // } // // #endif // } #if !defined(BOOST_MSVC6_OR_EARLIER) template boost::shared_ptr from_python(PyObject*p, python::type >) { return smart_ptr_from_python(p, python::type >(), python::type()); } #endif #if 0 template PyObject* to_python(std::auto_ptr p) { return new python::wrapped_pointer, T>(p); } template PyObject* to_python(boost::shared_ptr p) { return new python::wrapped_pointer, T>(p); } #endif // // inline implementations // #ifndef BOOST_MSVC6_OR_EARLIER inline PyObject* to_python(double d) { return PyFloat_FromDouble(d); } inline PyObject* to_python(float f) { return PyFloat_FromDouble(f); } #endif // BOOST_MSVC6_OR_EARLIER inline PyObject* to_python(long l) { return PyInt_FromLong(l); } inline PyObject* to_python(int x) { return PyInt_FromLong(x); } inline PyObject* to_python(short x) { return PyInt_FromLong(x); } inline PyObject* to_python(bool b) { return PyInt_FromLong(b); } inline PyObject* to_python(void) { return python::detail::none(); } inline PyObject* to_python(const char* s) { return PyString_FromString(s); } inline std::string from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline PyObject* to_python(PyObject* p) { Py_INCREF(p); return p; } inline PyObject* from_python(PyObject* p, python::type) { return p; } inline const char* from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline double from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline float from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline int from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline short from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline long from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline bool from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline unsigned int from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline unsigned short from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline signed char from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline unsigned char from_python(PyObject* p, python::type) { return from_python(p, python::type()); } inline unsigned long from_python(PyObject* p, python::type) { return from_python(p, python::type()); } BOOST_PYTHON_END_CONVERSION_NAMESPACE #endif // METHOD_DWA122899_H_