From 004113fc268453289b7c34774601f228f020f27f Mon Sep 17 00:00:00 2001 From: Kenneth Leiter Date: Tue, 2 Jul 2013 13:16:59 -0400 Subject: [PATCH] BUG: Add typemap for std::vector > When using XPaths from Python, the reader returns a vector of XdmfItems fitting the XPath description. However, it only returns the base class, which is useless. This commit adds a typemap in swig to convert the base classes to the appropriate types in a python list, allowing utilization of XPaths to from Python. --- Xdmf.i | 108 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 62 insertions(+), 46 deletions(-) diff --git a/Xdmf.i b/Xdmf.i index 5890b2e7..aeafb0ab 100644 --- a/Xdmf.i +++ b/Xdmf.i @@ -61,7 +61,7 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i // Typemaps that work for Java -%typemap(out) shared_ptr { +%typemap(out) shared_ptr { if(shared_ptr value = shared_dynamic_cast($1)) { *(shared_ptr< XdmfAttribute > **)&($result) = value ? new shared_ptr< XdmfAttribute >(value) : 0; } @@ -180,54 +180,70 @@ swig -v -c++ -python -o XdmfPython.cpp Xdmf.i from XdmfCore import * } -%typemap(out) shared_ptr { - if(shared_ptr attribute = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(attribute)), SWIGTYPE_p_boost__shared_ptrT_XdmfAttribute_t, SWIG_POINTER_OWN); - } - else if(shared_ptr gridCurvilinear = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridCurvilinear)), SWIGTYPE_p_boost__shared_ptrT_XdmfCurvilinearGrid_t, SWIG_POINTER_OWN); - } - else if(shared_ptr domain = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(domain)), SWIGTYPE_p_boost__shared_ptrT_XdmfDomain_t, SWIG_POINTER_OWN); - } - else if(shared_ptr geometry = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(geometry)), SWIGTYPE_p_boost__shared_ptrT_XdmfGeometry_t, SWIG_POINTER_OWN); - } - else if(shared_ptr graph = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(graph)), SWIGTYPE_p_boost__shared_ptrT_XdmfGraph_t, SWIG_POINTER_OWN); - } - else if(shared_ptr gridCollection = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridCollection)), SWIGTYPE_p_boost__shared_ptrT_XdmfGridCollection_t, SWIG_POINTER_OWN); - } - else if(shared_ptr information = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(information)), SWIGTYPE_p_boost__shared_ptrT_XdmfInformation_t, SWIG_POINTER_OWN); - } - else if(shared_ptr map = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(map)), SWIGTYPE_p_boost__shared_ptrT_XdmfMap_t, SWIG_POINTER_OWN); - } - else if(shared_ptr gridRectilinear = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridRectilinear)), SWIGTYPE_p_boost__shared_ptrT_XdmfRectilinearGrid_t, SWIG_POINTER_OWN); - } - else if(shared_ptr gridRegular = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridRegular)), SWIGTYPE_p_boost__shared_ptrT_XdmfRegularGrid_t, SWIG_POINTER_OWN); - } - else if(shared_ptr set = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(set)), SWIGTYPE_p_boost__shared_ptrT_XdmfSet_t, SWIG_POINTER_OWN); - } - else if(shared_ptr time = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(time)), SWIGTYPE_p_boost__shared_ptrT_XdmfTime_t, SWIG_POINTER_OWN); - } - else if(shared_ptr topology = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(topology)), SWIGTYPE_p_boost__shared_ptrT_XdmfTopology_t, SWIG_POINTER_OWN); - } - else if(shared_ptr gridUnstructured = shared_dynamic_cast($1)) { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridUnstructured)), SWIGTYPE_p_boost__shared_ptrT_XdmfUnstructuredGrid_t, SWIG_POINTER_OWN); - } - else { - $result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr($1)), SWIGTYPE_p_boost__shared_ptrT_XdmfItem_t, SWIG_POINTER_OWN); +%fragment("XdmfItemCast", "header") { + #include + PyObject * XdmfItemCast(shared_ptr obj) { + PyObject * result; + if(shared_ptr attribute = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(attribute)), SWIGTYPE_p_boost__shared_ptrT_XdmfAttribute_t, SWIG_POINTER_OWN); + } + else if(shared_ptr gridCurvilinear = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridCurvilinear)), SWIGTYPE_p_boost__shared_ptrT_XdmfCurvilinearGrid_t, SWIG_POINTER_OWN); + } + else if(shared_ptr domain = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(domain)), SWIGTYPE_p_boost__shared_ptrT_XdmfDomain_t, SWIG_POINTER_OWN); + } + else if(shared_ptr geometry = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(geometry)), SWIGTYPE_p_boost__shared_ptrT_XdmfGeometry_t, SWIG_POINTER_OWN); + } + else if(shared_ptr graph = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(graph)), SWIGTYPE_p_boost__shared_ptrT_XdmfGraph_t, SWIG_POINTER_OWN); + } + else if(shared_ptr gridCollection = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridCollection)), SWIGTYPE_p_boost__shared_ptrT_XdmfGridCollection_t, SWIG_POINTER_OWN); + } + else if(shared_ptr information = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(information)), SWIGTYPE_p_boost__shared_ptrT_XdmfInformation_t, SWIG_POINTER_OWN); + } + else if(shared_ptr map = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(map)), SWIGTYPE_p_boost__shared_ptrT_XdmfMap_t, SWIG_POINTER_OWN); + } + else if(shared_ptr gridRectilinear = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridRectilinear)), SWIGTYPE_p_boost__shared_ptrT_XdmfRectilinearGrid_t, SWIG_POINTER_OWN); + } + else if(shared_ptr gridRegular = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridRegular)), SWIGTYPE_p_boost__shared_ptrT_XdmfRegularGrid_t, SWIG_POINTER_OWN); + } + else if(shared_ptr set = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(set)), SWIGTYPE_p_boost__shared_ptrT_XdmfSet_t, SWIG_POINTER_OWN); + } + else if(shared_ptr time = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(time)), SWIGTYPE_p_boost__shared_ptrT_XdmfTime_t, SWIG_POINTER_OWN); + } + else if(shared_ptr topology = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(topology)), SWIGTYPE_p_boost__shared_ptrT_XdmfTopology_t, SWIG_POINTER_OWN); + } + else if(shared_ptr gridUnstructured = shared_dynamic_cast(obj)) { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(gridUnstructured)), SWIGTYPE_p_boost__shared_ptrT_XdmfUnstructuredGrid_t, SWIG_POINTER_OWN); + } + else { + result = SWIG_NewPointerObj(SWIG_as_voidptr(new shared_ptr(obj)), SWIGTYPE_p_boost__shared_ptrT_XdmfItem_t, SWIG_POINTER_OWN); + } + return result; } } +%typemap(out, fragment="XdmfItemCast") shared_ptr { + $result = XdmfItemCast($1); +} + +%typemap(out, fragment="XdmfItemCast") std::vector > { + $result = PyList_New($1.size()); + for(std::vector >::size_type i = 0; i < $1.size(); ++i) { + PyList_SetItem($result, i, XdmfItemCast($1[i])); + } +} + %extend XdmfAttributeCenter { bool __eq__(const XdmfAttributeCenter * attributeCenter) { return $self == attributeCenter; -- GitLab