Finish globalOverloads: made sure overloaded global functions actually work, including within namespaces.

release/4.3a0
dellaert 2014-05-25 16:44:52 -04:00
commit 6384e4d336
11 changed files with 150 additions and 89 deletions

View File

@ -17,7 +17,8 @@ using namespace std;
/* ************************************************************************* */
void GlobalFunction::addOverload(bool verbose, const std::string& name,
const ArgumentList& args, const ReturnValue& retVal, const StrVec& ns_stack) {
const ArgumentList& args, const ReturnValue& retVal,
const StrVec& ns_stack) {
this->verbose_ = verbose;
this->name = name;
this->argLists.push_back(args);
@ -26,16 +27,16 @@ void GlobalFunction::addOverload(bool verbose, const std::string& name,
}
/* ************************************************************************* */
void GlobalFunction::matlab_proxy(const std::string& toolboxPath, const std::string& wrapperName,
const TypeAttributesTable& typeAttributes, FileWriter& wrapperFile,
std::vector<std::string>& functionNames) const {
void GlobalFunction::matlab_proxy(const std::string& toolboxPath,
const std::string& wrapperName, const TypeAttributesTable& typeAttributes,
FileWriter& file, std::vector<std::string>& functionNames) const {
// cluster overloads with same namespace
// create new GlobalFunction structures around namespaces - same namespaces and names are overloads
// map of namespace to global function
typedef map<string, GlobalFunction> GlobalFunctionMap;
GlobalFunctionMap grouped_functions;
for (size_t i=0; i<namespaces.size(); ++i) {
for (size_t i = 0; i < namespaces.size(); ++i) {
StrVec ns = namespaces.at(i);
string str_ns = qualifiedName("", ns, "");
ReturnValue ret = returnVals.at(i);
@ -51,16 +52,17 @@ void GlobalFunction::matlab_proxy(const std::string& toolboxPath, const std::str
size_t lastcheck = grouped_functions.size();
BOOST_FOREACH(const GlobalFunctionMap::value_type& p, grouped_functions) {
p.second.generateSingleFunction(toolboxPath, wrapperName, typeAttributes, wrapperFile, functionNames);
p.second.generateSingleFunction(toolboxPath, wrapperName, typeAttributes,
file, functionNames);
if (--lastcheck != 0)
wrapperFile.oss << endl;
file.oss << endl;
}
}
/* ************************************************************************* */
void GlobalFunction::generateSingleFunction(const std::string& toolboxPath, const std::string& wrapperName,
const TypeAttributesTable& typeAttributes, FileWriter& wrapperFile,
std::vector<std::string>& functionNames) const {
void GlobalFunction::generateSingleFunction(const std::string& toolboxPath,
const std::string& wrapperName, const TypeAttributesTable& typeAttributes,
FileWriter& file, std::vector<std::string>& functionNames) const {
// create the folder for the namespace
const StrVec& ns = namespaces.front();
@ -68,79 +70,68 @@ void GlobalFunction::generateSingleFunction(const std::string& toolboxPath, cons
// open destination mfunctionFileName
string mfunctionFileName = toolboxPath;
if(!ns.empty())
if (!ns.empty())
mfunctionFileName += "/+" + wrap::qualifiedName("/+", ns);
mfunctionFileName += "/" + name + ".m";
FileWriter mfunctionFile(mfunctionFileName, verbose_, "%");
// get the name of actual matlab object
const string
matlabQualName = qualifiedName(".", ns, name),
matlabUniqueName = qualifiedName("", ns, name),
cppName = qualifiedName("::", ns, name);
const string matlabQualName = qualifiedName(".", ns, name), matlabUniqueName =
qualifiedName("", ns, name), cppName = qualifiedName("::", ns, name);
mfunctionFile.oss << "function varargout = " << name << "(varargin)\n";
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
// check for number of arguments...
mfunctionFile.oss << (overload==0?"":"else") << "if length(varargin) == " << nrArgs;
if (nrArgs>0) mfunctionFile.oss << " && ";
// ...and their types
bool first = true;
for(size_t i=0;i<nrArgs;i++) {
if (!first) mfunctionFile.oss << " && ";
mfunctionFile.oss << "isa(varargin{" << i+1 << "},'" << args[i].matlabClass(".") << "')";
first=false;
}
mfunctionFile.oss << "\n";
// output call to C++ wrapper
mfunctionFile.oss << " ";
returnVal.emit_matlab(mfunctionFile);
mfunctionFile.oss << wrapperName << "(" << id << ", varargin{:});\n";
mfunctionFile.oss << " " << (overload == 0 ? "" : "else");
argLists[overload].emit_conditional_call(mfunctionFile,
returnVals[overload], wrapperName, id, true); // true omits "this"
// Output C++ wrapper code
const string wrapFunctionName = matlabUniqueName + "_" + boost::lexical_cast<string>(id);
const string wrapFunctionName = matlabUniqueName + "_"
+ boost::lexical_cast<string>(id);
// call
wrapperFile.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
wrapperFile.oss << "{\n";
file.oss << "{\n";
returnVal.wrapTypeUnwrap(wrapperFile);
returnVal.wrapTypeUnwrap(file);
// check arguments
// NOTE: for static functions, there is no object passed
wrapperFile.oss << " checkArguments(\"" << matlabUniqueName << "\",nargout,nargin," << args.size() << ");\n";
file.oss << " checkArguments(\"" << matlabUniqueName
<< "\",nargout,nargin," << args.size() << ");\n";
// unwrap arguments, see Argument.cpp
args.matlab_unwrap(wrapperFile,0); // We start at 0 because there is no self object
args.matlab_unwrap(file, 0); // We start at 0 because there is no self object
// call method with default type and wrap result
if (returnVal.type1!="void")
returnVal.wrap_result(cppName+"("+args.names()+")", wrapperFile, typeAttributes);
if (returnVal.type1 != "void")
returnVal.wrap_result(cppName + "(" + args.names() + ")", file,
typeAttributes);
else
wrapperFile.oss << cppName+"("+args.names()+");\n";
file.oss << cppName + "(" + args.names() + ");\n";
// finish
wrapperFile.oss << "}\n";
file.oss << "}\n";
// Add to function list
functionNames.push_back(wrapFunctionName);
}
mfunctionFile.oss << "else\n";
mfunctionFile.oss << " error('Arguments do not match any overload of function " << matlabQualName << "');" << endl;
mfunctionFile.oss << "end" << endl;
mfunctionFile.oss << " else\n";
mfunctionFile.oss
<< " error('Arguments do not match any overload of function "
<< matlabQualName << "');" << endl;
mfunctionFile.oss << " end" << endl;
// Close file
mfunctionFile.emit(true);
@ -148,9 +139,5 @@ void GlobalFunction::generateSingleFunction(const std::string& toolboxPath, cons
/* ************************************************************************* */
} // \namespace wrap

View File

@ -22,37 +22,38 @@ struct GlobalFunction {
std::string name;
// each overload, regardless of namespace
std::vector<ArgumentList> argLists; ///< arugments for each overload
std::vector<ReturnValue> returnVals; ///< returnVals for each overload
std::vector<StrVec> namespaces; ///< Stack of namespaces
std::vector<ArgumentList> argLists; ///< arugments for each overload
std::vector<ReturnValue> returnVals; ///< returnVals for each overload
std::vector<StrVec> namespaces; ///< Stack of namespaces
// Constructor only used in Module
GlobalFunction(bool verbose = true) : verbose_(verbose) {}
GlobalFunction(bool verbose = true) :
verbose_(verbose) {
}
// Used to reconstruct
GlobalFunction(const std::string& name_, bool verbose = true)
: verbose_(verbose), name(name_) {}
GlobalFunction(const std::string& name_, bool verbose = true) :
verbose_(verbose), name(name_) {
}
// adds an overloaded version of this function
void addOverload(bool verbose, const std::string& name,
const ArgumentList& args, const ReturnValue& retVal, const StrVec& ns_stack);
const ArgumentList& args, const ReturnValue& retVal,
const StrVec& ns_stack);
// codegen function called from Module to build the cpp and matlab versions of the function
void matlab_proxy(const std::string& toolboxPath, const std::string& wrapperName,
const TypeAttributesTable& typeAttributes, FileWriter& wrapperFile,
std::vector<std::string>& functionNames) const;
void matlab_proxy(const std::string& toolboxPath,
const std::string& wrapperName, const TypeAttributesTable& typeAttributes,
FileWriter& file, std::vector<std::string>& functionNames) const;
private:
// Creates a single global function - all in same namespace
void generateSingleFunction(const std::string& toolboxPath, const std::string& wrapperName,
const TypeAttributesTable& typeAttributes, FileWriter& wrapperFile,
std::vector<std::string>& functionNames) const;
void generateSingleFunction(const std::string& toolboxPath,
const std::string& wrapperName, const TypeAttributesTable& typeAttributes,
FileWriter& file, std::vector<std::string>& functionNames) const;
};
} // \namespace wrap

View File

@ -1,6 +1,6 @@
function varargout = aGlobalFunction(varargin)
if length(varargin) == 0
varargout{1} = geometry_wrapper(42, varargin{:});
else
error('Arguments do not match any overload of function aGlobalFunction');
end
if length(varargin) == 0
varargout{1} = geometry_wrapper(42, varargin{:});
else
error('Arguments do not match any overload of function aGlobalFunction');
end

View File

@ -502,6 +502,19 @@ void aGlobalFunction_42(int nargout, mxArray *out[], int nargin, const mxArray *
checkArguments("aGlobalFunction",nargout,nargin,0);
out[0] = wrap< Vector >(aGlobalFunction());
}
void overloadedGlobalFunction_43(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
checkArguments("overloadedGlobalFunction",nargout,nargin,1);
int a = unwrap< int >(in[0]);
out[0] = wrap< Vector >(overloadedGlobalFunction(a));
}
void overloadedGlobalFunction_44(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
checkArguments("overloadedGlobalFunction",nargout,nargin,2);
int a = unwrap< int >(in[0]);
double b = unwrap< double >(in[1]);
out[0] = wrap< Vector >(overloadedGlobalFunction(a,b));
}
void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
@ -643,6 +656,12 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
case 42:
aGlobalFunction_42(nargout, out, nargin-1, in+1);
break;
case 43:
overloadedGlobalFunction_43(nargout, out, nargin-1, in+1);
break;
case 44:
overloadedGlobalFunction_44(nargout, out, nargin-1, in+1);
break;
}
} catch(const std::exception& e) {
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());

View File

@ -0,0 +1,8 @@
function varargout = overloadedGlobalFunction(varargin)
if length(varargin) == 1 && isa(varargin{1},'numeric')
varargout{1} = geometry_wrapper(43, varargin{:});
elseif length(varargin) == 2 && isa(varargin{1},'numeric') && isa(varargin{2},'double')
varargout{1} = geometry_wrapper(44, varargin{:});
else
error('Arguments do not match any overload of function overloadedGlobalFunction');
end

View File

@ -0,0 +1,8 @@
function varargout = overloadedGlobalFunction(varargin)
if length(varargin) == 1 && isa(varargin{1},'ns1.ClassA')
varargout{1} = testNamespaces_wrapper(24, varargin{:});
elseif length(varargin) == 2 && isa(varargin{1},'ns1.ClassA') && isa(varargin{2},'double')
varargout{1} = testNamespaces_wrapper(25, varargin{:});
else
error('Arguments do not match any overload of function ns2.overloadedGlobalFunction');
end

View File

@ -342,6 +342,21 @@ void ns2aGlobalFunction_23(int nargout, mxArray *out[], int nargin, const mxArra
checkArguments("ns2aGlobalFunction",nargout,nargin,0);
out[0] = wrap< Vector >(ns2::aGlobalFunction());
}
void ns2overloadedGlobalFunction_24(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
typedef boost::shared_ptr<ns1::ClassA> SharedClassA;
checkArguments("ns2overloadedGlobalFunction",nargout,nargin,1);
ns1::ClassA& a = *unwrap_shared_ptr< ns1::ClassA >(in[0], "ptr_ns1ClassA");
out[0] = wrap_shared_ptr(SharedClassA(new ns1::ClassA(ns2::overloadedGlobalFunction(a))),"ns1.ClassA", false);
}
void ns2overloadedGlobalFunction_25(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
typedef boost::shared_ptr<ns1::ClassA> SharedClassA;
checkArguments("ns2overloadedGlobalFunction",nargout,nargin,2);
ns1::ClassA& a = *unwrap_shared_ptr< ns1::ClassA >(in[0], "ptr_ns1ClassA");
double b = unwrap< double >(in[1]);
out[0] = wrap_shared_ptr(SharedClassA(new ns1::ClassA(ns2::overloadedGlobalFunction(a,b))),"ns1.ClassA", false);
}
void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
{
@ -426,6 +441,12 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
case 23:
ns2aGlobalFunction_23(nargout, out, nargin-1, in+1);
break;
case 24:
ns2overloadedGlobalFunction_24(nargout, out, nargin-1, in+1);
break;
case 25:
ns2overloadedGlobalFunction_25(nargout, out, nargin-1, in+1);
break;
}
} catch(const std::exception& e) {
mexErrMsgTxt(("Exception from gtsam:\n" + std::string(e.what()) + "\n").c_str());

View File

@ -87,6 +87,10 @@ class Test {
Vector aGlobalFunction();
// An overloaded global function
Vector overloadedGlobalFunction(int a);
Vector overloadedGlobalFunction(int a, double b);
// comments at the end!
// even more comments at the end!

View File

@ -47,6 +47,10 @@ class ClassC {
// separate namespace global function, same name
Vector aGlobalFunction();
// An overloaded global function
ns1::ClassA overloadedGlobalFunction(const ns1::ClassA& a);
ns1::ClassA overloadedGlobalFunction(const ns1::ClassA& a, double b);
} //\namespace ns2
class ClassD {

View File

@ -15,16 +15,18 @@
* @author Frank Dellaert
**/
#include <wrap/utilities.h>
#include <wrap/Module.h>
#include <CppUnitLite/TestHarness.h>
#include <boost/assign/std/vector.hpp>
#include <boost/filesystem.hpp>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/assign/std/vector.hpp>
#include <boost/filesystem.hpp>
#include <CppUnitLite/TestHarness.h>
#include <wrap/utilities.h>
#include <wrap/Module.h>
using namespace std;
using namespace boost::assign;
@ -305,8 +307,8 @@ TEST( wrap, parse_geometry ) {
}
// evaluate global functions
// Vector aGlobalFunction();
LONGS_EQUAL(1, module.global_functions.size());
// Vector aGlobalFunction();
LONGS_EQUAL(2, module.global_functions.size());
CHECK(module.global_functions.find("aGlobalFunction") != module.global_functions.end());
{
GlobalFunction gfunc = module.global_functions.at("aGlobalFunction");
@ -380,7 +382,7 @@ TEST( wrap, parse_namespaces ) {
// evaluate global functions
// Vector ns1::aGlobalFunction();
// Vector ns2::aGlobalFunction();
LONGS_EQUAL(1, module.global_functions.size());
LONGS_EQUAL(2, module.global_functions.size());
CHECK(module.global_functions.find("aGlobalFunction") != module.global_functions.end());
{
GlobalFunction gfunc = module.global_functions.at("aGlobalFunction");
@ -415,13 +417,17 @@ TEST( wrap, matlab_code_namespaces ) {
module.matlab_code("actual_namespaces", headerPath);
EXPECT(files_equal(exp_path + "ClassD.m" , act_path + "ClassD.m" ));
EXPECT(files_equal(exp_path + "+ns1/ClassA.m" , act_path + "+ns1/ClassA.m" ));
EXPECT(files_equal(exp_path + "+ns1/ClassB.m" , act_path + "+ns1/ClassB.m" ));
EXPECT(files_equal(exp_path + "+ns2/ClassA.m" , act_path + "+ns2/ClassA.m" ));
EXPECT(files_equal(exp_path + "+ns2/ClassC.m" , act_path + "+ns2/ClassC.m" ));
EXPECT(files_equal(exp_path + "+ns2/+ns3/ClassB.m" , act_path + "+ns2/+ns3/ClassB.m" ));
EXPECT(files_equal(exp_path + "testNamespaces_wrapper.cpp" , act_path + "testNamespaces_wrapper.cpp" ));
EXPECT(files_equal(exp_path + "ClassD.m", act_path + "ClassD.m" ));
EXPECT(files_equal(exp_path + "+ns1/ClassA.m", act_path + "+ns1/ClassA.m" ));
EXPECT(files_equal(exp_path + "+ns1/ClassB.m", act_path + "+ns1/ClassB.m" ));
EXPECT(files_equal(exp_path + "+ns2/ClassA.m", act_path + "+ns2/ClassA.m" ));
EXPECT(files_equal(exp_path + "+ns2/ClassC.m", act_path + "+ns2/ClassC.m" ));
EXPECT(
files_equal(exp_path + "+ns2/overloadedGlobalFunction.m", exp_path + "+ns2/overloadedGlobalFunction.m" ));
EXPECT(
files_equal(exp_path + "+ns2/+ns3/ClassB.m", act_path + "+ns2/+ns3/ClassB.m" ));
EXPECT(
files_equal(exp_path + "testNamespaces_wrapper.cpp", act_path + "testNamespaces_wrapper.cpp" ));
}
/* ************************************************************************* */
@ -445,6 +451,7 @@ TEST( wrap, matlab_code_geometry ) {
EXPECT(files_equal(epath + "Point3.m" , apath + "Point3.m" ));
EXPECT(files_equal(epath + "Test.m" , apath + "Test.m" ));
EXPECT(files_equal(epath + "aGlobalFunction.m" , apath + "aGlobalFunction.m" ));
EXPECT(files_equal(epath + "overloadedGlobalFunction.m" , apath + "overloadedGlobalFunction.m" ));
}
/* ************************************************************************* */

View File

@ -88,9 +88,11 @@ bool files_equal(const string& expected, const string& actual, bool skipheader)
string actual_contents = file_contents(actual, skipheader);
bool equal = actual_contents == expected_contents;
if (!equal) {
cerr << "<<< DIFF OUTPUT (if none, white-space differences only):\n";
stringstream command;
command << "diff " << expected << " " << actual << endl;
command << "diff --ignore-all-space " << expected << " " << actual << endl;
system(command.str().c_str());
cerr << ">>>\n";
}
return equal;
}