Commit 69fd88d7 authored by peastman's avatar peastman
Browse files

Merge pull request #1135 from rmcgibbo/typemaps2

More typechecks
parents 992487c6 fbf13b1d
...@@ -137,6 +137,8 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -137,6 +137,8 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
while ((item = PyIter_Next(iterator))) { while ((item = PyIter_Next(iterator))) {
item1 = Py_StripOpenMMUnits(item); item1 = Py_StripOpenMMUnits(item);
if (item1 == NULL) { if (item1 == NULL) {
Py_DECREF(stripped);
Py_DECREF(iterator);
Py_DECREF(item); Py_DECREF(item);
return SWIG_ERROR; return SWIG_ERROR;
} }
...@@ -145,6 +147,8 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -145,6 +147,8 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
Py_DECREF(item1); Py_DECREF(item1);
if (PyErr_Occurred() != NULL) { if (PyErr_Occurred() != NULL) {
Py_DECREF(stripped);
Py_DECREF(iterator);
return SWIG_ERROR; return SWIG_ERROR;
} }
out.push_back(d); out.push_back(d);
...@@ -155,7 +159,51 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -155,7 +159,51 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
} }
} }
%fragment("Py_SequenceToVecVec3", "header", fragment="Py_SequenceToVec3") {
int Py_SequenceToVecVec3(PyObject* obj, std::vector<Vec3>& out) {
int ret = 0;
PyObject* stripped = NULL;
PyObject* item = NULL;
PyObject* item1 = NULL;
PyObject* iterator = NULL;
stripped = Py_StripOpenMMUnits(obj); // new reference
iterator = PyObject_GetIter(stripped); // new reference
if (iterator == NULL) {
Py_DECREF(stripped);
return SWIG_ERROR;
}
while ((item = PyIter_Next(iterator))) { // new reference
item1 = Py_StripOpenMMUnits(item); // new reference
if (item1 == NULL) {
Py_DECREF(stripped);
Py_DECREF(iterator);
Py_DECREF(item);
return SWIG_ERROR;
}
OpenMM::Vec3 v = Py_SequenceToVec3(item1, ret);
Py_DECREF(item);
Py_DECREF(item1);
if (!SWIG_IsOK(ret)) {
Py_DECREF(stripped);
Py_DECREF(iterator);
return SWIG_ERROR;
}
out.push_back(v);
}
Py_DECREF(iterator);
Py_DECREF(stripped);
return SWIG_OK;
}
}
// ------ typemap for double ----
%typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE, fragment="Py_StripOpenMMUnits") double { %typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE, fragment="Py_StripOpenMMUnits") double {
double argp = 0; double argp = 0;
PyObject* s = NULL; PyObject* s = NULL;
...@@ -163,7 +211,6 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -163,7 +211,6 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
$1 = (s != NULL) ? SWIG_IsOK(SWIG_AsVal_double(s, &argp)) : 0; $1 = (s != NULL) ? SWIG_IsOK(SWIG_AsVal_double(s, &argp)) : 0;
Py_DECREF(s); Py_DECREF(s);
} }
%typemap(in, noblock=1, fragment="Py_StripOpenMMUnits") double (double argp = 0, int res = 0, %typemap(in, noblock=1, fragment="Py_StripOpenMMUnits") double (double argp = 0, int res = 0,
PyObject* stripped = NULL) { PyObject* stripped = NULL) {
...@@ -179,6 +226,7 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -179,6 +226,7 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
} }
// ------ typemap for Vec3
%typemap(in, fragment="Py_SequenceToVec3") Vec3 (int res=0){ %typemap(in, fragment="Py_SequenceToVec3") Vec3 (int res=0){
// typemap -- %typemap(in) Vec3 // typemap -- %typemap(in) Vec3
$1 = Py_SequenceToVec3($input, res); $1 = Py_SequenceToVec3($input, res);
...@@ -187,8 +235,14 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -187,8 +235,14 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
SWIG_fail; SWIG_fail;
} }
} }
%typemap(typecheck, fragment="Py_SequenceToVec3") Vec3 {
int res = 0;
Py_SequenceToVec3($input, res);
$1 = SWIG_IsOK(res);
}
// typemap for const Vec3&
%typemap(in, fragment="Py_SequenceToVec3") const Vec3& (OpenMM::Vec3 myVec, int res=0) { %typemap(in, fragment="Py_SequenceToVec3") const Vec3& (OpenMM::Vec3 myVec, int res=0) {
// typemap -- %typemap(in) Vec3 // typemap -- %typemap(in) Vec3
myVec = Py_SequenceToVec3($input, res); myVec = Py_SequenceToVec3($input, res);
...@@ -198,30 +252,32 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -198,30 +252,32 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
} }
$1 = &myVec; $1 = &myVec;
} }
%typemap(typecheck, fragment="Py_SequenceToVec3") const Vec3& {
int res = 0;
Py_SequenceToVec3($input, res);
$1 = SWIG_IsOK(res);
}
/* Convert python list of tuples to C++ std::vector of Vec3 objects */
%typemap(in, fragment="Py_SequenceToVec3") const std::vector<Vec3>& (std::vector<OpenMM::Vec3> vVec, PyObject* s=NULL, PyObject* o=NULL) { // typemap for vector<Vec3>
int i, pLength, ret; %typemap(in, fragment="Py_SequenceToVecVec3") const std::vector<Vec3>& (std::vector<Vec3> v, int res=0) {
s = Py_StripOpenMMUnits($input); res = Py_SequenceToVecVec3($input, v);
pLength = (int)PySequence_Length(s); if (!SWIG_IsOK(res)) {
for (i = 0; i < pLength; i++) { PyErr_SetString(PyExc_ValueError, "in method $symname, argument $argnum could not be converted to type $type");
o = PySequence_GetItem(s, i); SWIG_fail;
OpenMM::Vec3 v = Py_SequenceToVec3(o, ret);
if (!SWIG_IsOK(ret)) {
Py_DECREF(s);
Py_DECREF(o);
PyErr_SetString(PyExc_ValueError, "in method $symname, argument $argnum could not be converted to type $type");
SWIG_fail;
}
vVec.push_back(v);
} }
$1 = &vVec; $1 = &v;
Py_DECREF(s); }
%typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE_ARRAY, fragment="Py_SequenceToVecVec3") const std::vector<Vec3>& {
std::vector<double> v;
int res=0;
res = Py_SequenceToVecVec3($input, v);
$1 = SWIG_IsOK(res);
} }
// typemap for const vector<double>
%typemap(in, fragment="Py_SequenceToVecDouble") const std::vector<double> & (std::vector<double> v, int res=0) { %typemap(in, fragment="Py_SequenceToVecDouble") const std::vector<double> & (std::vector<double> v, int res=0) {
res = Py_SequenceToVecDouble($input, v); res = Py_SequenceToVecDouble($input, v);
if (!SWIG_IsOK(res)) { if (!SWIG_IsOK(res)) {
...@@ -230,6 +286,14 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -230,6 +286,14 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
} }
$1 = &v; $1 = &v;
} }
%typemap(typecheck, precedence=SWIG_TYPECHECK_DOUBLE_ARRAY) const std::vector<double> & {
std::vector<double> v;
int res = 0;
res = Py_SequenceToVecDouble($input, v);
$1 = SWIG_IsOK(res);
}
/* The following two typemaps cause a non-const vector<Vec3>& to become a return value. */ /* The following two typemaps cause a non-const vector<Vec3>& to become a return value. */
%typemap(in, numinputs=0) std::vector<Vec3>& (std::vector<Vec3> temp) { %typemap(in, numinputs=0) std::vector<Vec3>& (std::vector<Vec3> temp) {
...@@ -260,17 +324,7 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) { ...@@ -260,17 +324,7 @@ int Py_SequenceToVecDouble(PyObject* obj, std::vector<double>& out) {
%typemap(argout) const std::vector<Vec3>& { %typemap(argout) const std::vector<Vec3>& {
} }
/* Convert python tuple to C++ Vec3 object*/
%typemap(typecheck) Vec3 {
// typemap -- %typemap(typecheck) Vec3
$1 = (PySequence_Length($input) >= 3 ? 1 : 0);
}
%typemap(typecheck) const Vec3& {
// typemap -- %typemap(typecheck) Vec3
$1 = (PySequence_Length($input) >= 3 ? 1 : 0);
}
%typemap(out) Vec3 { %typemap(out) Vec3 {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment