From 7d4f5a48203a3fdaa6bd2c722db67e7781b6048b Mon Sep 17 00:00:00 2001 From: dellaert Date: Wed, 12 Nov 2014 20:51:47 +0100 Subject: [PATCH] Make explicit whether wrapper or proxy is written to... --- wrap/Argument.cpp | 31 +++++++++--------- wrap/Argument.h | 12 +++---- wrap/Method.cpp | 78 ++++++++++++++++++++++---------------------- wrap/Method.h | 21 ++++++------ wrap/ReturnValue.cpp | 30 ++++++++--------- 5 files changed, 86 insertions(+), 86 deletions(-) diff --git a/wrap/Argument.cpp b/wrap/Argument.cpp index 6e9ac5514..cc235207a 100644 --- a/wrap/Argument.cpp +++ b/wrap/Argument.cpp @@ -156,36 +156,37 @@ void ArgumentList::emit_prototype(FileWriter& file, const string& name) const { file.oss << ")"; } /* ************************************************************************* */ -void ArgumentList::emit_call(FileWriter& file, const ReturnValue& returnVal, - const string& wrapperName, int id, bool staticMethod) const { - returnVal.emit_matlab(file); - file.oss << wrapperName << "(" << id; +void ArgumentList::emit_call(FileWriter& proxyFile, + const ReturnValue& returnVal, const string& wrapperName, int id, + bool staticMethod) const { + returnVal.emit_matlab(proxyFile); + proxyFile.oss << wrapperName << "(" << id; if (!staticMethod) - file.oss << ", this"; - file.oss << ", varargin{:});\n"; + proxyFile.oss << ", this"; + proxyFile.oss << ", varargin{:});\n"; } /* ************************************************************************* */ -void ArgumentList::emit_conditional_call(FileWriter& file, +void ArgumentList::emit_conditional_call(FileWriter& proxyFile, const ReturnValue& returnVal, const string& wrapperName, int id, bool staticMethod) const { // Check nr of arguments - file.oss << "if length(varargin) == " << size(); + proxyFile.oss << "if length(varargin) == " << size(); if (size() > 0) - file.oss << " && "; + proxyFile.oss << " && "; // ...and their type.names bool first = true; for (size_t i = 0; i < size(); i++) { if (!first) - file.oss << " && "; - file.oss << "isa(varargin{" << i + 1 << "},'" << (*this)[i].matlabClass(".") - << "')"; + proxyFile.oss << " && "; + proxyFile.oss << "isa(varargin{" << i + 1 << "},'" + << (*this)[i].matlabClass(".") << "')"; first = false; } - file.oss << "\n"; + proxyFile.oss << "\n"; // output call to C++ wrapper - file.oss << " "; - emit_call(file, returnVal, wrapperName, id, staticMethod); + proxyFile.oss << " "; + emit_call(proxyFile, returnVal, wrapperName, id, staticMethod); } /* ************************************************************************* */ diff --git a/wrap/Argument.h b/wrap/Argument.h index 73bc66929..8c0bb9a33 100644 --- a/wrap/Argument.h +++ b/wrap/Argument.h @@ -77,23 +77,23 @@ struct ArgumentList: public std::vector { void emit_prototype(FileWriter& file, const std::string& name) const; /** - * emit emit MATLAB call to wrapper - * @param file output stream + * emit emit MATLAB call to proxy + * @param proxyFile output stream * @param returnVal the return value * @param wrapperName of method or function * @param staticMethod flag to emit "this" in call */ - void emit_call(FileWriter& file, const ReturnValue& returnVal, + void emit_call(FileWriter& proxyFile, const ReturnValue& returnVal, const std::string& wrapperName, int id, bool staticMethod = false) const; /** - * emit conditional MATLAB call to wrapper (checking arguments first) - * @param file output stream + * emit conditional MATLAB call to proxy (checking arguments first) + * @param proxyFile output stream * @param returnVal the return value * @param wrapperName of method or function * @param staticMethod flag to emit "this" in call */ - void emit_conditional_call(FileWriter& file, const ReturnValue& returnVal, + void emit_conditional_call(FileWriter& proxyFile, const ReturnValue& returnVal, const std::string& wrapperName, int id, bool staticMethod = false) const; }; diff --git a/wrap/Method.cpp b/wrap/Method.cpp index 933d78858..8f18c5f94 100644 --- a/wrap/Method.cpp +++ b/wrap/Method.cpp @@ -39,51 +39,51 @@ void Method::addOverload(bool verbose, bool is_const, const std::string& name, } /* ************************************************************************* */ -void Method::proxy_wrapper_fragments(FileWriter& file, FileWriter& wrapperFile, - const string& cppClassName, const std::string& matlabQualName, - const std::string& matlabUniqueName, const string& wrapperName, - const TypeAttributesTable& typeAttributes, +void Method::proxy_wrapper_fragments(FileWriter& proxyFile, + FileWriter& wrapperFile, const string& cppClassName, + const std::string& matlabQualName, const std::string& matlabUniqueName, + const string& wrapperName, const TypeAttributesTable& typeAttributes, vector& functionNames) const { // Create function header - file.oss << " function varargout = " << name << "(this, varargin)\n"; + proxyFile.oss << " function varargout = " << name << "(this, varargin)\n"; // Emit comments for documentation string up_name = boost::to_upper_copy(name); - file.oss << " % " << up_name << " usage: "; + proxyFile.oss << " % " << up_name << " usage: "; unsigned int argLCount = 0; BOOST_FOREACH(ArgumentList argList, argLists) { - argList.emit_prototype(file, name); + argList.emit_prototype(proxyFile, name); if (argLCount != argLists.size() - 1) - file.oss << ", "; + proxyFile.oss << ", "; else - file.oss << " : returns " - << returnVals[0].return_type(false) << endl; + proxyFile.oss << " : returns " << returnVals[0].return_type(false) + << endl; argLCount++; } // Emit URL to Doxygen page - file.oss << " % " + proxyFile.oss << " % " << "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html" << endl; // Document all overloads, if any if (argLists.size() > 1) { - file.oss << " % " << "" << endl; - file.oss << " % " << "Method Overloads" << endl; + proxyFile.oss << " % " << "" << endl; + proxyFile.oss << " % " << "Method Overloads" << endl; BOOST_FOREACH(ArgumentList argList, argLists) { - file.oss << " % "; - argList.emit_prototype(file, name); - file.oss << endl; + proxyFile.oss << " % "; + argList.emit_prototype(proxyFile, name); + proxyFile.oss << endl; } } // Handle special case of single overload with all numeric arguments if (argLists.size() == 1 && argLists[0].allScalar()) { // Output proxy matlab code - file.oss << " "; + proxyFile.oss << " "; const int id = (int) functionNames.size(); - argLists[0].emit_call(file, returnVals[0], wrapperName, id); + argLists[0].emit_call(proxyFile, returnVals[0], wrapperName, id); // Output C++ wrapper code const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, @@ -96,9 +96,9 @@ void Method::proxy_wrapper_fragments(FileWriter& file, FileWriter& wrapperFile, for (size_t overload = 0; overload < argLists.size(); ++overload) { // Output proxy matlab code - file.oss << " " << (overload == 0 ? "" : "else"); + proxyFile.oss << " " << (overload == 0 ? "" : "else"); const int id = (int) functionNames.size(); - argLists[overload].emit_conditional_call(file, returnVals[overload], + argLists[overload].emit_conditional_call(proxyFile, returnVals[overload], wrapperName, id); // Output C++ wrapper code @@ -108,20 +108,20 @@ void Method::proxy_wrapper_fragments(FileWriter& file, FileWriter& wrapperFile, // Add to function list functionNames.push_back(wrapFunctionName); } - file.oss << " else\n"; - file.oss + proxyFile.oss << " else\n"; + proxyFile.oss << " error('Arguments do not match any overload of function " << matlabQualName << "." << name << "');" << endl; - file.oss << " end\n"; + proxyFile.oss << " end\n"; } - file.oss << " end\n"; + proxyFile.oss << " end\n"; } /* ************************************************************************* */ -string Method::wrapper_fragment(FileWriter& file, const string& cppClassName, - const string& matlabUniqueName, int overload, int id, - const TypeAttributesTable& typeAttributes) const { +string Method::wrapper_fragment(FileWriter& wrapperFile, + const string& cppClassName, const string& matlabUniqueName, int overload, + int id, const TypeAttributesTable& typeAttributes) const { // generate code @@ -132,39 +132,39 @@ string Method::wrapper_fragment(FileWriter& file, const string& cppClassName, const ReturnValue& returnVal = returnVals[overload]; // call - file.oss << "void " << wrapFunctionName + wrapperFile.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; // start - file.oss << "{\n"; + wrapperFile.oss << "{\n"; - returnVal.wrapTypeUnwrap(file); + returnVal.wrapTypeUnwrap(wrapperFile); - file.oss << " typedef boost::shared_ptr<" << cppClassName << "> Shared;" - << endl; + wrapperFile.oss << " typedef boost::shared_ptr<" << cppClassName + << "> Shared;" << endl; // check arguments // extra argument obj -> nargin-1 is passed ! // example: checkArguments("equals",nargout,nargin-1,2); - file.oss << " checkArguments(\"" << name << "\",nargout,nargin-1," + wrapperFile.oss << " checkArguments(\"" << name << "\",nargout,nargin-1," << args.size() << ");\n"; // get class pointer // example: shared_ptr = unwrap_shared_ptr< Test >(in[0], "Test"); - file.oss << " Shared obj = unwrap_shared_ptr<" << cppClassName + wrapperFile.oss << " Shared obj = unwrap_shared_ptr<" << cppClassName << ">(in[0], \"ptr_" << matlabUniqueName << "\");" << endl; // unwrap arguments, see Argument.cpp - args.matlab_unwrap(file, 1); + args.matlab_unwrap(wrapperFile, 1); // call method and wrap result // example: out[0]=wrap(self->return_field(t)); if (returnVal.type1.name != "void") - returnVal.wrap_result("obj->" + name + "(" + args.names() + ")", file, - typeAttributes); + returnVal.wrap_result("obj->" + name + "(" + args.names() + ")", + wrapperFile, typeAttributes); else - file.oss << " obj->" + name + "(" + args.names() + ");\n"; + wrapperFile.oss << " obj->" + name + "(" + args.names() + ");\n"; // finish - file.oss << "}\n"; + wrapperFile.oss << "}\n"; return wrapFunctionName; } diff --git a/wrap/Method.h b/wrap/Method.h index 9926a5179..fa512d874 100644 --- a/wrap/Method.h +++ b/wrap/Method.h @@ -32,7 +32,8 @@ struct Method { /// Constructor creates empty object Method(bool verbose = true) : - verbose_(verbose), is_const_(false) {} + verbose_(verbose), is_const_(false) { + } // Then the instance variables are set directly by the Module constructor bool verbose_; @@ -45,22 +46,20 @@ struct Method { // with those in rhs, but in subsequent calls it adds additional argument // lists as function overloads. void addOverload(bool verbose, bool is_const, const std::string& name, - const ArgumentList& args, const ReturnValue& retVal); + const ArgumentList& args, const ReturnValue& retVal); // MATLAB code generation // classPath is class directory, e.g., ../matlab/@Point2 void proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, - const std::string& cppClassName, const std::string& matlabQualName, const std::string& matlabUniqueName, - const std::string& wrapperName, const TypeAttributesTable& typeAttributes, - std::vector& functionNames) const; + const std::string& cppClassName, const std::string& matlabQualName, + const std::string& matlabUniqueName, const std::string& wrapperName, + const TypeAttributesTable& typeAttributes, + std::vector& functionNames) const; private: - std::string wrapper_fragment(FileWriter& file, - const std::string& cppClassName, - const std::string& matlabUniqueName, - int overload, - int id, - const TypeAttributesTable& typeAttributes) const; ///< cpp wrapper + std::string wrapper_fragment(FileWriter& wrapperFile, + const std::string& cppClassName, const std::string& matlabUniqueName, + int overload, int id, const TypeAttributesTable& typeAttributes) const; ///< cpp wrapper }; } // \namespace wrap diff --git a/wrap/ReturnValue.cpp b/wrap/ReturnValue.cpp index cd8273731..5287410e0 100644 --- a/wrap/ReturnValue.cpp +++ b/wrap/ReturnValue.cpp @@ -7,10 +7,10 @@ * @author Richard Roberts */ -#include - #include "ReturnValue.h" #include "utilities.h" +#include +#include using namespace std; using namespace wrap; @@ -52,9 +52,9 @@ void ReturnType::wrap_result(const string& out, const string& result, } /* ************************************************************************* */ -void ReturnType::wrapTypeUnwrap(FileWriter& file) const { +void ReturnType::wrapTypeUnwrap(FileWriter& wrapperFile) const { if (category == CLASS) - file.oss << " typedef boost::shared_ptr<" << qualifiedName("::") + wrapperFile.oss << " typedef boost::shared_ptr<" << qualifiedName("::") << "> Shared" << name << ";" << endl; } @@ -72,33 +72,33 @@ string ReturnValue::matlab_returnType() const { } /* ************************************************************************* */ -void ReturnValue::wrap_result(const string& result, FileWriter& file, +void ReturnValue::wrap_result(const string& result, FileWriter& wrapperFile, const TypeAttributesTable& typeAttributes) const { if (isPair) { // For a pair, store the returned pair so we do not evaluate the function twice - file.oss << " " << return_type(true) << " pairResult = " << result + wrapperFile.oss << " " << return_type(true) << " pairResult = " << result << ";\n"; - type1.wrap_result(" out[0]", "pairResult.first", file, typeAttributes); - type2.wrap_result(" out[1]", "pairResult.second", file, typeAttributes); + type1.wrap_result(" out[0]", "pairResult.first", wrapperFile, typeAttributes); + type2.wrap_result(" out[1]", "pairResult.second", wrapperFile, typeAttributes); } else { // Not a pair - type1.wrap_result(" out[0]", result, file, typeAttributes); + type1.wrap_result(" out[0]", result, wrapperFile, typeAttributes); } } /* ************************************************************************* */ -void ReturnValue::wrapTypeUnwrap(FileWriter& file) const { - type1.wrapTypeUnwrap(file); +void ReturnValue::wrapTypeUnwrap(FileWriter& wrapperFile) const { + type1.wrapTypeUnwrap(wrapperFile); if (isPair) - type2.wrapTypeUnwrap(file); + type2.wrapTypeUnwrap(wrapperFile); } /* ************************************************************************* */ -void ReturnValue::emit_matlab(FileWriter& file) const { +void ReturnValue::emit_matlab(FileWriter& proxyFile) const { string output; if (isPair) - file.oss << "[ varargout{1} varargout{2} ] = "; + proxyFile.oss << "[ varargout{1} varargout{2} ] = "; else if (type1.category != ReturnType::VOID) - file.oss << "varargout{1} = "; + proxyFile.oss << "varargout{1} = "; } /* ************************************************************************* */