Make explicit whether wrapper or proxy is written to...

release/4.3a0
dellaert 2014-11-12 20:51:47 +01:00
parent 72d44fe0af
commit 7d4f5a4820
5 changed files with 86 additions and 86 deletions

View File

@ -156,36 +156,37 @@ void ArgumentList::emit_prototype(FileWriter& file, const string& name) const {
file.oss << ")"; file.oss << ")";
} }
/* ************************************************************************* */ /* ************************************************************************* */
void ArgumentList::emit_call(FileWriter& file, const ReturnValue& returnVal, void ArgumentList::emit_call(FileWriter& proxyFile,
const string& wrapperName, int id, bool staticMethod) const { const ReturnValue& returnVal, const string& wrapperName, int id,
returnVal.emit_matlab(file); bool staticMethod) const {
file.oss << wrapperName << "(" << id; returnVal.emit_matlab(proxyFile);
proxyFile.oss << wrapperName << "(" << id;
if (!staticMethod) if (!staticMethod)
file.oss << ", this"; proxyFile.oss << ", this";
file.oss << ", varargin{:});\n"; 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, const ReturnValue& returnVal, const string& wrapperName, int id,
bool staticMethod) const { bool staticMethod) const {
// Check nr of arguments // Check nr of arguments
file.oss << "if length(varargin) == " << size(); proxyFile.oss << "if length(varargin) == " << size();
if (size() > 0) if (size() > 0)
file.oss << " && "; proxyFile.oss << " && ";
// ...and their type.names // ...and their type.names
bool first = true; bool first = true;
for (size_t i = 0; i < size(); i++) { for (size_t i = 0; i < size(); i++) {
if (!first) if (!first)
file.oss << " && "; proxyFile.oss << " && ";
file.oss << "isa(varargin{" << i + 1 << "},'" << (*this)[i].matlabClass(".") proxyFile.oss << "isa(varargin{" << i + 1 << "},'"
<< "')"; << (*this)[i].matlabClass(".") << "')";
first = false; first = false;
} }
file.oss << "\n"; proxyFile.oss << "\n";
// output call to C++ wrapper // output call to C++ wrapper
file.oss << " "; proxyFile.oss << " ";
emit_call(file, returnVal, wrapperName, id, staticMethod); emit_call(proxyFile, returnVal, wrapperName, id, staticMethod);
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -77,23 +77,23 @@ 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 emit MATLAB call to wrapper * emit emit MATLAB call to proxy
* @param file output stream * @param proxyFile output stream
* @param returnVal the return value * @param returnVal the return value
* @param wrapperName of method or function * @param wrapperName of method or function
* @param staticMethod flag to emit "this" in call * @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; const std::string& wrapperName, int id, bool staticMethod = false) const;
/** /**
* emit conditional MATLAB call to wrapper (checking arguments first) * emit conditional MATLAB call to proxy (checking arguments first)
* @param file output stream * @param proxyFile output stream
* @param returnVal the return value * @param returnVal the return value
* @param wrapperName of method or function * @param wrapperName of method or function
* @param staticMethod flag to emit "this" in call * @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; const std::string& wrapperName, int id, bool staticMethod = false) const;
}; };

View File

@ -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, void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
const string& cppClassName, const std::string& matlabQualName, FileWriter& wrapperFile, const string& cppClassName,
const std::string& matlabUniqueName, const string& wrapperName, const std::string& matlabQualName, const std::string& matlabUniqueName,
const TypeAttributesTable& typeAttributes, const string& wrapperName, const TypeAttributesTable& typeAttributes,
vector<string>& functionNames) const { vector<string>& functionNames) const {
// Create function header // Create function header
file.oss << " function varargout = " << name << "(this, varargin)\n"; proxyFile.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);
file.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) {
argList.emit_prototype(file, name); argList.emit_prototype(proxyFile, name);
if (argLCount != argLists.size() - 1) if (argLCount != argLists.size() - 1)
file.oss << ", "; proxyFile.oss << ", ";
else else
file.oss << " : returns " proxyFile.oss << " : returns " << returnVals[0].return_type(false)
<< returnVals[0].return_type(false) << endl; << endl;
argLCount++; argLCount++;
} }
// Emit URL to Doxygen page // 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" << "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) {
file.oss << " % " << "" << endl; proxyFile.oss << " % " << "" << endl;
file.oss << " % " << "Method Overloads" << endl; proxyFile.oss << " % " << "Method Overloads" << endl;
BOOST_FOREACH(ArgumentList argList, argLists) { BOOST_FOREACH(ArgumentList argList, argLists) {
file.oss << " % "; proxyFile.oss << " % ";
argList.emit_prototype(file, name); argList.emit_prototype(proxyFile, name);
file.oss << endl; proxyFile.oss << endl;
} }
} }
// Handle special case of single overload with all numeric arguments // Handle special case of single overload with all numeric arguments
if (argLists.size() == 1 && argLists[0].allScalar()) { if (argLists.size() == 1 && argLists[0].allScalar()) {
// Output proxy matlab code // Output proxy matlab code
file.oss << " "; proxyFile.oss << " ";
const int id = (int) functionNames.size(); 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 // Output C++ wrapper code
const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, 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) { for (size_t overload = 0; overload < argLists.size(); ++overload) {
// Output proxy matlab code // Output proxy matlab code
file.oss << " " << (overload == 0 ? "" : "else"); proxyFile.oss << " " << (overload == 0 ? "" : "else");
const int id = (int) functionNames.size(); const int id = (int) functionNames.size();
argLists[overload].emit_conditional_call(file, returnVals[overload], argLists[overload].emit_conditional_call(proxyFile, returnVals[overload],
wrapperName, id); wrapperName, id);
// Output C++ wrapper code // Output C++ wrapper code
@ -108,20 +108,20 @@ void Method::proxy_wrapper_fragments(FileWriter& file, FileWriter& wrapperFile,
// 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 proxyFile.oss
<< " error('Arguments do not match any overload of function " << " 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";
} }
/* ************************************************************************* */ /* ************************************************************************* */
string Method::wrapper_fragment(FileWriter& file, const string& cppClassName, string Method::wrapper_fragment(FileWriter& wrapperFile,
const string& matlabUniqueName, int overload, int id, const string& cppClassName, const string& matlabUniqueName, int overload,
const TypeAttributesTable& typeAttributes) const { int id, const TypeAttributesTable& typeAttributes) const {
// generate code // generate code
@ -132,39 +132,39 @@ string Method::wrapper_fragment(FileWriter& file, const string& cppClassName,
const ReturnValue& returnVal = returnVals[overload]; const ReturnValue& returnVal = returnVals[overload];
// call // call
file.oss << "void " << wrapFunctionName wrapperFile.oss << "void " << wrapFunctionName
<< "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n"; << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n";
// start // start
file.oss << "{\n"; wrapperFile.oss << "{\n";
returnVal.wrapTypeUnwrap(file); returnVal.wrapTypeUnwrap(wrapperFile);
file.oss << " typedef boost::shared_ptr<" << cppClassName << "> Shared;" wrapperFile.oss << " typedef boost::shared_ptr<" << cppClassName
<< endl; << "> 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," wrapperFile.oss << " checkArguments(\"" << name << "\",nargout,nargin-1,"
<< args.size() << ");\n"; << 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 wrapperFile.oss << " Shared obj = unwrap_shared_ptr<" << cppClassName
<< ">(in[0], \"ptr_" << matlabUniqueName << "\");" << endl; << ">(in[0], \"ptr_" << matlabUniqueName << "\");" << endl;
// unwrap arguments, see Argument.cpp // unwrap arguments, see Argument.cpp
args.matlab_unwrap(file, 1); args.matlab_unwrap(wrapperFile, 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.name != "void") if (returnVal.type1.name != "void")
returnVal.wrap_result("obj->" + name + "(" + args.names() + ")", file, returnVal.wrap_result("obj->" + name + "(" + args.names() + ")",
typeAttributes); wrapperFile, typeAttributes);
else else
file.oss << " obj->" + name + "(" + args.names() + ");\n"; wrapperFile.oss << " obj->" + name + "(" + args.names() + ");\n";
// finish // finish
file.oss << "}\n"; wrapperFile.oss << "}\n";
return wrapFunctionName; return wrapFunctionName;
} }

View File

@ -32,7 +32,8 @@ struct Method {
/// Constructor creates empty object /// Constructor creates empty object
Method(bool verbose = true) : 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 // Then the instance variables are set directly by the Module constructor
bool verbose_; bool verbose_;
@ -45,22 +46,20 @@ struct Method {
// with those in rhs, but in subsequent calls it adds additional argument // with those in rhs, but in subsequent calls it adds additional argument
// lists as function overloads. // lists as function overloads.
void addOverload(bool verbose, bool is_const, const std::string& name, 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 // MATLAB code generation
// classPath is class directory, e.g., ../matlab/@Point2 // classPath is class directory, e.g., ../matlab/@Point2
void proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, void proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile,
const std::string& cppClassName, const std::string& matlabQualName, const std::string& matlabUniqueName, const std::string& cppClassName, const std::string& matlabQualName,
const std::string& wrapperName, const TypeAttributesTable& typeAttributes, const std::string& matlabUniqueName, const std::string& wrapperName,
std::vector<std::string>& functionNames) const; const TypeAttributesTable& typeAttributes,
std::vector<std::string>& functionNames) const;
private: private:
std::string wrapper_fragment(FileWriter& file, std::string wrapper_fragment(FileWriter& wrapperFile,
const std::string& cppClassName, const std::string& cppClassName, const std::string& matlabUniqueName,
const std::string& matlabUniqueName, int overload, int id, const TypeAttributesTable& typeAttributes) const; ///< cpp wrapper
int overload,
int id,
const TypeAttributesTable& typeAttributes) const; ///< cpp wrapper
}; };
} // \namespace wrap } // \namespace wrap

View File

@ -7,10 +7,10 @@
* @author Richard Roberts * @author Richard Roberts
*/ */
#include <boost/foreach.hpp>
#include "ReturnValue.h" #include "ReturnValue.h"
#include "utilities.h" #include "utilities.h"
#include <boost/foreach.hpp>
#include <iostream>
using namespace std; using namespace std;
using namespace wrap; 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) if (category == CLASS)
file.oss << " typedef boost::shared_ptr<" << qualifiedName("::") wrapperFile.oss << " typedef boost::shared_ptr<" << qualifiedName("::")
<< "> Shared" << name << ";" << endl; << "> 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 { const TypeAttributesTable& typeAttributes) const {
if (isPair) { if (isPair) {
// For a pair, store the returned pair so we do not evaluate the function twice // 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"; << ";\n";
type1.wrap_result(" out[0]", "pairResult.first", file, typeAttributes); type1.wrap_result(" out[0]", "pairResult.first", wrapperFile, typeAttributes);
type2.wrap_result(" out[1]", "pairResult.second", file, typeAttributes); type2.wrap_result(" out[1]", "pairResult.second", wrapperFile, typeAttributes);
} else { // Not a pair } 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 { void ReturnValue::wrapTypeUnwrap(FileWriter& wrapperFile) const {
type1.wrapTypeUnwrap(file); type1.wrapTypeUnwrap(wrapperFile);
if (isPair) if (isPair)
type2.wrapTypeUnwrap(file); type2.wrapTypeUnwrap(wrapperFile);
} }
/* ************************************************************************* */ /* ************************************************************************* */
void ReturnValue::emit_matlab(FileWriter& file) const { void ReturnValue::emit_matlab(FileWriter& proxyFile) const {
string output; string output;
if (isPair) if (isPair)
file.oss << "[ varargout{1} varargout{2} ] = "; proxyFile.oss << "[ varargout{1} varargout{2} ] = ";
else if (type1.category != ReturnType::VOID) else if (type1.category != ReturnType::VOID)
file.oss << "varargout{1} = "; proxyFile.oss << "varargout{1} = ";
} }
/* ************************************************************************* */ /* ************************************************************************* */