Minimize passing STL objects past dynamic library boundaries to improve stability of IfcOpenShell-Python

This commit is contained in:
Thomas Krijnen
2015-06-02 13:29:08 +00:00
parent 5a994ebe28
commit b96cf38e17
4 changed files with 138 additions and 32 deletions
+41 -8
View File
@@ -41,13 +41,46 @@
#include "../ifcparse/IfcLateBoundEntity.h"
%}
// The following typemaps are an alternative for the read-only std::vector
// implementations provided by SWIG, as passing STL objects across dynamic
// library boundaries can be problematic.
%typemap(out) std::pair<const float*, unsigned> {
$result = PyTuple_New($1.second);
for (unsigned i = 0; i < $1.second; ++i) {
PyTuple_SetItem($result, i, PyFloat_FromDouble($1.first[i]));
}
}
%typemap(out) std::pair<const double*, unsigned> {
$result = PyTuple_New($1.second);
for (unsigned i = 0; i < $1.second; ++i) {
PyTuple_SetItem($result, i, PyFloat_FromDouble($1.first[i]));
}
}
%typemap(out) std::pair<const int*, unsigned> {
$result = PyTuple_New($1.second);
for (unsigned i = 0; i < $1.second; ++i) {
PyTuple_SetItem($result, i, PyLong_FromLong($1.first[i]));
}
}
%typemap(out) std::pair<const std::string*, unsigned> {
$result = PyTuple_New($1.second);
for (unsigned i = 0; i < $1.second; ++i) {
PyTuple_SetItem($result, i, PyUnicode_FromString($1.first[i]->c_str()));
}
}
%typemap(out) std::pair<const IfcGeom::Material*, unsigned> {
$result = PyTuple_New($1.second);
for (unsigned i = 0; i < $1.second; ++i) {
PyTuple_SetItem($result, i, SWIG_NewPointerObj(SWIG_as_voidptr(&$1.first[i]), SWIGTYPE_p_IfcGeom__Material, 0));
}
}
%typemap(out) std::vector<unsigned> {
$result = PyTuple_New($1.size());
unsigned i = 0;
for (std::vector<unsigned>::const_iterator it = $1.begin(); it != $1.end(); ++it) {
PyTuple_SetItem($result, i++, PyLong_FromLong(*it));
}
}
%include "IfcGeomWrapper.i"
%include "IfcParseWrapper.i"
namespace std {
%template(int_vector) vector<int>;
%template(float_vector) vector<float>;
%template(double_vector) vector<double>;
%template(string_vector) vector<std::string>;
%template(material_vector) vector<IfcGeom::Material>;
};