Generating code for global functions now works
parent
ece5888cac
commit
b7c2177f0b
|
@ -24,7 +24,6 @@
|
|||
#include <stdint.h> // works on Linux GCC
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
#include "Class.h"
|
||||
|
@ -40,18 +39,7 @@ void Class::matlab_proxy(const string& toolboxPath, const string& wrapperName,
|
|||
FileWriter& wrapperFile, vector<string>& functionNames) const {
|
||||
|
||||
// Create namespace folders
|
||||
{
|
||||
using namespace boost::filesystem;
|
||||
path curPath = toolboxPath;
|
||||
BOOST_FOREACH(const string& subdir, namespaces) {
|
||||
curPath /= "+" + subdir;
|
||||
if(!is_directory(curPath))
|
||||
if(exists("+" + subdir))
|
||||
throw OutputError("Need to write files to directory " + curPath.string() + ", which already exists as a file but is not a directory");
|
||||
else
|
||||
boost::filesystem::create_directory(curPath);
|
||||
}
|
||||
}
|
||||
createNamespaceStructure(namespaces, toolboxPath);
|
||||
|
||||
// open destination classFile
|
||||
string classFile = toolboxPath;
|
||||
|
|
|
@ -6,9 +6,15 @@
|
|||
*/
|
||||
|
||||
#include "GlobalFunction.h"
|
||||
#include "utilities.h"
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
namespace wrap {
|
||||
|
||||
using namespace std;
|
||||
|
||||
/* ************************************************************************* */
|
||||
void GlobalFunction::addOverload(bool verbose, const std::string& name,
|
||||
const ArgumentList& args, const ReturnValue& retVal, const StrVec& ns_stack) {
|
||||
|
@ -19,6 +25,142 @@ void GlobalFunction::addOverload(bool verbose, const std::string& name,
|
|||
this->namespaces.push_back(ns_stack);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void GlobalFunction::matlab_proxy(const std::string& toolboxPath, const std::string& wrapperName,
|
||||
const TypeAttributesTable& typeAttributes, FileWriter& wrapperFile,
|
||||
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) {
|
||||
StrVec ns = namespaces.at(i);
|
||||
string str_ns = qualifiedName("", ns, "");
|
||||
ReturnValue ret = returnVals.at(i);
|
||||
ArgumentList args = argLists.at(i);
|
||||
|
||||
if (!grouped_functions.count(str_ns))
|
||||
grouped_functions[str_ns] = GlobalFunction(name, verbose_);
|
||||
|
||||
grouped_functions[str_ns].argLists.push_back(args);
|
||||
grouped_functions[str_ns].returnVals.push_back(ret);
|
||||
grouped_functions[str_ns].namespaces.push_back(ns);
|
||||
}
|
||||
|
||||
size_t lastcheck = grouped_functions.size();
|
||||
BOOST_FOREACH(const GlobalFunctionMap::value_type& p, grouped_functions) {
|
||||
p.second.generateSingleFunction(toolboxPath, wrapperName, typeAttributes, wrapperFile, functionNames);
|
||||
if (--lastcheck != 0)
|
||||
wrapperFile.oss << endl;
|
||||
}
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void GlobalFunction::generateSingleFunction(const std::string& toolboxPath, const std::string& wrapperName,
|
||||
const TypeAttributesTable& typeAttributes, FileWriter& wrapperFile,
|
||||
std::vector<std::string>& functionNames) const {
|
||||
|
||||
// create the folder for the namespace
|
||||
const StrVec& ns = namespaces.front();
|
||||
createNamespaceStructure(ns, toolboxPath);
|
||||
|
||||
// open destination mfunctionFileName
|
||||
string mfunctionFileName = toolboxPath;
|
||||
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);
|
||||
|
||||
mfunctionFile.oss << "function varargout = " << name << "(varargin)\n";
|
||||
|
||||
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
|
||||
string output;
|
||||
if(returnVal.isPair)
|
||||
output = "[ varargout{1} varargout{2} ] = ";
|
||||
else if(returnVal.category1 == ReturnValue::VOID)
|
||||
output = "";
|
||||
else
|
||||
output = "varargout{1} = ";
|
||||
mfunctionFile.oss << " " << output << wrapperName << "(" << id << ", varargin{:});\n";
|
||||
|
||||
// Output C++ wrapper code
|
||||
|
||||
const string wrapFunctionName = matlabUniqueName + "_" + boost::lexical_cast<string>(id);
|
||||
|
||||
// call
|
||||
wrapperFile.oss << "void " << wrapFunctionName << "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n";
|
||||
// start
|
||||
wrapperFile.oss << "{\n";
|
||||
|
||||
if(returnVal.isPair)
|
||||
{
|
||||
if(returnVal.category1 == ReturnValue::CLASS)
|
||||
wrapperFile.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType1("::") << "> Shared" << returnVal.type1 << ";"<< endl;
|
||||
if(returnVal.category2 == ReturnValue::CLASS)
|
||||
wrapperFile.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType2("::") << "> Shared" << returnVal.type2 << ";"<< endl;
|
||||
}
|
||||
else {
|
||||
if (returnVal.category1 == ReturnValue::CLASS)
|
||||
wrapperFile.oss << " typedef boost::shared_ptr<" << returnVal.qualifiedType1("::") << "> Shared" << returnVal.type1 << ";"<< endl;
|
||||
}
|
||||
|
||||
// check arguments
|
||||
// NOTE: for static functions, there is no object passed
|
||||
wrapperFile.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
|
||||
|
||||
// call method with default type and wrap result
|
||||
if (returnVal.type1!="void")
|
||||
returnVal.wrap_result(cppName+"("+args.names()+")", wrapperFile, typeAttributes);
|
||||
else
|
||||
wrapperFile.oss << cppName+"("+args.names()+");\n";
|
||||
|
||||
// finish
|
||||
wrapperFile.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;
|
||||
|
||||
// Close file
|
||||
mfunctionFile.emit(true);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
||||
|
||||
|
|
|
@ -29,10 +29,26 @@ struct GlobalFunction {
|
|||
// Constructor only used in Module
|
||||
GlobalFunction(bool verbose = true) : verbose_(verbose) {}
|
||||
|
||||
// Used to reconstruct
|
||||
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);
|
||||
|
||||
// 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;
|
||||
|
||||
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;
|
||||
|
||||
};
|
||||
|
||||
} // \namespace wrap
|
||||
|
|
|
@ -464,6 +464,11 @@ void Module::matlab_code(const string& toolboxPath, const string& headerPath) co
|
|||
cls.matlab_proxy(toolboxPath, wrapperName, typeAttributes, wrapperFile, functionNames);
|
||||
}
|
||||
|
||||
// create matlab files and wrapper code for global functions
|
||||
BOOST_FOREACH(const GlobalFunctions::value_type& p, global_functions) {
|
||||
p.second.matlab_proxy(toolboxPath, wrapperName, typeAttributes, wrapperFile, functionNames);
|
||||
}
|
||||
|
||||
// finish wrapper file
|
||||
wrapperFile.oss << "\n";
|
||||
finish_wrapper(wrapperFile, functionNames);
|
||||
|
|
|
@ -38,6 +38,7 @@ void StaticMethod::addOverload(bool verbose, const std::string& name,
|
|||
this->returnVals.push_back(retVal);
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void StaticMethod::proxy_wrapper_fragments(FileWriter& proxyFile, FileWriter& wrapperFile,
|
||||
const string& cppClassName,
|
||||
const std::string& matlabQualName,
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
% automatically generated by wrap
|
||||
function varargout = aGlobalFunction(varargin)
|
||||
if length(varargin) == 0
|
||||
varargout{1} = geometry_wrapper(40, varargin{:});
|
||||
else
|
||||
error('Arguments do not match any overload of function aGlobalFunction');
|
||||
end
|
|
@ -495,6 +495,11 @@ using namespace geometry;
|
|||
out[0] = wrap< Vector >(obj->return_vector2(value));
|
||||
}
|
||||
|
||||
void aGlobalFunction_40(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
||||
{
|
||||
checkArguments("aGlobalFunction",nargout,nargin,0);
|
||||
out[0] = wrap< Vector >(aGlobalFunction());
|
||||
}
|
||||
|
||||
void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
||||
{
|
||||
|
@ -626,6 +631,9 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
|||
case 39:
|
||||
Test_return_vector2_39(nargout, out, nargin-1, in+1);
|
||||
break;
|
||||
case 40:
|
||||
aGlobalFunction_40(nargout, out, nargin-1, in+1);
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout.rdbuf(outbuf);
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
% automatically generated by wrap
|
||||
function varargout = aGlobalFunction(varargin)
|
||||
if length(varargin) == 0
|
||||
varargout{1} = testNamespaces_wrapper(22, varargin{:});
|
||||
else
|
||||
error('Arguments do not match any overload of function ns1.aGlobalFunction');
|
||||
end
|
|
@ -0,0 +1,7 @@
|
|||
% automatically generated by wrap
|
||||
function varargout = aGlobalFunction(varargin)
|
||||
if length(varargin) == 0
|
||||
varargout{1} = testNamespaces_wrapper(23, varargin{:});
|
||||
else
|
||||
error('Arguments do not match any overload of function ns2.aGlobalFunction');
|
||||
end
|
|
@ -332,6 +332,17 @@ void ClassD_deconstructor_21(int nargout, mxArray *out[], int nargin, const mxAr
|
|||
}
|
||||
}
|
||||
|
||||
void ns1aGlobalFunction_22(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
||||
{
|
||||
checkArguments("ns1aGlobalFunction",nargout,nargin,0);
|
||||
out[0] = wrap< Vector >(ns1::aGlobalFunction());
|
||||
}
|
||||
|
||||
void ns2aGlobalFunction_23(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
||||
{
|
||||
checkArguments("ns2aGlobalFunction",nargout,nargin,0);
|
||||
out[0] = wrap< Vector >(ns2::aGlobalFunction());
|
||||
}
|
||||
|
||||
void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
||||
{
|
||||
|
@ -409,6 +420,12 @@ void mexFunction(int nargout, mxArray *out[], int nargin, const mxArray *in[])
|
|||
case 21:
|
||||
ClassD_deconstructor_21(nargout, out, nargin-1, in+1);
|
||||
break;
|
||||
case 22:
|
||||
ns1aGlobalFunction_22(nargout, out, nargin-1, in+1);
|
||||
break;
|
||||
case 23:
|
||||
ns2aGlobalFunction_23(nargout, out, nargin-1, in+1);
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout.rdbuf(outbuf);
|
||||
|
|
|
@ -291,6 +291,7 @@ TEST( wrap, matlab_code_geometry ) {
|
|||
EXPECT(files_equal(epath + "Point2.m" , apath + "Point2.m" ));
|
||||
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" ));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <cstdlib>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include "utilities.h"
|
||||
|
||||
|
@ -137,6 +138,21 @@ string qualifiedName(const string& separator, const vector<string>& names, const
|
|||
return result;
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
void createNamespaceStructure(const std::vector<std::string>& namespaces, const std::string& toolboxPath) {
|
||||
using namespace boost::filesystem;
|
||||
path curPath = toolboxPath;
|
||||
BOOST_FOREACH(const string& subdir, namespaces) {
|
||||
curPath /= "+" + subdir;
|
||||
if(!is_directory(curPath)) {
|
||||
if(exists("+" + subdir))
|
||||
throw OutputError("Need to write files to directory " + curPath.string() + ", which already exists as a file but is not a directory");
|
||||
else
|
||||
boost::filesystem::create_directory(curPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
|
||||
} // \namespace wrap
|
||||
|
|
|
@ -133,4 +133,7 @@ void generateUsingNamespace(FileWriter& file, const std::vector<std::string>& us
|
|||
*/
|
||||
std::string qualifiedName(const std::string& separator, const std::vector<std::string>& names, const std::string& finalName = "");
|
||||
|
||||
/** creates the necessary folders for namespaces, as specified by a namespace stack */
|
||||
void createNamespaceStructure(const std::vector<std::string>& namespaces, const std::string& toolboxPath);
|
||||
|
||||
} // \namespace wrap
|
||||
|
|
Loading…
Reference in New Issue