summaryrefslogtreecommitdiffstats
path: root/libshiboken
diff options
context:
space:
mode:
authorMarcelo Lira <marcelo.lira@openbossa.org>2011-08-11 19:37:11 -0300
committerMarcelo Lira <marcelo.lira@openbossa.org>2011-12-09 20:28:05 -0300
commit3783e1c72b5497c5564a02308efb0b1fc7d4e224 (patch)
treef2b4d3c7707f6f26945cd328fef3d092407c247f /libshiboken
parenta3f655eb36f0a3d7a854db9cb5721d30c7668ef0 (diff)
downloadshiboken-3783e1c72b5497c5564a02308efb0b1fc7d4e224.tar.gz
shiboken-3783e1c72b5497c5564a02308efb0b1fc7d4e224.tar.xz
shiboken-3783e1c72b5497c5564a02308efb0b1fc7d4e224.zip
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 b252e16c..c5e64f3d 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 d7be2577..1d539248 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 83c431cd..6f3594e7 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 f089ad8d..cb5656fc 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