Standard BORG formatting

release/4.3a0
dellaert 2014-05-25 12:06:34 -04:00
parent 1762825c28
commit 02c3fe9516
1 changed files with 88 additions and 80 deletions

View File

@ -38,57 +38,54 @@ void Method::addOverload(bool verbose, bool is_const, const std::string& name,
} }
/* ************************************************************************* */ /* ************************************************************************* */
void Method::proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
const string& cppClassName, FileWriter& wrapperFile, const string& cppClassName,
const std::string& matlabQualName, const std::string& matlabQualName, const std::string& matlabUniqueName,
const std::string& matlabUniqueName, const string& wrapperName, const TypeAttributesTable& typeAttributes,
const string& wrapperName, vector<string>& functionNames) const {
const TypeAttributesTable& typeAttributes,
vector<string>& functionNames) const {
proxyFile.oss << " function varargout = " << name << "(this, varargin)\n"; proxyFile.oss << " function varargout = " << name << "(this, 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:"; proxyFile.oss << " % " << up_name << " usage:";
unsigned int argLCount = 0; unsigned int argLCount = 0;
BOOST_FOREACH(ArgumentList argList, argLists) BOOST_FOREACH(ArgumentList argList, argLists) {
{ proxyFile.oss << " " << name << "(";
proxyFile.oss << " " << name << "("; unsigned int i = 0;
unsigned int i = 0; BOOST_FOREACH(const Argument& arg, argList) {
BOOST_FOREACH(const Argument& arg, argList) if (i != argList.size() - 1)
{ proxyFile.oss << arg.type << " " << arg.name << ", ";
if(i != argList.size()-1) else
proxyFile.oss << arg.type << " " << arg.name << ", "; proxyFile.oss << arg.type << " " << arg.name;
else i++;
proxyFile.oss << arg.type << " " << arg.name;
i++;
}
if(argLCount != argLists.size()-1)
proxyFile.oss << "), ";
else
proxyFile.oss << ") : returns " << returnVals[0].return_type(false, returnVals[0].pair) << endl;
argLCount++;
} }
if (argLCount != argLists.size() - 1)
proxyFile.oss << "), ";
else
proxyFile.oss << ") : returns "
<< returnVals[0].return_type(false, returnVals[0].pair) << endl;
argLCount++;
}
proxyFile.oss << " % " << "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html" << endl; proxyFile.oss << " % "
<< "Doxygen can be found at http://research.cc.gatech.edu/borg/sites/edu.borg/html/index.html"
<< endl;
proxyFile.oss << " % " << "" << endl; proxyFile.oss << " % " << "" << endl;
proxyFile.oss << " % " << "Method Overloads" << endl; proxyFile.oss << " % " << "Method Overloads" << endl;
BOOST_FOREACH(ArgumentList argList, argLists) BOOST_FOREACH(ArgumentList argList, argLists) {
{ proxyFile.oss << " % " << name << "(";
proxyFile.oss << " % " << name << "("; unsigned int i = 0;
unsigned int i = 0; BOOST_FOREACH(const Argument& arg, argList) {
BOOST_FOREACH(const Argument& arg, argList) if (i != argList.size() - 1)
{ proxyFile.oss << arg.type << " " << arg.name << ", ";
if(i != argList.size()-1) else
proxyFile.oss << arg.type << " " << arg.name << ", "; proxyFile.oss << arg.type << " " << arg.name;
else i++;
proxyFile.oss << arg.type << " " << arg.name;
i++;
}
proxyFile.oss << ")" << endl;
} }
proxyFile.oss << ")" << endl;
}
for(size_t overload = 0; overload < argLists.size(); ++overload) { for (size_t overload = 0; overload < argLists.size(); ++overload) {
const ArgumentList& args = argLists[overload]; const ArgumentList& args = argLists[overload];
const ReturnValue& returnVal = returnVals[overload]; const ReturnValue& returnVal = returnVals[overload];
size_t nrArgs = args.size(); size_t nrArgs = args.size();
@ -98,95 +95,106 @@ void Method::proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperF
// Output proxy matlab code // Output proxy matlab code
// check for number of arguments... // check for number of arguments...
proxyFile.oss << " " << (overload==0?"":"else") << "if length(varargin) == " << nrArgs; proxyFile.oss << " " << (overload == 0 ? "" : "else")
if (nrArgs>0) proxyFile.oss << " && "; << "if length(varargin) == " << nrArgs;
if (nrArgs > 0)
proxyFile.oss << " && ";
// ...and their types // ...and their types
bool first = true; bool first = true;
for(size_t i=0;i<nrArgs;i++) { for (size_t i = 0; i < nrArgs; i++) {
if (!first) proxyFile.oss << " && "; if (!first)
proxyFile.oss << "isa(varargin{" << i+1 << "},'" << args[i].matlabClass(".") << "')"; proxyFile.oss << " && ";
first=false; proxyFile.oss << "isa(varargin{" << i + 1 << "},'"
<< args[i].matlabClass(".") << "')";
first = false;
} }
proxyFile.oss << "\n"; proxyFile.oss << "\n";
// output call to C++ wrapper // output call to C++ wrapper
string output; string output;
if(returnVal.isPair) if (returnVal.isPair)
output = "[ varargout{1} varargout{2} ] = "; output = "[ varargout{1} varargout{2} ] = ";
else if(returnVal.category1 == ReturnValue::VOID) else if (returnVal.category1 == ReturnValue::VOID)
output = ""; output = "";
else else
output = "varargout{1} = "; output = "varargout{1} = ";
proxyFile.oss << " " << output << wrapperName << "(" << id << ", this, varargin{:});\n"; proxyFile.oss << " " << output << wrapperName << "(" << id
<< ", this, varargin{:});\n";
// Output C++ wrapper code // Output C++ wrapper code
const string wrapFunctionName = wrapper_fragment( const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName,
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);
} }
proxyFile.oss << " else\n"; proxyFile.oss << " else\n";
proxyFile.oss << " error('Arguments do not match any overload of function " << proxyFile.oss
matlabQualName << "." << name << "');" << endl; << " error('Arguments do not match any overload of function "
<< matlabQualName << "." << name << "');" << endl;
proxyFile.oss << " end\n"; proxyFile.oss << " end\n";
proxyFile.oss << " end\n"; proxyFile.oss << " end\n";
} }
/* ************************************************************************* */ /* ************************************************************************* */
string Method::wrapper_fragment(FileWriter& file, string Method::wrapper_fragment(FileWriter& file, const string& cppClassName,
const string& cppClassName, const string& matlabUniqueName, int overload, int id,
const string& matlabUniqueName, const TypeAttributesTable& typeAttributes) const {
int overload,
int id,
const TypeAttributesTable& typeAttributes) const {
// generate code // generate code
const string wrapFunctionName = matlabUniqueName + "_" + name + "_" + boost::lexical_cast<string>(id); const string wrapFunctionName = matlabUniqueName + "_" + name + "_"
+ boost::lexical_cast<string>(id);
const ArgumentList& args = argLists[overload]; const ArgumentList& args = argLists[overload];
const ReturnValue& returnVal = returnVals[overload]; const ReturnValue& returnVal = returnVals[overload];
// call // call
file.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; file.oss << "void " << wrapFunctionName
<< "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n";
// start // start
file.oss << "{\n"; file.oss << "{\n";
if(returnVal.isPair) if (returnVal.isPair) {
{ if (returnVal.category1 == ReturnValue::CLASS)
if(returnVal.category1 == ReturnValue::CLASS) file.oss << " typedef boost::shared_ptr<"
file.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType1("::") << "> Shared" << returnVal.type1 << ";"<< endl; << returnVal.qualifiedType1("::") << "> Shared" << returnVal.type1
if(returnVal.category2 == ReturnValue::CLASS) << ";" << endl;
file.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType2("::") << "> Shared" << returnVal.type2 << ";"<< endl; if (returnVal.category2 == ReturnValue::CLASS)
} file.oss << " typedef boost::shared_ptr<"
else << returnVal.qualifiedType2("::") << "> Shared" << returnVal.type2
if(returnVal.category1 == ReturnValue::CLASS) << ";" << endl;
file.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType1("::") << "> Shared" << returnVal.type1 << ";"<< endl; } else if (returnVal.category1 == ReturnValue::CLASS)
file.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType1("::")
<< "> Shared" << returnVal.type1 << ";" << endl;
file.oss << " typedef boost::shared_ptr<" << cppClassName << "> Shared;" << endl; file.oss << " typedef boost::shared_ptr<" << cppClassName << "> Shared;"
<< endl;
// check arguments // check arguments
// extra argument obj -> nargin-1 is passed ! // extra argument obj -> nargin-1 is passed !
// example: checkArguments("equals",nargout,nargin-1,2); // example: checkArguments("equals",nargout,nargin-1,2);
file.oss << " checkArguments(\"" << name << "\",nargout,nargin-1," << args.size() << ");\n"; file.oss << " checkArguments(\"" << name << "\",nargout,nargin-1,"
<< args.size() << ");\n";
// get class pointer // get class pointer
// example: shared_ptr<Test> = unwrap_shared_ptr< Test >(in[0], "Test"); // example: shared_ptr<Test> = unwrap_shared_ptr< Test >(in[0], "Test");
file.oss << " Shared obj = unwrap_shared_ptr<" << cppClassName << ">(in[0], \"ptr_" << matlabUniqueName << "\");" << endl; file.oss << " Shared obj = unwrap_shared_ptr<" << cppClassName
<< ">(in[0], \"ptr_" << matlabUniqueName << "\");" << endl;
// unwrap arguments, see Argument.cpp // unwrap arguments, see Argument.cpp
args.matlab_unwrap(file,1); args.matlab_unwrap(file, 1);
// call method and wrap result // call method and wrap result
// example: out[0]=wrap<bool>(self->return_field(t)); // example: out[0]=wrap<bool>(self->return_field(t));
if (returnVal.type1!="void") if (returnVal.type1 != "void")
returnVal.wrap_result("obj->"+name+"("+args.names()+")", file, typeAttributes); returnVal.wrap_result("obj->" + name + "(" + args.names() + ")", file,
typeAttributes);
else else
file.oss << " obj->"+name+"("+args.names()+");\n"; file.oss << " obj->" + name + "(" + args.names() + ");\n";
// finish // finish
file.oss << "}\n"; file.oss << "}\n";