Refactored emit call

release/4.3a0
dellaert 2014-11-29 20:53:38 +01:00
parent e2ab47b610
commit 0eaabd700b
6 changed files with 47 additions and 43 deletions

View File

@ -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);
}
/* ************************************************************************* */

View File

@ -88,26 +88,12 @@ struct ArgumentList: public std::vector<Argument> {
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<Argument> {
return os;
}
};
} // \namespace wrap

View File

@ -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);
}
/* ************************************************************************* */

View File

@ -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

View File

@ -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

View File

@ -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