From 905aac29f8b80797fe4b2032870df97d13870cf5 Mon Sep 17 00:00:00 2001 From: dellaert Date: Sun, 6 Aug 2017 17:15:47 -0700 Subject: [PATCH] Only define as many return values as needed --- wrap/Method.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/wrap/Method.cpp b/wrap/Method.cpp index ac0cb4f00..8427b8736 100644 --- a/wrap/Method.cpp +++ b/wrap/Method.cpp @@ -156,14 +156,23 @@ void Method::emit_cython_pyx(FileWriter& file, const Class& cls) const { file.oss << " cdef list __params\n"; // Define return values for all possible overloads - vector return_value; + vector return_type; // every overload has a return type, possibly void + map return_value; // we only define one return value for every distinct type + size_t j = 1; for (size_t i = 0; i < nrOverloads(); ++i) { - return_value.push_back("return_value_" + to_string(i)); - if (!returnVals_[i].isVoid()) { - file.oss << " cdef " << returnVals_[i].pyx_returnType() << " " - << return_value[i] << "\n"; + if (returnVals_[i].isVoid()) { + return_type.push_back("void"); + } else { + const string type = returnVals_[i].pyx_returnType(); + return_type.push_back(type); + if (return_value.count(type) == 0) { + const string value = "return_value_" + to_string(j++); + return_value[type] = value; + file.oss << " cdef " << type << " " << value << "\n"; + } } } + for (size_t i = 0; i < nrOverloads(); ++i) { ArgumentList args = argumentList(i); file.oss << " try:\n"; @@ -174,9 +183,11 @@ void Method::emit_cython_pyx(FileWriter& file, const Class& cls) const { string caller = "self." + cls.shared_pxd_obj_in_pyx() + ".get()"; string call = pyx_functionCall(caller, funcName, i); if (!returnVals_[i].isVoid()) { - file.oss << " " << return_value[i] << " = " << call << "\n"; - file.oss << " return " - << returnVals_[i].pyx_casting(return_value[i]) << "\n"; + const string type = return_type[i]; + const string value = return_value[type]; + file.oss << " " << value << " = " << call << "\n"; + file.oss << " return " << returnVals_[i].pyx_casting(value) + << "\n"; } else { file.oss << " " << call << "\n"; file.oss << " return\n";