| 181 | |
| 182 | |
| 183 | PyObject* PyAlgorithm::compute(PyAlgorithm* self, PyObject* args) { |
| 184 | E_DEBUG(EPyBindings, PY_ALGONAME << "::compute()"); |
| 185 | |
| 186 | // parse the arguments into separate python objects |
| 187 | vector<PyObject*> arg_list = unpack(args); |
| 188 | |
| 189 | int nInputs = self->algo->inputs().size(); |
| 190 | vector<string> inputNames = self->algo->inputNames(); |
| 191 | vector<const type_info*> inputTypes = self->algo->inputTypes(); |
| 192 | |
| 193 | // check the correct number of arguments has been given to the function call |
| 194 | if (int(arg_list.size()) != nInputs) { |
| 195 | ostringstream msg; |
| 196 | msg << self->algo->name() << ".compute has " << nInputs << " inputs, " << arg_list.size() << " given"; |
| 197 | PyErr_SetString(PyExc_RuntimeError, msg.str().c_str()); |
| 198 | return NULL; |
| 199 | } |
| 200 | |
| 201 | // bind the inputs and outputs |
| 202 | |
| 203 | // parse all inputs given by the python interpreter to the corresponding |
| 204 | // C++ variables and assign them to the algorithm's input ports |
| 205 | E_DEBUG_NONL(EPyBindings, PY_ALGONAME << ": binding inputs..."); |
| 206 | |
| 207 | // parse each python obj to a Cpp pointer and set appropriate input |
| 208 | vector<void*> givenInputs(nInputs); |
| 209 | vector<Edt> givenInputTypes(nInputs); |
| 210 | |
| 211 | for (int i=0; i<nInputs; ++i) { |
| 212 | InputBase& port = self->algo->input(inputNames[i]); |
| 213 | Edt tp = typeInfoToEdt(*inputTypes[i]); |
| 214 | givenInputTypes[i] = tp; |
| 215 | |
| 216 | #define SET_PORT_COPY(tname, type) \ |
| 217 | givenInputs[i] = tname::fromPythonCopy(arg_list[i]); \ |
| 218 | port.set(*(type*)givenInputs[i]); \ |
| 219 | break; |
| 220 | |
| 221 | #define SET_PORT_REF(tname, type) \ |
| 222 | givenInputs[i] = tname::fromPythonRef(arg_list[i]); \ |
| 223 | port.set(*(type*)givenInputs[i]); \ |
| 224 | break; |
| 225 | |
| 226 | try { |
| 227 | switch (tp) { |
| 228 | case REAL: SET_PORT_COPY(PyReal, Real); |
| 229 | case VECTOR_STRING: SET_PORT_COPY(VectorString, vector<string>); |
| 230 | case STRING: SET_PORT_COPY(String, string); |
| 231 | case BOOL: SET_PORT_COPY(Boolean, bool); |
| 232 | case INTEGER: SET_PORT_COPY(Integer, int); |
| 233 | case VECTOR_VECTOR_REAL: SET_PORT_COPY(VectorVectorReal, vector<vector<Real> >); |
| 234 | case VECTOR_VECTOR_STRING: SET_PORT_COPY(VectorVectorString, vector<vector<string> >); |
| 235 | case VECTOR_STEREOSAMPLE: SET_PORT_COPY(VectorStereoSample, vector<StereoSample>); |
| 236 | case MATRIX_REAL: SET_PORT_COPY(MatrixReal, TNT::Array2D<Real>); |
| 237 | |
| 238 | case POOL: SET_PORT_REF(PyPool, Pool); |
| 239 | case VECTOR_REAL: SET_PORT_REF(VectorReal, vector<Real>); |
| 240 | case VECTOR_INTEGER: SET_PORT_REF(VectorInteger, vector<int>); |