diff --git a/wrap/ReturnValue.cpp b/wrap/ReturnValue.cpp index 37e6b15ff..4f5a030cc 100644 --- a/wrap/ReturnValue.cpp +++ b/wrap/ReturnValue.cpp @@ -19,8 +19,8 @@ using namespace wrap; string ReturnValue::return_type(bool add_ptr, pairing p) const { if (p==pair && isPair) { string str = "pair< " + - maybe_shared_ptr(add_ptr && isPtr1, qualifiedType1("::"), type1) + ", " + - maybe_shared_ptr(add_ptr && isPtr2, qualifiedType2("::"), type2) + " >"; + maybe_shared_ptr(add_ptr || isPtr1, qualifiedType1("::"), type1) + ", " + + maybe_shared_ptr(add_ptr || isPtr2, qualifiedType2("::"), type2) + " >"; return str; } else return maybe_shared_ptr(add_ptr && isPtr1, (p==arg2)? qualifiedType2("::") : qualifiedType1("::"), (p==arg2)? type2 : type1); @@ -52,6 +52,9 @@ void ReturnValue::wrap_result(const string& result, FileWriter& file, const Type string cppType2 = qualifiedType2("::"), matlabType2 = qualifiedType2("."); if (isPair) { + // For a pair, store the returned pair so we do not evaluate the function twice + file.oss << " " << return_type(false, pair) << " pairResult = " << result << ";\n"; + // first return value in pair if (category1 == ReturnValue::CLASS) { // if we are going to make one string objCopy, ptrType; @@ -59,21 +62,21 @@ void ReturnValue::wrap_result(const string& result, FileWriter& file, const Type const bool isVirtual = typeAttributes.at(cppType1).isVirtual; if(isVirtual) { if(isPtr1) - objCopy = result + ".first"; + objCopy = "pairResult.first"; else - objCopy = result + ".first.clone()"; + objCopy = "pairResult.first.clone()"; } else { if(isPtr1) - objCopy = result + ".first"; + objCopy = "pairResult.first"; else - objCopy = ptrType + "(new " + cppType1 + "(" + result + ".first))"; + objCopy = ptrType + "(new " + cppType1 + "(pairResult.first))"; } file.oss << " out[0] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; } else if(isPtr1) { - file.oss << " Shared" << type1 <<"* ret = new Shared" << type1 << "(" << result << ".first);" << endl; + file.oss << " Shared" << type1 <<"* ret = new Shared" << type1 << "(pairResult.first);" << endl; file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\", false);\n"; } else // if basis type - file.oss << " out[0] = wrap< " << return_type(true,arg1) << " >(" << result << ".first);\n"; + file.oss << " out[0] = wrap< " << return_type(true,arg1) << " >(pairResult.first);\n"; // second return value in pair if (category2 == ReturnValue::CLASS) { // if we are going to make one @@ -82,21 +85,21 @@ void ReturnValue::wrap_result(const string& result, FileWriter& file, const Type const bool isVirtual = typeAttributes.at(cppType2).isVirtual; if(isVirtual) { if(isPtr2) - objCopy = result + ".second"; + objCopy = "pairResult.second"; else - objCopy = result + ".second.clone()"; + objCopy = "pairResult.second.clone()"; } else { if(isPtr2) - objCopy = result + ".second"; + objCopy = "pairResult.second"; else - objCopy = ptrType + "(new " + cppType2 + "(" + result + ".second))"; + objCopy = ptrType + "(new " + cppType2 + "(pairResult.second))"; } - file.oss << " out[0] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType2 << "\", " << (isVirtual ? "true" : "false") << ");\n"; + file.oss << " out[1] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType2 << "\", " << (isVirtual ? "true" : "false") << ");\n"; } else if(isPtr2) { - file.oss << " Shared" << type2 <<"* ret = new Shared" << type2 << "(" << result << ".second);" << endl; + file.oss << " Shared" << type2 <<"* ret = new Shared" << type2 << "(pairResult.second);" << endl; file.oss << " out[1] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n"; } else - file.oss << " out[1] = wrap< " << return_type(true,arg2) << " >(" << result << ".second);\n"; + file.oss << " out[1] = wrap< " << return_type(true,arg2) << " >(pairResult.second);\n"; } else if (category1 == ReturnValue::CLASS){ string objCopy, ptrType;