aboutsummaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-08-11 19:37:11 -0300
committerHugo Parente Lima <hugo.pl@gmail.com>2012-03-08 16:18:26 -0300
commit6b2b4f0cf9157e044965acba63fb6b5ee1e9cf15 (patch)
treebc96a4a47a6c804921c804993dc0aaf53e9233b0 /libshiboken
parentb8bd47404fd3860bdb282750a1e5919921ca80df (diff)
New converters for user added primitive types.
Win32 fix for zero length type converter array.
Diffstat (limited to 'libshiboken')
-rw-r--r--libshiboken/sbkconverter.cpp25
-rw-r--r--libshiboken/sbkconverter.h11
-rw-r--r--libshiboken/sbkmodule.cpp19
-rw-r--r--libshiboken/sbkmodule.h19
4 files changed, 70 insertions, 4 deletions
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<PyObject*, PyTypeObject**> ModuleTypesMap;
+/// This hash maps module objects to arrays of converters.
+typedef google::dense_hash_map<PyObject*, SbkConverter**> 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