eliminated more copy/paste mess between Method and StaticMethod

release/4.3a0
dellaert 2014-05-25 14:35:07 -04:00
parent 61baef3be0
commit 406419317f
4 changed files with 118 additions and 129 deletions

View File

@ -33,13 +33,13 @@ string Argument::matlabClass(const string& delim) const {
string result; string result;
BOOST_FOREACH(const string& ns, namespaces) BOOST_FOREACH(const string& ns, namespaces)
result += ns + delim; result += ns + delim;
if (type=="string" || type=="unsigned char" || type=="char") if (type == "string" || type == "unsigned char" || type == "char")
return result + "char"; return result + "char";
if (type=="Vector" || type=="Matrix") if (type == "Vector" || type == "Matrix")
return result + "double"; return result + "double";
if (type=="int" || type=="size_t") if (type == "int" || type == "size_t")
return result + "numeric"; return result + "numeric";
if (type=="bool") if (type == "bool")
return result + "logical"; return result + "logical";
return result + type; return result + type;
} }
@ -53,7 +53,8 @@ void Argument::matlab_unwrap(FileWriter& file, const string& matlabName) const {
if (is_ptr) if (is_ptr)
// A pointer: emit an "unwrap_shared_ptr" call which returns a pointer // A pointer: emit an "unwrap_shared_ptr" call which returns a pointer
file.oss << "boost::shared_ptr<" << cppType << "> " << name << " = unwrap_shared_ptr< "; file.oss << "boost::shared_ptr<" << cppType << "> " << name
<< " = unwrap_shared_ptr< ";
else if (is_ref) else if (is_ref)
// A reference: emit an "unwrap_shared_ptr" call and de-reference the pointer // A reference: emit an "unwrap_shared_ptr" call and de-reference the pointer
file.oss << cppType << "& " << name << " = *unwrap_shared_ptr< "; file.oss << cppType << "& " << name << " = *unwrap_shared_ptr< ";
@ -66,23 +67,28 @@ void Argument::matlab_unwrap(FileWriter& file, const string& matlabName) const {
file.oss << cppType << " " << name << " = unwrap< "; file.oss << cppType << " " << name << " = unwrap< ";
file.oss << cppType << " >(" << matlabName; file.oss << cppType << " >(" << matlabName;
if (is_ptr || is_ref) file.oss << ", \"ptr_" << matlabUniqueType << "\""; if (is_ptr || is_ref)
file.oss << ", \"ptr_" << matlabUniqueType << "\"";
file.oss << ");" << endl; file.oss << ");" << endl;
} }
/* ************************************************************************* */ /* ************************************************************************* */
string Argument::qualifiedType(const string& delim) const { string Argument::qualifiedType(const string& delim) const {
string result; string result;
BOOST_FOREACH(const string& ns, namespaces) result += ns + delim; BOOST_FOREACH(const string& ns, namespaces)
result += ns + delim;
return result + type; return result + type;
} }
/* ************************************************************************* */ /* ************************************************************************* */
string ArgumentList::types() const { string ArgumentList::types() const {
string str; string str;
bool first=true; bool first = true;
BOOST_FOREACH(Argument arg, *this) { BOOST_FOREACH(Argument arg, *this) {
if (!first) str += ","; str += arg.type; first=false; if (!first)
str += ",";
str += arg.type;
first = false;
} }
return str; return str;
} }
@ -90,16 +96,17 @@ string ArgumentList::types() const {
/* ************************************************************************* */ /* ************************************************************************* */
string ArgumentList::signature() const { string ArgumentList::signature() const {
string sig; string sig;
bool cap=false; bool cap = false;
BOOST_FOREACH(Argument arg, *this) { BOOST_FOREACH(Argument arg, *this) {
BOOST_FOREACH(char ch, arg.type) BOOST_FOREACH(char ch, arg.type)
if(isupper(ch)) { if (isupper(ch)) {
sig += ch; sig += ch;
//If there is a capital letter, we don't want to read it below //If there is a capital letter, we don't want to read it below
cap=true; cap = true;
} }
if(!cap) sig += arg.type[0]; if (!cap)
sig += arg.type[0];
//Reset to default //Reset to default
cap = false; cap = false;
} }
@ -110,9 +117,12 @@ string ArgumentList::signature() const {
/* ************************************************************************* */ /* ************************************************************************* */
string ArgumentList::names() const { string ArgumentList::names() const {
string str; string str;
bool first=true; bool first = true;
BOOST_FOREACH(Argument arg, *this) { BOOST_FOREACH(Argument arg, *this) {
if (!first) str += ","; str += arg.name; first=false; if (!first)
str += ",";
str += arg.name;
first = false;
} }
return str; return str;
} }
@ -123,7 +133,7 @@ void ArgumentList::matlab_unwrap(FileWriter& file, int start) const {
BOOST_FOREACH(Argument arg, *this) { BOOST_FOREACH(Argument arg, *this) {
stringstream buf; stringstream buf;
buf << "in[" << index << "]"; buf << "in[" << index << "]";
arg.matlab_unwrap(file,buf.str()); arg.matlab_unwrap(file, buf.str());
index++; index++;
} }
} }
@ -133,11 +143,44 @@ void ArgumentList::emit_prototype(FileWriter& file, const string& name) const {
file.oss << name << "("; file.oss << name << "(";
bool first = true; bool first = true;
BOOST_FOREACH(Argument arg, *this) { BOOST_FOREACH(Argument arg, *this) {
if (!first) file.oss << ", "; if (!first)
file.oss << ", ";
file.oss << arg.type << " " << arg.name; file.oss << arg.type << " " << arg.name;
first = false; first = false;
} }
file.oss << ")"; file.oss << ")";
} }
/* ************************************************************************* */ /* ************************************************************************* */
void ArgumentList::emit_conditional_call(FileWriter& file,
const ReturnValue& returnVal, const string& wrapperName, int id,
bool staticMethod) const {
// Check nr of arguments
file.oss << "if length(varargin) == " << size();
if (size() > 0)
file.oss << " && ";
// ...and their types
bool first = true;
for (size_t i = 0; i < size(); i++) {
if (!first)
file.oss << " && ";
file.oss << "isa(varargin{" << i + 1 << "},'" << (*this)[i].matlabClass(".")
<< "')";
first = false;
}
file.oss << "\n";
// output call to C++ wrapper
string output;
if (returnVal.isPair)
output = "[ varargout{1} varargout{2} ] = ";
else if (returnVal.category1 == ReturnValue::VOID)
output = "";
else
output = "varargout{1} = ";
file.oss << " " << output << wrapperName << "(" << id;
if (!staticMethod)
file.oss << ", this";
file.oss << ", varargin{:});\n";
}
/* ************************************************************************* */

View File

@ -20,6 +20,7 @@
#pragma once #pragma once
#include "FileWriter.h" #include "FileWriter.h"
#include "ReturnValue.h"
#include <string> #include <string>
#include <vector> #include <vector>
@ -75,6 +76,14 @@ struct ArgumentList: public std::vector<Argument> {
*/ */
void emit_prototype(FileWriter& file, const std::string& name) const; void emit_prototype(FileWriter& file, const std::string& name) const;
/**
* emit conditional MATLAB call (checking arguments first)
* @param file output stream
* @param returnVal the return value
* @param wrapperName of method or function
*/
void emit_conditional_call(FileWriter& file, const ReturnValue& returnVal,
const std::string& wrapperName, int id, bool staticMethod=false) const;
}; };
} // \namespace wrap } // \namespace wrap

View File

@ -39,99 +39,67 @@ void Method::addOverload(bool verbose, bool is_const, const std::string& name,
} }
/* ************************************************************************* */ /* ************************************************************************* */
void Method::proxy_wrapper_fragments(FileWriter& proxyFile, void Method::proxy_wrapper_fragments(FileWriter& file, FileWriter& wrapperFile,
FileWriter& wrapperFile, const string& cppClassName, const string& cppClassName, const std::string& matlabQualName,
const std::string& matlabQualName, const std::string& matlabUniqueName, const std::string& matlabUniqueName, const string& wrapperName,
const string& wrapperName, const TypeAttributesTable& typeAttributes, const TypeAttributesTable& typeAttributes,
vector<string>& functionNames) const { vector<string>& functionNames) const {
// Create function header // Create function header
proxyFile.oss << " function varargout = " << name << "(this, varargin)\n"; file.oss << " function varargout = " << name << "(this, varargin)\n";
// Emit comments for documentation // Emit comments for documentation
string up_name = boost::to_upper_copy(name); string up_name = boost::to_upper_copy(name);
proxyFile.oss << " % " << up_name << " usage: "; file.oss << " % " << up_name << " usage: ";
unsigned int argLCount = 0; unsigned int argLCount = 0;
BOOST_FOREACH(ArgumentList argList, argLists) { BOOST_FOREACH(ArgumentList argList, argLists) {
argList.emit_prototype(proxyFile, name); argList.emit_prototype(file, name);
if (argLCount != argLists.size() - 1) if (argLCount != argLists.size() - 1)
proxyFile.oss << ", "; file.oss << ", ";
else else
proxyFile.oss << " : returns " file.oss << " : returns "
<< returnVals[0].return_type(false, returnVals[0].pair) << endl; << returnVals[0].return_type(false, returnVals[0].pair) << endl;
argLCount++; argLCount++;
} }
// Emit URL to Doxygen page // Emit URL to Doxygen page
proxyFile.oss << " % " file.oss << " % "
<< "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html" << "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html"
<< endl; << endl;
// Document all overloads, if any // Document all overloads, if any
if (argLists.size() > 1) { if (argLists.size() > 1) {
proxyFile.oss << " % " << "" << endl; file.oss << " % " << "" << endl;
proxyFile.oss << " % " << "Method Overloads" << endl; file.oss << " % " << "Method Overloads" << endl;
BOOST_FOREACH(ArgumentList argList, argLists) { BOOST_FOREACH(ArgumentList argList, argLists) {
proxyFile.oss << " % "; file.oss << " % ";
argList.emit_prototype(proxyFile, name); argList.emit_prototype(file, name);
proxyFile.oss << endl; file.oss << endl;
} }
} }
// For all overloads, check the number of arguments // Check arguments for all overloads
for (size_t overload = 0; overload < argLists.size(); ++overload) { for (size_t overload = 0; overload < argLists.size(); ++overload) {
const ArgumentList& args = argLists[overload];
const ReturnValue& returnVal = returnVals[overload];
size_t nrArgs = args.size();
const int id = functionNames.size();
// Output proxy matlab code // Output proxy matlab code
file.oss << " " << (overload == 0 ? "" : "else");
// check for number of arguments... const int id = (int) functionNames.size();
proxyFile.oss << " " << (overload == 0 ? "" : "else") argLists[overload].emit_conditional_call(file, returnVals[overload],
<< "if length(varargin) == " << nrArgs; wrapperName, id);
if (nrArgs > 0)
proxyFile.oss << " && ";
// ...and their types
bool first = true;
for (size_t i = 0; i < nrArgs; i++) {
if (!first)
proxyFile.oss << " && ";
proxyFile.oss << "isa(varargin{" << i + 1 << "},'"
<< args[i].matlabClass(".") << "')";
first = false;
}
proxyFile.oss << "\n";
// output call to C++ wrapper
string output;
if (returnVal.isPair)
output = "[ varargout{1} varargout{2} ] = ";
else if (returnVal.category1 == ReturnValue::VOID)
output = "";
else
output = "varargout{1} = ";
proxyFile.oss << " " << output << wrapperName << "(" << id
<< ", this, varargin{:});\n";
// Output C++ wrapper code // Output C++ wrapper code
const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName,
matlabUniqueName, overload, id, typeAttributes); matlabUniqueName, overload, id, typeAttributes);
// Add to function list // Add to function list
functionNames.push_back(wrapFunctionName); functionNames.push_back(wrapFunctionName);
} }
file.oss << " else\n";
proxyFile.oss << " else\n"; file.oss << " error('Arguments do not match any overload of function "
proxyFile.oss
<< " error('Arguments do not match any overload of function "
<< matlabQualName << "." << name << "');" << endl; << matlabQualName << "." << name << "');" << endl;
file.oss << " end\n";
proxyFile.oss << " end\n"; file.oss << " end\n";
proxyFile.oss << " end\n";
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -39,7 +39,7 @@ void StaticMethod::addOverload(bool verbose, const std::string& name,
} }
/* ************************************************************************* */ /* ************************************************************************* */
void StaticMethod::proxy_wrapper_fragments(FileWriter& proxyFile, void StaticMethod::proxy_wrapper_fragments(FileWriter& file,
FileWriter& wrapperFile, const string& cppClassName, FileWriter& wrapperFile, const string& cppClassName,
const std::string& matlabQualName, const std::string& matlabUniqueName, const std::string& matlabQualName, const std::string& matlabUniqueName,
const string& wrapperName, const TypeAttributesTable& typeAttributes, const string& wrapperName, const TypeAttributesTable& typeAttributes,
@ -48,85 +48,54 @@ void StaticMethod::proxy_wrapper_fragments(FileWriter& proxyFile,
string upperName = name; string upperName = name;
upperName[0] = std::toupper(upperName[0], std::locale()); upperName[0] = std::toupper(upperName[0], std::locale());
proxyFile.oss << " function varargout = " << upperName << "(varargin)\n"; file.oss << " function varargout = " << upperName << "(varargin)\n";
//Comments for documentation //Comments for documentation
string up_name = boost::to_upper_copy(name); string up_name = boost::to_upper_copy(name);
proxyFile.oss << " % " << up_name << " usage: "; file.oss << " % " << up_name << " usage: ";
unsigned int argLCount = 0; unsigned int argLCount = 0;
BOOST_FOREACH(ArgumentList argList, argLists) { BOOST_FOREACH(ArgumentList argList, argLists) {
argList.emit_prototype(proxyFile, name); argList.emit_prototype(file, name);
if (argLCount != argLists.size() - 1) if (argLCount != argLists.size() - 1)
proxyFile.oss << ", "; file.oss << ", ";
else else
proxyFile.oss << " : returns " file.oss << " : returns "
<< returnVals[0].return_type(false, returnVals[0].pair) << endl; << returnVals[0].return_type(false, returnVals[0].pair) << endl;
argLCount++; argLCount++;
} }
proxyFile.oss << " % " file.oss << " % "
<< "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html" << "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html"
<< endl; << endl;
proxyFile.oss << " % " << "" << endl; file.oss << " % " << "" << endl;
proxyFile.oss << " % " << "Usage" << endl; file.oss << " % " << "Usage" << endl;
BOOST_FOREACH(ArgumentList argList, argLists) { BOOST_FOREACH(ArgumentList argList, argLists) {
proxyFile.oss << " % "; file.oss << " % ";
argList.emit_prototype(proxyFile, up_name); argList.emit_prototype(file, up_name);
proxyFile.oss << endl; file.oss << endl;
} }
// Check arguments for all overloads
for (size_t overload = 0; overload < argLists.size(); ++overload) { for (size_t overload = 0; overload < argLists.size(); ++overload) {
const ArgumentList& args = argLists[overload];
const ReturnValue& returnVal = returnVals[overload];
size_t nrArgs = args.size();
const int id = (int) functionNames.size();
// Output proxy matlab code // Output proxy matlab code
file.oss << " " << (overload == 0 ? "" : "else");
// check for number of arguments... const int id = (int) functionNames.size();
proxyFile.oss << " " << (overload == 0 ? "" : "else") argLists[overload].emit_conditional_call(file, returnVals[overload],
<< "if length(varargin) == " << nrArgs; wrapperName, id, true); // last bool is to indicate static !
if (nrArgs > 0)
proxyFile.oss << " && ";
// ...and their types
bool first = true;
for (size_t i = 0; i < nrArgs; i++) {
if (!first)
proxyFile.oss << " && ";
proxyFile.oss << "isa(varargin{" << i + 1 << "},'"
<< args[i].matlabClass(".") << "')";
first = false;
}
proxyFile.oss << "\n";
// output call to C++ wrapper
string output;
if (returnVal.isPair)
output = "[ varargout{1} varargout{2} ] = ";
else if (returnVal.category1 == ReturnValue::VOID)
output = "";
else
output = "varargout{1} = ";
proxyFile.oss << " " << output << wrapperName << "(" << id
<< ", varargin{:});\n";
// Output C++ wrapper code // Output C++ wrapper code
const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName,
matlabUniqueName, (int) overload, id, typeAttributes); matlabUniqueName, (int) overload, id, typeAttributes);
// Add to function list // Add to function list
functionNames.push_back(wrapFunctionName); functionNames.push_back(wrapFunctionName);
} }
file.oss << " else\n";
proxyFile.oss << " else\n"; file.oss << " error('Arguments do not match any overload of function "
proxyFile.oss
<< " error('Arguments do not match any overload of function "
<< matlabQualName << "." << upperName << "');" << endl; << matlabQualName << "." << upperName << "');" << endl;
file.oss << " end\n";
proxyFile.oss << " end\n"; file.oss << " end\n";
proxyFile.oss << " end\n";
} }
/* ************************************************************************* */ /* ************************************************************************* */