From 6b2b4f0cf9157e044965acba63fb6b5ee1e9cf15 Mon Sep 17 00:00:00 2001 From: Marcelo Lira Date: Thu, 11 Aug 2011 19:37:11 -0300 Subject: New converters for user added primitive types. Win32 fix for zero length type converter array. --- libshiboken/sbkconverter.cpp | 25 +++++++++++++++++++++---- libshiboken/sbkconverter.h | 11 +++++++++++ libshiboken/sbkmodule.cpp | 19 +++++++++++++++++++ libshiboken/sbkmodule.h | 19 +++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) (limited to 'libshiboken') diff --git a/libshiboken/sbkconverter.cpp b/libshiboken/sbkconverter.cpp index b252e16c7..c5e64f3dd 100644 --- a/libshiboken/sbkconverter.cpp +++ b/libshiboken/sbkconverter.cpp @@ -41,7 +41,8 @@ static SbkConverter* createConverterObject(PyTypeObject* type, converter->pointerToPython = pointerToPythonFunc; converter->copyToPython = copyToPythonFunc; - converter->toCppPointerConversion = std::make_pair(toCppPointerCheckFunc, toCppPointerConvFunc); + if (toCppPointerCheckFunc && toCppPointerConvFunc) + converter->toCppPointerConversion = std::make_pair(toCppPointerCheckFunc, toCppPointerConvFunc); converter->toCppConversions.clear(); return converter; @@ -60,6 +61,11 @@ SbkConverter* createConverter(SbkObjectType* type, return converter; } +SbkConverter* createConverter(PyTypeObject* type, CppToPythonFunc toPythonFunc) +{ + return createConverterObject(type, 0, 0, 0, toPythonFunc); +} + void deleteConverter(SbkConverter* converter) { if (converter) { @@ -178,16 +184,27 @@ void pythonToCppPointer(SbkObjectType* type, PyObject* pyIn, void* cppOut) *((void**)cppOut) = (pyIn == Py_None) ? 0 : cppPointer((PyTypeObject*)type, (SbkObject*)pyIn); } -void pythonToCppCopy(SbkObjectType* type, PyObject* pyIn, void* cppOut) +static void _pythonToCppCopy(SbkConverter* converter, PyObject* pyIn, void* cppOut) { - assert(type); + assert(converter); assert(pyIn); assert(cppOut); - PythonToCppFunc toCpp = IsPythonToCppConvertible(type->d->converter, pyIn); + PythonToCppFunc toCpp = IsPythonToCppConvertible(converter, pyIn); if (toCpp) toCpp(pyIn, cppOut); } +void pythonToCppCopy(SbkObjectType* type, PyObject* pyIn, void* cppOut) +{ + assert(type); + _pythonToCppCopy(type->d->converter, pyIn, cppOut); +} + +void pythonToCpp(SbkConverter* converter, PyObject* pyIn, void* cppOut) +{ + _pythonToCppCopy(converter, pyIn, cppOut); +} + bool isImplicitConversion(SbkObjectType* type, PythonToCppFunc toCppFunc) { // This is the Object Type or Value Type conversion that only diff --git a/libshiboken/sbkconverter.h b/libshiboken/sbkconverter.h index d7be25776..1d5392488 100644 --- a/libshiboken/sbkconverter.h +++ b/libshiboken/sbkconverter.h @@ -102,6 +102,14 @@ LIBSHIBOKEN_API SbkConverter* createConverter(SbkObjectType* type, CppToPythonFunc pointerToPythonFunc, CppToPythonFunc copyToPythonFunc = 0); +/** + * Creates a converter for a non wrapper type (primitive or container type). + * \param type Python type representing to the new converter. + * \param toPythonFunc Function to convert a C++ object to a Python \p type. + * \returns A new type converter. + */ +LIBSHIBOKEN_API SbkConverter* createConverter(PyTypeObject* type, CppToPythonFunc toPythonFunc); + LIBSHIBOKEN_API void deleteConverter(SbkConverter* converter); /** @@ -187,6 +195,9 @@ LIBSHIBOKEN_API void pythonToCppPointer(SbkObjectType* type, PyObject* pyIn, voi /// Converts a Python object \p pyIn to C++ and copies the result in the C++ variable passed in \p cppOut. LIBSHIBOKEN_API void pythonToCppCopy(SbkObjectType* type, PyObject* pyIn, void* cppOut); +/// Converts a Python object \p pyIn to C++, copying the result in the C++ variable passed in \p cppOut. +LIBSHIBOKEN_API void pythonToCpp(SbkConverter* converter, PyObject* pyIn, void* cppOut); + /** * Helper function returned by generated convertible checking functions * that returns a C++ NULL when the input Python object is None. diff --git a/libshiboken/sbkmodule.cpp b/libshiboken/sbkmodule.cpp index 83c431cde..6f3594e77 100644 --- a/libshiboken/sbkmodule.cpp +++ b/libshiboken/sbkmodule.cpp @@ -32,8 +32,12 @@ /// This hash maps module objects to arrays of Python types. typedef google::dense_hash_map ModuleTypesMap; +/// This hash maps module objects to arrays of converters. +typedef google::dense_hash_map ModuleConvertersMap; + /// All types produced in imported modules are mapped here. static ModuleTypesMap moduleTypes; +static ModuleConvertersMap moduleConverters; namespace Shiboken { @@ -45,6 +49,8 @@ void init() // Initializes type registry for modules. moduleTypes.set_empty_key((ModuleTypesMap::key_type)0); moduleTypes.set_deleted_key((ModuleTypesMap::key_type)1); + moduleConverters.set_empty_key((ModuleConvertersMap::key_type)0); + moduleConverters.set_deleted_key((ModuleConvertersMap::key_type)1); } PyObject* import(const char* moduleName) @@ -85,4 +91,17 @@ PyTypeObject** getTypes(PyObject* module) return (iter == moduleTypes.end()) ? 0 : iter->second; } +void registerTypeConverters(PyObject* module, SbkConverter** converters) +{ + ModuleConvertersMap::iterator iter = moduleConverters.find(module); + if (iter == moduleConverters.end()) + moduleConverters.insert(std::make_pair(module, converters)); +} + +SbkConverter** getTypeConverters(PyObject* module) +{ + ModuleConvertersMap::iterator iter = moduleConverters.find(module); + return (iter == moduleConverters.end()) ? 0 : iter->second; +} + } } // namespace Shiboken::Module diff --git a/libshiboken/sbkmodule.h b/libshiboken/sbkmodule.h index f089ad8d4..cb5656fca 100644 --- a/libshiboken/sbkmodule.h +++ b/libshiboken/sbkmodule.h @@ -42,6 +42,11 @@ } #endif +extern "C" +{ +struct SbkConverter; +} + namespace Shiboken { namespace Module { @@ -74,6 +79,20 @@ LIBSHIBOKEN_API void registerTypes(PyObject* module, PyTypeObject** types); */ LIBSHIBOKEN_API PyTypeObject** getTypes(PyObject* module); +/** + * Registers the list of converters created by \p module for non-wrapper types. + * \param module Module where the converters were created. + * \param converters Array of SbkConverter* objects representing the converters created on \p module. + */ +LIBSHIBOKEN_API void registerTypeConverters(PyObject* module, SbkConverter** converters); + +/** + * Retrieves the array of converters. + * \param module Module where the converters were created. + * \returns A pointer to the SbkConverter* array of converters. + */ +LIBSHIBOKEN_API SbkConverter** getTypeConverters(PyObject* module); + } } // namespace Shiboken::Module #endif // SBK_MODULE_H -- cgit v1.2.3