gtsam/wrap/GlobalFunction.cpp

218 lines
7.6 KiB
C++
Raw Normal View History

/**
* @file GlobalFunction.cpp
*
* @date Jul 22, 2012
* @author Alex Cunningham
*/
#include "GlobalFunction.h"
#include "Class.h"
#include "utilities.h"
#include <boost/lexical_cast.hpp>
namespace wrap {
using namespace std;
/* ************************************************************************* */
void GlobalFunction::addOverload(const Qualified& overload,
2016-11-23 06:09:35 +08:00
const ArgumentList& args, const ReturnValue& retVal, const std::string& _includeFile,
boost::optional<const Qualified> instName, bool verbose) {
2014-12-01 03:12:03 +08:00
FullyOverloadedFunction::addOverload(overload.name(), args, retVal, instName,
verbose);
overloads.push_back(overload);
2016-11-23 06:09:35 +08:00
includeFile = _includeFile;
}
/* ************************************************************************* */
void GlobalFunction::matlab_proxy(const string& toolboxPath,
const string& wrapperName, const TypeAttributesTable& typeAttributes,
FileWriter& file, vector<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 < overloads.size(); ++i) {
Qualified overload = overloads.at(i);
// use concatenated namespaces as key
2014-12-01 03:12:03 +08:00
string str_ns = qualifiedName("", overload.namespaces());
const ReturnValue& ret = returnValue(i);
const ArgumentList& args = argumentList(i);
grouped_functions[str_ns].addOverload(overload, args, ret);
}
size_t lastcheck = grouped_functions.size();
for(const GlobalFunctionMap::value_type& p: grouped_functions) {
2014-05-26 04:01:30 +08:00
p.second.generateSingleFunction(toolboxPath, wrapperName, typeAttributes,
file, functionNames);
if (--lastcheck != 0)
2014-05-26 04:01:30 +08:00
file.oss << endl;
}
}
/* ************************************************************************* */
void GlobalFunction::generateSingleFunction(const string& toolboxPath,
const string& wrapperName, const TypeAttributesTable& typeAttributes,
FileWriter& file, vector<string>& functionNames) const {
// create the folder for the namespace
const Qualified& overload1 = overloads.front();
2014-12-01 03:12:03 +08:00
createNamespaceStructure(overload1.namespaces(), toolboxPath);
// open destination mfunctionFileName
string mfunctionFileName = overload1.matlabName(toolboxPath);
FileWriter mfunctionFile(mfunctionFileName, verbose_, "%");
// get the name of actual matlab object
const string matlabQualName = overload1.qualifiedName(".");
const string matlabUniqueName = overload1.qualifiedName("");
const string cppName = overload1.qualifiedName("::");
mfunctionFile.oss << "function varargout = " << name_ << "(varargin)\n";
for (size_t i = 0; i < nrOverloads(); ++i) {
const ArgumentList& args = argumentList(i);
const ReturnValue& returnVal = returnValue(i);
const int id = functionNames.size();
// Output proxy matlab code
mfunctionFile.oss << " " << (i == 0 ? "" : "else");
emit_conditional_call(mfunctionFile, returnVal, args, wrapperName, id);
// Output C++ wrapper code
2014-05-26 04:01:30 +08:00
const string wrapFunctionName = matlabUniqueName + "_"
+ boost::lexical_cast<string>(id);
// call
2014-05-26 04:01:30 +08:00
file.oss << "void " << wrapFunctionName
<< "(int nargout, mxArray *out[], int nargin, const mxArray *in[])\n";
// start
2014-05-26 04:01:30 +08:00
file.oss << "{\n";
2014-05-26 04:01:30 +08:00
returnVal.wrapTypeUnwrap(file);
// check arguments
// NOTE: for static functions, there is no object passed
2014-05-26 04:01:30 +08:00
file.oss << " checkArguments(\"" << matlabUniqueName
<< "\",nargout,nargin," << args.size() << ");\n";
// unwrap arguments, see Argument.cpp
2014-05-26 04:01:30 +08:00
args.matlab_unwrap(file, 0); // We start at 0 because there is no self object
// call method with default type and wrap result
2014-12-01 03:12:03 +08:00
if (returnVal.type1.name() != "void")
2014-05-26 04:01:30 +08:00
returnVal.wrap_result(cppName + "(" + args.names() + ")", file,
typeAttributes);
else
2014-05-26 04:01:30 +08:00
file.oss << cppName + "(" + args.names() + ");\n";
// finish
2014-05-26 04:01:30 +08:00
file.oss << "}\n";
// Add to function list
functionNames.push_back(wrapFunctionName);
}
mfunctionFile.oss << " else\n";
2014-05-26 04:01:30 +08:00
mfunctionFile.oss
<< " error('Arguments do not match any overload of function "
2014-05-26 04:01:30 +08:00
<< matlabQualName << "');" << endl;
mfunctionFile.oss << " end" << endl;
// Close file
mfunctionFile.emit(true);
}
2014-11-15 00:47:25 +08:00
/* ************************************************************************* */
void GlobalFunction::python_wrapper(FileWriter& wrapperFile) const {
wrapperFile.oss << "def(\"" << name_ << "\", " << name_ << ");\n";
}
2016-11-23 06:09:35 +08:00
/* ************************************************************************* */
void GlobalFunction::emit_cython_pxd(FileWriter& file) const {
file.oss << "cdef extern from \"" << includeFile << "\" namespace \""
<< overloads[0].qualifiedNamespaces("::")
<< "\":" << endl;
for (size_t i = 0; i < nrOverloads(); ++i) {
2016-11-30 18:56:07 +08:00
file.oss << " ";
returnVals_[i].emit_cython_pxd(file, "", vector<string>());
file.oss << pxdName() + " \"" + overloads[0].qualifiedName("::") +
2016-11-23 06:09:35 +08:00
"\"(";
argumentList(i).emit_cython_pxd(file, "", vector<string>());
2016-11-23 06:09:35 +08:00
file.oss << ")";
file.oss << "\n";
}
}
/* ************************************************************************* */
2016-11-23 06:32:48 +08:00
void GlobalFunction::emit_cython_pyx_no_overload(FileWriter& file) const {
string funcName = pyxName();
2016-11-23 06:09:35 +08:00
// Function definition
file.oss << "def " << funcName;
// modify name of function instantiation as python doesn't allow overloads
// e.g. template<T={A,B,C}> funcName(...) --> funcNameA, funcNameB, funcNameC
if (templateArgValue_) file.oss << templateArgValue_->pyxClassName();
2016-11-23 06:09:35 +08:00
// funtion arguments
file.oss << "(";
argumentList(0).emit_cython_pyx(file);
file.oss << "):\n";
/// Call cython corresponding function and return
string ret = pyx_functionCall("", pxdName(), 0);
2016-11-23 06:09:35 +08:00
if (!returnVals_[0].isVoid()) {
2016-11-30 18:56:07 +08:00
file.oss << " cdef " << returnVals_[0].pyx_returnType()
2016-11-23 06:09:35 +08:00
<< " ret = " << ret << "\n";
2016-11-30 18:56:07 +08:00
file.oss << " return " << returnVals_[0].pyx_casting("ret") << "\n";
2016-11-23 06:09:35 +08:00
} else {
2016-11-30 18:56:07 +08:00
file.oss << " " << ret << "\n";
2016-11-23 06:09:35 +08:00
}
}
2016-11-23 06:32:48 +08:00
/* ************************************************************************* */
void GlobalFunction::emit_cython_pyx(FileWriter& file) const {
string funcName = pyxName();
2016-11-23 06:32:48 +08:00
size_t N = nrOverloads();
if (N == 1) {
emit_cython_pyx_no_overload(file);
return;
}
// Dealing with overloads..
file.oss << "def " << funcName << "(*args, **kwargs):\n";
for (size_t i = 0; i < N; ++i) {
2016-11-30 18:56:07 +08:00
file.oss << " success, results = " << funcName << "_" << i
2016-11-23 06:32:48 +08:00
<< "(*args, **kwargs)\n";
2016-11-30 18:56:07 +08:00
file.oss << " if success:\n return results\n";
2016-11-23 06:32:48 +08:00
}
2016-11-30 18:56:07 +08:00
file.oss << " raise TypeError('Could not find the correct overload')\n";
2016-11-23 06:32:48 +08:00
for (size_t i = 0; i < N; ++i) {
ArgumentList args = argumentList(i);
file.oss << "def " + funcName + "_" + to_string(i) +
"(*args, **kwargs):\n";
file.oss << pyx_resolveOverloadParams(args, false, 1); // lazy: always return None even if it's a void function
/// Call cython corresponding function
string ret = pyx_functionCall("", pxdName(), i);
2016-11-23 06:32:48 +08:00
if (!returnVals_[i].isVoid()) {
2016-11-30 18:56:07 +08:00
file.oss << " cdef " << returnVals_[i].pyx_returnType()
2016-11-23 06:32:48 +08:00
<< " ret = " << ret << "\n";
2016-11-30 18:56:07 +08:00
file.oss << " return True, " << returnVals_[i].pyx_casting("ret") << "\n";
2016-11-23 06:32:48 +08:00
} else {
2016-11-30 18:56:07 +08:00
file.oss << " " << ret << "\n";
file.oss << " return True, None\n";
2016-11-23 06:32:48 +08:00
}
}
}
/* ************************************************************************* */
} // \namespace wrap