// (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 PY_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, py::Type) { return static_cast( from_python(x, py::Type())); } friend EnumType from_python(PyObject* x, py::Type) { return static_cast( from_python(x, py::Type())); } friend PyObject* to_python(EnumType x) { return to_python(static_cast(x)); } }; PY_END_CONVERSION_NAMESPACE namespace py { template class enum_as_int_converters : public PY_CONVERSION::py_enum_as_int_converters {}; template class WrappedPointer; //#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 py PY_BEGIN_CONVERSION_NAMESPACE // // Converters // PyObject* to_python(long); long from_python(PyObject* p, py::Type); long from_python(PyObject* p, py::Type); PyObject* to_python(unsigned long); unsigned long from_python(PyObject* p, py::Type); unsigned long from_python(PyObject* p, py::Type); PyObject* to_python(int); int from_python(PyObject*, py::Type); int from_python(PyObject*, py::Type); PyObject* to_python(unsigned int); unsigned int from_python(PyObject*, py::Type); unsigned int from_python(PyObject*, py::Type); PyObject* to_python(short); short from_python(PyObject*, py::Type); short from_python(PyObject*, py::Type); PyObject* to_python(unsigned short); unsigned short from_python(PyObject*, py::Type); unsigned short from_python(PyObject*, py::Type); PyObject* to_python(signed char); signed char from_python(PyObject*, py::Type); signed char from_python(PyObject*, py::Type); PyObject* to_python(unsigned char); unsigned char from_python(PyObject*, py::Type); unsigned char from_python(PyObject*, py::Type); PyObject* to_python(float); float from_python(PyObject*, py::Type); float from_python(PyObject*, py::Type); PyObject* to_python(double); double from_python(PyObject*, py::Type); double from_python(PyObject*, py::Type); PyObject* to_python(bool); bool from_python(PyObject*, py::Type); bool from_python(PyObject*, py::Type); PyObject* to_python(void); void from_python(PyObject*, py::Type); PyObject* to_python(const char* s); const char* from_python(PyObject*, py::Type); PyObject* to_python(const std::string& s); std::string from_python(PyObject*, py::Type); std::string from_python(PyObject*, py::Type); // For when your C++ function really wants to pass/return a PyObject* PyObject* to_python(PyObject*); PyObject* from_python(PyObject*, py::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 // WrappedPointer 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 WrappedType. // // 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 py { // #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(PY_MSVC6_OR_EARLIER) template boost::shared_ptr from_python(PyObject*p, py::Type >) { return smart_ptr_from_python(p, py::Type >(), py::Type()); } #endif #if 0 template PyObject* to_python(std::auto_ptr p) { return new py::WrappedPointer, T>(p); } template PyObject* to_python(boost::shared_ptr p) { return new py::WrappedPointer, T>(p); } #endif // // inline implementations // #ifndef PY_MSVC6_OR_EARLIER inline PyObject* to_python(double d) { return PyFloat_FromDouble(d); } inline PyObject* to_python(float f) { return PyFloat_FromDouble(f); } #endif // PY_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 py::none(); } inline PyObject* to_python(const char* s) { return PyString_FromString(s); } inline std::string from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline PyObject* to_python(PyObject* p) { Py_INCREF(p); return p; } inline PyObject* from_python(PyObject* p, py::Type) { return p; } inline const char* from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline double from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline float from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline int from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline short from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline long from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline bool from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline unsigned int from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline unsigned short from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline signed char from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline unsigned char from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } inline unsigned long from_python(PyObject* p, py::Type) { return from_python(p, py::Type()); } PY_END_CONVERSION_NAMESPACE #endif // METHOD_DWA122899_H_