From 443b710a8db85f26911162f566ef0f05b303ca3a Mon Sep 17 00:00:00 2001 From: dellaert Date: Wed, 12 Nov 2014 13:31:46 +0100 Subject: [PATCH] Re-factoring ReturnValue --- wrap/Module.cpp | 41 ++++++------- wrap/ReturnValue.cpp | 133 ++++++++++++++++++++++++++++++++++++------- wrap/ReturnValue.h | 26 ++++++--- 3 files changed, 150 insertions(+), 50 deletions(-) diff --git a/wrap/Module.cpp b/wrap/Module.cpp index d75f6e08b..34a79effa 100644 --- a/wrap/Module.cpp +++ b/wrap/Module.cpp @@ -99,10 +99,7 @@ Module::Module(const string& interfacePath, void Module::parseMarkup(const std::string& data) { // these variables will be imperatively updated to gradually build [cls] // The one with postfix 0 are used to reset the variables after parse. - bool isConst, isConst0 = false; - ReturnValue retVal0, retVal; Argument arg0, arg; - ArgumentList args; vector arg_dup; ///keep track of duplicates Constructor constructor0(verbose), constructor(verbose); Deconstructor deconstructor0(verbose), deconstructor(verbose); @@ -210,6 +207,7 @@ void Module::parseMarkup(const std::string& data) { '>'); // NOTE: allows for pointers to all types + ArgumentList args; Rule argument_p = ((basisType_p[assign_a(arg.type.name)] | argEigenType_p | eigenRef_p | classArg_p) >> !ch_p('*')[assign_a(arg.is_ptr,true)] @@ -223,10 +221,6 @@ void Module::parseMarkup(const std::string& data) { (className_p >> '(' >> argumentList_p >> ')' >> ';' >> !comments_p) [push_back_a(constructor.args_list, args)] [clear_a(args)]; - //[assign_a(constructor.args,args)] - //[assign_a(constructor.name,cls.name)] - //[push_back_a(cls.constructors, constructor)] - //[assign_a(constructor,constructor0)]; Rule namespace_ret_p = namespace_name_p[push_back_a(namespaces_return)] >> str_p("::"); @@ -236,6 +230,8 @@ void Module::parseMarkup(const std::string& data) { static const ReturnValue::return_category RETURN_CLASS = ReturnValue::CLASS; static const ReturnValue::return_category RETURN_VOID = ReturnValue::VOID; + // TODO, eliminate copy/paste + ReturnValue retVal0, retVal; Rule returnType1_p = (basisType_p[assign_a(retVal.type1.name)][assign_a(retVal.category1, RETURN_BASIS)]) | ((*namespace_ret_p)[assign_a(retVal.type1.namespaces, namespaces_return)][clear_a(namespaces_return)] @@ -262,6 +258,7 @@ void Module::parseMarkup(const std::string& data) { // gtsam::Values retract(const gtsam::VectorValues& delta) const; string methodName; + bool isConst, isConst0 = false; vector methodInstantiations; Rule method_p = !templateArgValues_p @@ -272,9 +269,9 @@ void Module::parseMarkup(const std::string& data) { [bl::bind(&Method::addOverload, bl::var(cls.methods)[bl::var(methodName)], verbose, bl::var(isConst), bl::var(methodName), bl::var(args), bl::var(retVal))] - [assign_a(isConst,isConst0)] + [assign_a(retVal,retVal0)] [clear_a(args)] - [assign_a(retVal,retVal0)]; + [assign_a(isConst,isConst0)]; Rule staticMethodName_p = lexeme_d[(upper_p | lower_p) >> *(alnum_p | '_')]; @@ -283,18 +280,16 @@ void Module::parseMarkup(const std::string& data) { '(' >> argumentList_p >> ')' >> ';' >> *comments_p) [bl::bind(&StaticMethod::addOverload, bl::var(cls.static_methods)[bl::var(methodName)], - verbose, - bl::var(methodName), - bl::var(args), - bl::var(retVal))] - [clear_a(args)] - [assign_a(retVal,retVal0)]; + verbose, bl::var(methodName), bl::var(args), bl::var(retVal))] + [assign_a(retVal,retVal0)] + [clear_a(args)]; Rule functions_p = constructor_p | method_p | static_method_p; + // parse a full class vector templateInstantiations; Rule class_p = - (str_p("")[assign_a(cls,cls0)]) + eps_p[assign_a(cls,cls0)] >> (!(templateArgValues_p [push_back_a(cls.templateArgs, templateArgName)] [assign_a(templateInstantiations,templateArgValues)] @@ -313,11 +308,12 @@ void Module::parseMarkup(const std::string& data) { [assign_a(cls.deconstructor, deconstructor)] [bl::bind(&handle_possible_template, bl::var(classes), bl::var(cls), bl::var(templateArgName), bl::var(templateInstantiations))] + [clear_a(templateInstantiations)] [assign_a(deconstructor,deconstructor0)] [assign_a(constructor, constructor0)] - [assign_a(cls,cls0)] - [clear_a(templateInstantiations)]; + [assign_a(cls,cls0)]; + // parse a global function Qualified globalFunction; Rule global_function_p = (returnType_p >> staticMethodName_p[assign_a(globalFunction.name)] >> @@ -325,13 +321,10 @@ void Module::parseMarkup(const std::string& data) { [assign_a(globalFunction.namespaces,namespaces)] [bl::bind(&GlobalFunction::addOverload, bl::var(global_functions)[bl::var(globalFunction.name)], - verbose, - bl::var(globalFunction), - bl::var(args), - bl::var(retVal))] + verbose, bl::var(globalFunction), bl::var(args), bl::var(retVal))] + [assign_a(retVal,retVal0)] [clear_a(globalFunction)] - [clear_a(args)] - [assign_a(retVal,retVal0)]; + [clear_a(args)]; Rule include_p = str_p("#include") >> ch_p('<') >> (*(anychar_p - '>'))[push_back_a(includes)] >> ch_p('>'); diff --git a/wrap/ReturnValue.cpp b/wrap/ReturnValue.cpp index 87d49de6a..6da5a65f8 100644 --- a/wrap/ReturnValue.cpp +++ b/wrap/ReturnValue.cpp @@ -19,13 +19,13 @@ 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.name) + + maybe_shared_ptr(add_ptr || type1.isPtr, qualifiedType1("::"), type1.name) + ", " - + maybe_shared_ptr(add_ptr || isPtr2, qualifiedType2("::"), type2.name) + + maybe_shared_ptr(add_ptr || type2.isPtr, qualifiedType2("::"), type2.name) + " >"; return str; } else - return maybe_shared_ptr(add_ptr && isPtr1, + return maybe_shared_ptr(add_ptr && type1.isPtr, (p == arg2) ? qualifiedType2("::") : qualifiedType1("::"), (p == arg2) ? type2.name : type1.name); } @@ -45,6 +45,101 @@ string ReturnValue::qualifiedType2(const string& delim) const { return type2.qualifiedName(delim); } +/* ************************************************************************* */ +void ReturnType::wrap_result(const string& result, FileWriter& file, + const TypeAttributesTable& typeAttributes) const { + string cppType1 = qualifiedType1("::"), matlabType1 = qualifiedType1("."); + 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 (type1.category == ReturnValue::CLASS) { // if we are going to make one + string objCopy, ptrType; + ptrType = "Shared" + type1.name; + const bool isVirtual = typeAttributes.at(cppType1).isVirtual; + if (isVirtual) { + if (type1.isPtr) + objCopy = "pairResult.first"; + else + objCopy = "pairResult.first.clone()"; + } else { + if (type1.isPtr) + objCopy = "pairResult.first"; + else + objCopy = ptrType + "(new " + cppType1 + "(pairResult.first))"; + } + file.oss << " out[0] = wrap_shared_ptr(" << objCopy << ",\"" + << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; + } else if (type1.isPtr) { + file.oss << " Shared" << type1.name << "* ret = new Shared" << type1.name + << "(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) + << " >(pairResult.first);\n"; + + // second return value in pair + if (type2.category == ReturnValue::CLASS) { // if we are going to make one + string objCopy, ptrType; + ptrType = "Shared" + type2.name; + const bool isVirtual = typeAttributes.at(cppType2).isVirtual; + if (isVirtual) { + if (type2.isPtr) + objCopy = "pairResult.second"; + else + objCopy = "pairResult.second.clone()"; + } else { + if (type2.isPtr) + objCopy = "pairResult.second"; + else + objCopy = ptrType + "(new " + cppType2 + "(pairResult.second))"; + } + file.oss << " out[1] = wrap_shared_ptr(" << objCopy << ",\"" + << matlabType2 << "\", " << (isVirtual ? "true" : "false") << ");\n"; + } else if (type2.isPtr) { + file.oss << " Shared" << type2.name << "* ret = new Shared" << type2.name + << "(pairResult.second);" << endl; + file.oss << " out[1] = wrap_shared_ptr(ret,\"" << matlabType2 + << "\");\n"; + } else + file.oss << " out[1] = wrap< " << return_type(true, arg2) + << " >(pairResult.second);\n"; + } else { // Not a pair + + if (type1.category == ReturnValue::CLASS) { + string objCopy, ptrType; + ptrType = "Shared" + type1.name; + const bool isVirtual = typeAttributes.at(cppType1).isVirtual; + if (isVirtual) { + if (type1.isPtr) + objCopy = result; + else + objCopy = result + ".clone()"; + } else { + if (type1.isPtr) + objCopy = result; + else + objCopy = ptrType + "(new " + cppType1 + "(" + result + "))"; + } + file.oss << " out[0] = wrap_shared_ptr(" << objCopy << ",\"" + << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; + } else if (type1.isPtr) { + file.oss << " Shared" << type1.name << "* ret = new Shared" << type1.name + << "(" << result << ");" << endl; + file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 + << "\");\n"; + } else if (matlabType1 != "void") + file.oss << " out[0] = wrap< " << return_type(true, arg1) << " >(" + << result << ");\n"; + } +} + /* ************************************************************************* */ //TODO:Fix this void ReturnValue::wrap_result(const string& result, FileWriter& file, const TypeAttributesTable& typeAttributes) const { @@ -56,69 +151,69 @@ void ReturnValue::wrap_result(const string& result, FileWriter& file, const Type 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 + if (type1.category == ReturnValue::CLASS) { // if we are going to make one string objCopy, ptrType; ptrType = "Shared" + type1.name; const bool isVirtual = typeAttributes.at(cppType1).isVirtual; if(isVirtual) { - if(isPtr1) + if(type1.isPtr) objCopy = "pairResult.first"; else objCopy = "pairResult.first.clone()"; } else { - if(isPtr1) + if(type1.isPtr) objCopy = "pairResult.first"; else objCopy = ptrType + "(new " + cppType1 + "(pairResult.first))"; } file.oss << " out[0] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; - } else if(isPtr1) { + } else if(type1.isPtr) { file.oss << " Shared" << type1.name <<"* ret = new Shared" << type1.name << "(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) << " >(pairResult.first);\n"; // second return value in pair - if (category2 == ReturnValue::CLASS) { // if we are going to make one + if (type2.category == ReturnValue::CLASS) { // if we are going to make one string objCopy, ptrType; ptrType = "Shared" + type2.name; const bool isVirtual = typeAttributes.at(cppType2).isVirtual; if(isVirtual) { - if(isPtr2) + if(type2.isPtr) objCopy = "pairResult.second"; else objCopy = "pairResult.second.clone()"; } else { - if(isPtr2) + if(type2.isPtr) objCopy = "pairResult.second"; else objCopy = ptrType + "(new " + cppType2 + "(pairResult.second))"; } file.oss << " out[1] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType2 << "\", " << (isVirtual ? "true" : "false") << ");\n"; - } else if(isPtr2) { + } else if(type2.isPtr) { file.oss << " Shared" << type2.name <<"* ret = new Shared" << type2.name << "(pairResult.second);" << endl; file.oss << " out[1] = wrap_shared_ptr(ret,\"" << matlabType2 << "\");\n"; } else file.oss << " out[1] = wrap< " << return_type(true,arg2) << " >(pairResult.second);\n"; } else { // Not a pair - if (category1 == ReturnValue::CLASS) { + if (type1.category == ReturnValue::CLASS) { string objCopy, ptrType; ptrType = "Shared" + type1.name; const bool isVirtual = typeAttributes.at(cppType1).isVirtual; if(isVirtual) { - if(isPtr1) + if(type1.isPtr) objCopy = result; else objCopy = result + ".clone()"; } else { - if(isPtr1) + if(type1.isPtr) objCopy = result; else objCopy = ptrType + "(new " + cppType1 + "(" + result + "))"; } file.oss << " out[0] = wrap_shared_ptr(" << objCopy << ",\"" << matlabType1 << "\", " << (isVirtual ? "true" : "false") << ");\n"; - } else if(isPtr1) { + } else if(type1.isPtr) { file.oss << " Shared" << type1.name <<"* ret = new Shared" << type1.name << "(" << result << ");" << endl; file.oss << " out[0] = wrap_shared_ptr(ret,\"" << matlabType1 << "\");\n"; } else if (matlabType1!="void") @@ -130,13 +225,13 @@ void ReturnValue::wrap_result(const string& result, FileWriter& file, const Type void ReturnValue::wrapTypeUnwrap(FileWriter& wrapperFile) const { if(isPair) { - if(category1 == ReturnValue::CLASS) + if(type1.category == ReturnValue::CLASS) wrapperFile.oss << " typedef boost::shared_ptr<" << qualifiedType1("::") << "> Shared" << type1.name << ";"<< endl; - if(category2 == ReturnValue::CLASS) + if(type2.category == ReturnValue::CLASS) wrapperFile.oss << " typedef boost::shared_ptr<" << qualifiedType2("::") << "> Shared" << type2.name << ";"<< endl; } else { - if (category1 == ReturnValue::CLASS) + if (type1.category == ReturnValue::CLASS) wrapperFile.oss << " typedef boost::shared_ptr<" << qualifiedType1("::") << "> Shared" << type1.name << ";"<< endl; } } @@ -145,7 +240,7 @@ void ReturnValue::emit_matlab(FileWriter& file) const { string output; if (isPair) file.oss << "[ varargout{1} varargout{2} ] = "; - else if (category1 != ReturnValue::VOID) + else if (type1.category != ReturnValue::VOID) file.oss << "varargout{1} = "; } /* ************************************************************************* */ diff --git a/wrap/ReturnValue.h b/wrap/ReturnValue.h index 838946d49..953678d9b 100644 --- a/wrap/ReturnValue.h +++ b/wrap/ReturnValue.h @@ -19,21 +19,33 @@ namespace wrap { /** * Encapsulates return value of a method or function */ -struct ReturnValue { +struct ReturnType : Qualified { + + ReturnType(): isPtr(false), category(CLASS) { + } + + ReturnType(const Qualified& q): Qualified(q), isPtr(false), category(CLASS) { + } /// the different supported return value categories typedef enum { CLASS = 1, EIGEN = 2, BASIS = 3, VOID = 4 } return_category; - bool isPtr1, isPtr2, isPair; - return_category category1, category2; - Qualified type1, type2; + bool isPtr; + return_category category; +}; + +/** + * Encapsulates return value of a method or function, possibly a pair + */ +struct ReturnValue { + + bool isPair; + ReturnType type1, type2; /// Constructor - ReturnValue() : - isPtr1(false), isPtr2(false), isPair(false), category1(CLASS), category2( - CLASS) { + ReturnValue() : isPair(false) { } typedef enum {