From 0eaabd700b3354dd837b4b3d295e1a58af993677 Mon Sep 17 00:00:00 2001 From: dellaert Date: Sat, 29 Nov 2014 20:53:38 +0100 Subject: [PATCH] Refactored emit call --- wrap/Argument.cpp | 19 ++----------------- wrap/Argument.h | 21 ++++----------------- wrap/Function.cpp | 32 +++++++++++++++++++++++++++----- wrap/Function.h | 10 ++++++++++ wrap/GlobalFunction.cpp | 3 ++- wrap/MethodBase.cpp | 5 ++--- 6 files changed, 47 insertions(+), 43 deletions(-) diff --git a/wrap/Argument.cpp b/wrap/Argument.cpp index 4989afb0d..a3c0987d8 100644 --- a/wrap/Argument.cpp +++ b/wrap/Argument.cpp @@ -175,20 +175,9 @@ void ArgumentList::emit_prototype(FileWriter& file, const string& name) const { } file.oss << ")"; } + /* ************************************************************************* */ -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) - proxyFile.oss << ", this"; - proxyFile.oss << ", varargin{:});\n"; -} -/* ************************************************************************* */ -void ArgumentList::emit_conditional_call(FileWriter& proxyFile, - const ReturnValue& returnVal, const string& wrapperName, int id, - bool staticMethod) const { +void ArgumentList::proxy_check_arguments(FileWriter& proxyFile) const { // Check nr of arguments proxyFile.oss << "if length(varargin) == " << size(); if (size() > 0) @@ -203,10 +192,6 @@ void ArgumentList::emit_conditional_call(FileWriter& proxyFile, first = false; } proxyFile.oss << "\n"; - - // output call to C++ wrapper - proxyFile.oss << " "; - emit_call(proxyFile, returnVal, wrapperName, id, staticMethod); } /* ************************************************************************* */ diff --git a/wrap/Argument.h b/wrap/Argument.h index 3d8d7288f..f207e0766 100644 --- a/wrap/Argument.h +++ b/wrap/Argument.h @@ -88,26 +88,12 @@ struct ArgumentList: public std::vector { void emit_prototype(FileWriter& file, const std::string& name) const; /** - * emit emit MATLAB call to proxy + * emit checking arguments to MATLAB 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& proxyFile, const ReturnValue& returnVal, - const std::string& wrapperName, int id, bool staticMethod = false) const; - - /** - * 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& proxyFile, - const ReturnValue& returnVal, const std::string& wrapperName, int id, - bool staticMethod = false) const; + void proxy_check_arguments(FileWriter& proxyFile) const; + /// Output stream operator friend std::ostream& operator<<(std::ostream& os, const ArgumentList& argList) { os << "("; @@ -120,6 +106,7 @@ struct ArgumentList: public std::vector { return os; } + }; } // \namespace wrap diff --git a/wrap/Function.cpp b/wrap/Function.cpp index 6faa70fb9..29165c64b 100644 --- a/wrap/Function.cpp +++ b/wrap/Function.cpp @@ -29,12 +29,11 @@ using namespace std; using namespace wrap; /* ************************************************************************* */ -bool Function::initializeOrCheck(const std::string& name, - const Qualified& instName, bool verbose) { +bool Function::initializeOrCheck(const string& name, const Qualified& instName, + bool verbose) { if (name.empty()) - throw std::runtime_error( - "Function::initializeOrCheck called with empty name"); + throw runtime_error("Function::initializeOrCheck called with empty name"); // Check if this overload is give to the correct method if (name_.empty()) { @@ -44,7 +43,7 @@ bool Function::initializeOrCheck(const std::string& name, return true; } else { if (name_ != name || templateArgValue_ != instName || verbose_ != verbose) - throw std::runtime_error( + throw runtime_error( "Function::initializeOrCheck called with different arguments: with name " + name + " instead of expected " + name_ + ", or with template argument " + instName.qualifiedName(":") @@ -55,3 +54,26 @@ bool Function::initializeOrCheck(const std::string& name, } /* ************************************************************************* */ +void Function::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) + proxyFile.oss << ", this"; + proxyFile.oss << ", varargin{:});\n"; +} + +/* ************************************************************************* */ +void Function::emit_conditional_call(FileWriter& proxyFile, + const ReturnValue& returnVal, const ArgumentList& args, + const string& wrapperName, int id, bool staticMethod) const { + + // Check all arguments + args.proxy_check_arguments(proxyFile); + + // output call to C++ wrapper + proxyFile.oss << " "; + emit_call(proxyFile, returnVal, wrapperName, id, staticMethod); +} + +/* ************************************************************************* */ diff --git a/wrap/Function.h b/wrap/Function.h index 49a26bd8d..388e6568b 100644 --- a/wrap/Function.h +++ b/wrap/Function.h @@ -50,6 +50,16 @@ public: else return name_ + templateArgValue_.name; } + + /// Emit function call to MATLAB (no argument checking) + void emit_call(FileWriter& proxyFile, const ReturnValue& returnVal, + const std::string& wrapperName, int id, bool staticMethod) const; + + /// Emit checking arguments and function call to MATLAB + void emit_conditional_call(FileWriter& proxyFile, + const ReturnValue& returnVal, const ArgumentList& args, + const std::string& wrapperName, int id, bool staticMethod) const; + }; } // \namespace wrap diff --git a/wrap/GlobalFunction.cpp b/wrap/GlobalFunction.cpp index e843481a1..18f5c5f6c 100644 --- a/wrap/GlobalFunction.cpp +++ b/wrap/GlobalFunction.cpp @@ -80,7 +80,8 @@ void GlobalFunction::generateSingleFunction(const string& toolboxPath, // Output proxy matlab code mfunctionFile.oss << " " << (i == 0 ? "" : "else"); - args.emit_conditional_call(mfunctionFile, returnVal, wrapperName, id, true); // true omits "this" + emit_conditional_call(mfunctionFile, returnVal, args, wrapperName, id, + true); // true omits "this" // Output C++ wrapper code diff --git a/wrap/MethodBase.cpp b/wrap/MethodBase.cpp index a63b95b90..124963d9a 100644 --- a/wrap/MethodBase.cpp +++ b/wrap/MethodBase.cpp @@ -55,8 +55,7 @@ void MethodBase::proxy_wrapper_fragments(FileWriter& proxyFile, // TODO: document why is it OK to not check arguments in this case proxyFile.oss << " "; const int id = (int) functionNames.size(); - argumentList(0).emit_call(proxyFile, returnValue(0), wrapperName, id, - isStatic()); + emit_call(proxyFile, returnValue(0), wrapperName, id, isStatic()); // Output C++ wrapper code const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, @@ -71,7 +70,7 @@ void MethodBase::proxy_wrapper_fragments(FileWriter& proxyFile, // Output proxy matlab code proxyFile.oss << " " << (i == 0 ? "" : "else"); const int id = (int) functionNames.size(); - argumentList(i).emit_conditional_call(proxyFile, returnValue(i), + emit_conditional_call(proxyFile, returnValue(i), argumentList(i), wrapperName, id, isStatic()); // Output C++ wrapper code