Made methods and global functions derive from Function

release/4.3a0
dellaert 2014-11-13 12:52:41 +01:00
parent 16ba6ba254
commit a5e0adb7e6
16 changed files with 146 additions and 197 deletions

View File

@ -28,12 +28,37 @@
using namespace std; using namespace std;
using namespace wrap; using namespace wrap;
/* ************************************************************************* */
Argument Argument::expandTemplate(const string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) const {
Argument instArg = *this;
if (type.name == templateArg) {
instArg.type = qualifiedType;
} else if (type.name == "This") {
instArg.type = expandedClass;
}
return instArg;
}
/* ************************************************************************* */
ArgumentList ArgumentList::expandTemplate(const string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) const {
ArgumentList instArgList;
BOOST_FOREACH(const Argument& arg, *this) {
Argument instArg = arg.expandTemplate(templateArg, qualifiedType,
expandedClass);
instArgList.push_back(instArg);
}
return instArgList;
}
/* ************************************************************************* */ /* ************************************************************************* */
string Argument::matlabClass(const string& delim) const { string Argument::matlabClass(const string& delim) const {
string result; string result;
BOOST_FOREACH(const string& ns, type.namespaces) BOOST_FOREACH(const string& ns, type.namespaces)
result += ns + delim; result += ns + delim;
if (type.name == "string" || type.name == "unsigned char" || type.name == "char") if (type.name == "string" || type.name == "unsigned char"
|| type.name == "char")
return result + "char"; return result + "char";
if (type.name == "Vector" || type.name == "Matrix") if (type.name == "Vector" || type.name == "Matrix")
return result + "double"; return result + "double";
@ -46,8 +71,9 @@ string Argument::matlabClass(const string& delim) const {
/* ************************************************************************* */ /* ************************************************************************* */
bool Argument::isScalar() const { bool Argument::isScalar() const {
return (type.name == "bool" || type.name == "char" || type.name == "unsigned char" return (type.name == "bool" || type.name == "char"
|| type.name == "int" || type.name == "size_t" || type.name == "double"); || type.name == "unsigned char" || type.name == "int"
|| type.name == "size_t" || type.name == "double");
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -128,7 +154,8 @@ string ArgumentList::names() const {
/* ************************************************************************* */ /* ************************************************************************* */
bool ArgumentList::allScalar() const { bool ArgumentList::allScalar() const {
BOOST_FOREACH(Argument arg, *this) BOOST_FOREACH(Argument arg, *this)
if (!arg.isScalar()) return false; if (!arg.isScalar())
return false;
return true; return true;
} }

View File

@ -35,6 +35,9 @@ struct Argument {
is_const(false), is_ref(false), is_ptr(false) { is_const(false), is_ref(false), is_ptr(false) {
} }
Argument expandTemplate(const std::string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) const;
/// return MATLAB class for use in isa(x,class) /// return MATLAB class for use in isa(x,class)
std::string matlabClass(const std::string& delim = "") const; std::string matlabClass(const std::string& delim = "") const;
@ -60,6 +63,9 @@ struct ArgumentList: public std::vector<Argument> {
/// Check if all arguments scalar /// Check if all arguments scalar
bool allScalar() const; bool allScalar() const;
ArgumentList expandTemplate(const std::string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) const;
// MATLAB code generation: // MATLAB code generation:
/** /**
@ -93,8 +99,9 @@ struct ArgumentList: public std::vector<Argument> {
* @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& proxyFile, const ReturnValue& returnVal, void emit_conditional_call(FileWriter& proxyFile,
const std::string& wrapperName, int id, bool staticMethod = false) const; const ReturnValue& returnVal, const std::string& wrapperName, int id,
bool staticMethod = false) const;
}; };
template<class T> template<class T>
@ -108,7 +115,7 @@ inline void verifyArguments(const std::vector<std::string>& validArgs,
std::string fullType = arg.type.qualifiedName("::"); std::string fullType = arg.type.qualifiedName("::");
if (find(validArgs.begin(), validArgs.end(), fullType) if (find(validArgs.begin(), validArgs.end(), fullType)
== validArgs.end()) == validArgs.end())
throw DependencyMissing(fullType, t.name); throw DependencyMissing(fullType, t.name_);
} }
} }
} }

View File

@ -239,63 +239,6 @@ void Class::pointer_constructor_fragments(FileWriter& proxyFile,
"}\n"; "}\n";
} }
/* ************************************************************************* */
static vector<ArgumentList> expandArgumentListsTemplate(
const vector<ArgumentList>& argLists, const string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) {
vector<ArgumentList> result;
BOOST_FOREACH(const ArgumentList& argList, argLists) {
ArgumentList instArgList;
BOOST_FOREACH(const Argument& arg, argList) {
Argument instArg = arg;
if (arg.type.name == templateArg) {
instArg.type = qualifiedType;
} else if (arg.type.name == "This") {
instArg.type = expandedClass;
}
instArgList.push_back(instArg);
}
result.push_back(instArgList);
}
return result;
}
/* ************************************************************************* */
// TODO, Method, StaticMethod, and GlobalFunction should have common base ?
template<class METHOD>
METHOD expandMethodTemplate(const METHOD& method, const string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) {
// Create new instance
METHOD instMethod = method;
// substitute template in arguments
instMethod.argLists = expandArgumentListsTemplate(method.argLists,
templateArg, qualifiedType, expandedClass);
// do the same for return types
instMethod.returnVals.clear();
BOOST_FOREACH(const ReturnValue& retVal, method.returnVals) {
ReturnValue instRetVal = retVal.substituteTemplate(templateArg,
qualifiedType, expandedClass);
instMethod.returnVals.push_back(instRetVal);
}
// return new method
return instMethod;
}
/* ************************************************************************* */
template<class METHOD>
static map<string, METHOD> expandMethodTemplate(
const map<string, METHOD>& methods, const string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) {
map<string, METHOD> result;
typedef pair<const string, METHOD> NamedMethod;
BOOST_FOREACH(NamedMethod namedMethod, methods) {
namedMethod.second = expandMethodTemplate(namedMethod.second, templateArg,
qualifiedType, expandedClass);
result.insert(namedMethod);
}
return result;
}
/* ************************************************************************* */ /* ************************************************************************* */
Class Class::expandTemplate(const string& templateArg, Class Class::expandTemplate(const string& templateArg,
const Qualified& instName, const Qualified& expandedClass) const { const Qualified& instName, const Qualified& expandedClass) const {
@ -304,8 +247,8 @@ Class Class::expandTemplate(const string& templateArg,
expandedClass); expandedClass);
inst.static_methods = expandMethodTemplate(static_methods, templateArg, inst.static_methods = expandMethodTemplate(static_methods, templateArg,
instName, expandedClass); instName, expandedClass);
inst.constructor.args_list = expandArgumentListsTemplate( inst.constructor.args_list = inst.constructor.expandArgumentListsTemplate(
constructor.args_list, templateArg, instName, expandedClass); templateArg, instName, expandedClass);
inst.constructor.name = inst.name; inst.constructor.name = inst.name;
inst.deconstructor.name = inst.name; inst.deconstructor.name = inst.name;
return inst; return inst;
@ -335,14 +278,17 @@ void Class::addMethod(bool verbose, bool is_const, const string& name,
// Check if templated // Check if templated
if (!templateArgName.empty() && templateArgValues.size() > 0) { if (!templateArgName.empty() && templateArgValues.size() > 0) {
// Create method to expand // Create method to expand
Method method;
method.addOverload(verbose, is_const, name, args, retVal);
// For all values of the template argument, create a new method // For all values of the template argument, create a new method
BOOST_FOREACH(const Qualified& instName, templateArgValues) { BOOST_FOREACH(const Qualified& instName, templateArgValues) {
Method expanded = // string expandedName = name + instName.name;
expandMethodTemplate(method, templateArgName, instName, *this); // substitute template in arguments
methods[name].addOverload(verbose, is_const, name, expanded.argLists[0], ArgumentList expandedArgs = args.expandTemplate(templateArgName, instName,
expanded.returnVals[0], instName); name);
// do the same for return types
ReturnValue expandedRetVal = retVal.expandTemplate(templateArgName,
instName, name);
methods[expandedName].addOverload(verbose, is_const, expandedName,
expandedArgs, expandedRetVal, instName);
} }
} else } else
// just add overload // just add overload
@ -446,7 +392,7 @@ void Class::comment_fragment(FileWriter& proxyFile) const {
const Method& m = name_m.second; const Method& m = name_m.second;
BOOST_FOREACH(ArgumentList argList, m.argLists) { BOOST_FOREACH(ArgumentList argList, m.argLists) {
proxyFile.oss << "%"; proxyFile.oss << "%";
argList.emit_prototype(proxyFile, m.name); argList.emit_prototype(proxyFile, m.name_);
proxyFile.oss << " : returns " << m.returnVals[0].return_type(false) proxyFile.oss << " : returns " << m.returnVals[0].return_type(false)
<< endl; << endl;
} }
@ -458,7 +404,7 @@ void Class::comment_fragment(FileWriter& proxyFile) const {
const StaticMethod& m = name_m.second; const StaticMethod& m = name_m.second;
BOOST_FOREACH(ArgumentList argList, m.argLists) { BOOST_FOREACH(ArgumentList argList, m.argLists) {
proxyFile.oss << "%"; proxyFile.oss << "%";
argList.emit_prototype(proxyFile, m.name); argList.emit_prototype(proxyFile, m.name_);
proxyFile.oss << " : returns " << m.returnVals[0].return_type(false) proxyFile.oss << " : returns " << m.returnVals[0].return_type(false)
<< endl; << endl;
} }

View File

@ -36,6 +36,19 @@ string Constructor::matlab_wrapper_name(const string& className) const {
return str; return str;
} }
/* ************************************************************************* */
vector<ArgumentList> Constructor::expandArgumentListsTemplate(
const string& templateArg, const Qualified& qualifiedType,
const Qualified& expandedClass) const {
vector<ArgumentList> result;
BOOST_FOREACH(const ArgumentList& argList, args_list) {
ArgumentList instArgList = argList.expandTemplate(templateArg,
qualifiedType, expandedClass);
result.push_back(instArgList);
}
return result;
}
/* ************************************************************************* */ /* ************************************************************************* */
void Constructor::proxy_fragment(FileWriter& file, const std::string& wrapperName, void Constructor::proxy_fragment(FileWriter& file, const std::string& wrapperName,
bool hasParent, const int id, const ArgumentList args) const { bool hasParent, const int id, const ArgumentList args) const {

View File

@ -38,6 +38,11 @@ struct Constructor {
std::string name; std::string name;
bool verbose_; bool verbose_;
// TODO eliminate copy/paste with function
std::vector<ArgumentList> expandArgumentListsTemplate(
const std::string& templateArg, const Qualified& qualifiedType,
const Qualified& expandedClass) const;
// MATLAB code generation // MATLAB code generation
// toolboxPath is main toolbox directory, e.g., ../matlab // toolboxPath is main toolbox directory, e.g., ../matlab
// classFile is class proxy file, e.g., ../matlab/@Point2/Point2.m // classFile is class proxy file, e.g., ../matlab/@Point2/Point2.m

View File

@ -17,16 +17,9 @@ using namespace std;
/* ************************************************************************* */ /* ************************************************************************* */
void GlobalFunction::addOverload(bool verbose, const Qualified& overload, void GlobalFunction::addOverload(bool verbose, const Qualified& overload,
const ArgumentList& args, const ReturnValue& retVal) { const ArgumentList& args, const ReturnValue& retVal,
if (name.empty()) const Qualified& instName) {
name = overload.name; Function::addOverload(verbose, overload.name, args, retVal, instName);
else if (overload.name != name)
throw std::runtime_error(
"GlobalFunction::addOverload: tried to add overload with name "
+ overload.name + " instead of expected " + name);
verbose_ = verbose;
argLists.push_back(args);
returnVals.push_back(retVal);
overloads.push_back(overload); overloads.push_back(overload);
} }
@ -48,7 +41,7 @@ void GlobalFunction::matlab_proxy(const std::string& toolboxPath,
ArgumentList args = argLists.at(i); ArgumentList args = argLists.at(i);
if (!grouped_functions.count(str_ns)) if (!grouped_functions.count(str_ns))
grouped_functions[str_ns] = GlobalFunction(name, verbose_); grouped_functions[str_ns] = GlobalFunction(name_, verbose_);
grouped_functions[str_ns].argLists.push_back(args); grouped_functions[str_ns].argLists.push_back(args);
grouped_functions[str_ns].returnVals.push_back(ret); grouped_functions[str_ns].returnVals.push_back(ret);
@ -82,7 +75,7 @@ void GlobalFunction::generateSingleFunction(const std::string& toolboxPath,
const string matlabUniqueName = overload1.qualifiedName(""); const string matlabUniqueName = overload1.qualifiedName("");
const string cppName = overload1.qualifiedName("::"); const string cppName = overload1.qualifiedName("::");
mfunctionFile.oss << "function varargout = " << name << "(varargin)\n"; 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 ArgumentList& args = argLists[overload];

View File

@ -9,34 +9,28 @@
#pragma once #pragma once
#include "Argument.h" #include "Function.h"
#include "ReturnValue.h"
namespace wrap { namespace wrap {
struct GlobalFunction { struct GlobalFunction: public Function {
bool verbose_; std::vector<Qualified> overloads; ///< Stack of qualified names
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<Qualified> overloads; ///< Stack of qualified names
// Constructor only used in Module // Constructor only used in Module
GlobalFunction(bool verbose = true) : GlobalFunction(bool verbose = true) :
verbose_(verbose) { Function(verbose) {
} }
// Used to reconstruct // Used to reconstruct
GlobalFunction(const std::string& name_, bool verbose = true) : GlobalFunction(const std::string& name, bool verbose = true) :
verbose_(verbose), name(name_) { Function(name,verbose) {
} }
// adds an overloaded version of this function // adds an overloaded version of this function,
void addOverload(bool verbose, const Qualified& overload, void addOverload(bool verbose, const Qualified& overload,
const ArgumentList& args, const ReturnValue& retVal); const ArgumentList& args, const ReturnValue& retVal,
const Qualified& instName = Qualified());
// codegen function called from Module to build the cpp and matlab versions of the function // codegen function called from Module to build the cpp and matlab versions of the function
void matlab_proxy(const std::string& toolboxPath, void matlab_proxy(const std::string& toolboxPath,

View File

@ -29,20 +29,12 @@ using namespace std;
using namespace wrap; using namespace wrap;
/* ************************************************************************* */ /* ************************************************************************* */
void Method::addOverload(bool verbose, bool is_const, const std::string& name, void Method::addOverload(bool verbose, bool is_const, const std::string& name_,
const ArgumentList& args, const ReturnValue& retVal, const ArgumentList& args, const ReturnValue& retVal,
const Qualified& instName) { const Qualified& instName) {
if (!this->name.empty() && this->name != name)
throw std::runtime_error( Function::addOverload(verbose, name_, args, retVal);
"Method::addOverload: tried to add overload with name " + name
+ " instead of expected " + this->name);
else
this->name = name;
verbose_ = verbose;
is_const_ = is_const; is_const_ = is_const;
argLists.push_back(args);
returnVals.push_back(retVal);
templateArgValues.push_back(instName);
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -53,14 +45,14 @@ void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
vector<string>& functionNames) const { vector<string>& functionNames) const {
// Create function header // Create function header
proxyFile.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_);
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) {
argList.emit_prototype(proxyFile, name); argList.emit_prototype(proxyFile, name_);
if (argLCount != argLists.size() - 1) if (argLCount != argLists.size() - 1)
proxyFile.oss << ", "; proxyFile.oss << ", ";
else else
@ -80,7 +72,7 @@ void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
proxyFile.oss << " % " << "Method Overloads" << endl; proxyFile.oss << " % " << "Method Overloads" << endl;
BOOST_FOREACH(ArgumentList argList, argLists) { BOOST_FOREACH(ArgumentList argList, argLists) {
proxyFile.oss << " % "; proxyFile.oss << " % ";
argList.emit_prototype(proxyFile, name); argList.emit_prototype(proxyFile, name_);
proxyFile.oss << endl; proxyFile.oss << endl;
} }
} }
@ -94,7 +86,7 @@ void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
// Output C++ wrapper code // Output C++ wrapper code
const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName, const string wrapFunctionName = wrapper_fragment(wrapperFile, cppClassName,
matlabUniqueName, 0, id, typeAttributes, templateArgValues[0]); matlabUniqueName, 0, id, typeAttributes, templateArgValue_);
// Add to function list // Add to function list
functionNames.push_back(wrapFunctionName); functionNames.push_back(wrapFunctionName);
@ -105,13 +97,16 @@ void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
// Output proxy matlab code // Output proxy matlab code
proxyFile.oss << " " << (overload == 0 ? "" : "else"); proxyFile.oss << " " << (overload == 0 ? "" : "else");
const int id = (int) functionNames.size(); const int id = (int) functionNames.size();
string expanded = wrapperName;
if (!templateArgValue_.empty())
expanded += templateArgValue_.name;
argLists[overload].emit_conditional_call(proxyFile, returnVals[overload], argLists[overload].emit_conditional_call(proxyFile, returnVals[overload],
wrapperName, id); expanded, id);
// Output C++ wrapper code // Output C++ wrapper code
const string wrapFunctionName = wrapper_fragment(wrapperFile, const string wrapFunctionName = wrapper_fragment(wrapperFile,
cppClassName, matlabUniqueName, overload, id, typeAttributes, cppClassName, matlabUniqueName, overload, id, typeAttributes,
templateArgValues[overload]); templateArgValue_);
// Add to function list // Add to function list
functionNames.push_back(wrapFunctionName); functionNames.push_back(wrapFunctionName);
@ -119,7 +114,7 @@ void Method::proxy_wrapper_fragments(FileWriter& proxyFile,
proxyFile.oss << " else\n"; proxyFile.oss << " else\n";
proxyFile.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;
proxyFile.oss << " end\n"; proxyFile.oss << " end\n";
} }
@ -134,7 +129,7 @@ string Method::wrapper_fragment(FileWriter& wrapperFile,
// generate code // generate code
const string wrapFunctionName = matlabUniqueName + "_" + name + "_" const string wrapFunctionName = matlabUniqueName + "_" + name_ + "_"
+ boost::lexical_cast<string>(id); + boost::lexical_cast<string>(id);
const ArgumentList& args = argLists[overload]; const ArgumentList& args = argLists[overload];
@ -154,7 +149,7 @@ string Method::wrapper_fragment(FileWriter& wrapperFile,
// 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);
wrapperFile.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
@ -166,7 +161,7 @@ string Method::wrapper_fragment(FileWriter& wrapperFile,
// 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));
string expanded = "obj->" + name; string expanded = "obj->" + name_;
if (!instName.empty()) if (!instName.empty())
expanded += ("<" + instName.qualifiedName("::") + ">"); expanded += ("<" + instName.qualifiedName("::") + ">");
expanded += ("(" + args.names() + ")"); expanded += ("(" + args.names() + ")");

View File

@ -18,30 +18,19 @@
#pragma once #pragma once
#include "Argument.h" #include "Function.h"
#include "ReturnValue.h"
#include "TypeAttributesTable.h"
#include <string>
#include <list>
namespace wrap { namespace wrap {
/// Method class /// Method class
struct Method { struct Method : public Function {
/// Constructor creates empty object /// Constructor creates empty object
Method(bool verbose = true) : Method(bool verbose = true) :
verbose_(verbose), is_const_(false) { Function(verbose), is_const_(false) {
} }
// Then the instance variables are set directly by the Module constructor
bool verbose_;
bool is_const_; bool is_const_;
std::string name;
std::vector<ArgumentList> argLists;
std::vector<ReturnValue> returnVals;
std::vector<Qualified> templateArgValues; ///< value of template argument if applicable
// The first time this function is called, it initializes the class members // The first time this function is called, it initializes the class members
// with those in rhs, but in subsequent calls it adds additional argument // with those in rhs, but in subsequent calls it adds additional argument

View File

@ -274,7 +274,7 @@ void Module::parseMarkup(const std::string& data) {
'(' >> argumentList_p >> ')' >> ';' >> *comments_p) '(' >> argumentList_p >> ')' >> ';' >> *comments_p)
[bl::bind(&StaticMethod::addOverload, [bl::bind(&StaticMethod::addOverload,
bl::var(cls.static_methods)[bl::var(methodName)], bl::var(cls.static_methods)[bl::var(methodName)],
verbose, bl::var(methodName), bl::var(args), bl::var(retVal))] verbose, bl::var(methodName), bl::var(args), bl::var(retVal), Qualified())]
[assign_a(retVal,retVal0)] [assign_a(retVal,retVal0)]
[clear_a(args)]; [clear_a(args)];
@ -313,7 +313,7 @@ void Module::parseMarkup(const std::string& data) {
[assign_a(globalFunction.namespaces,namespaces)] [assign_a(globalFunction.namespaces,namespaces)]
[bl::bind(&GlobalFunction::addOverload, [bl::bind(&GlobalFunction::addOverload,
bl::var(global_functions)[bl::var(globalFunction.name)], bl::var(global_functions)[bl::var(globalFunction.name)],
verbose, bl::var(globalFunction), bl::var(args), bl::var(retVal))] verbose, bl::var(globalFunction), bl::var(args), bl::var(retVal), Qualified())]
[assign_a(retVal,retVal0)] [assign_a(retVal,retVal0)]
[clear_a(globalFunction)] [clear_a(globalFunction)]
[clear_a(args)]; [clear_a(args)];

View File

@ -44,6 +44,10 @@ struct Qualified {
name.clear(); name.clear();
} }
bool operator!=(const Qualified& other) const {
return other.name!=name || other.namespaces != namespaces;
}
/// Return a qualified string using given delimiter /// Return a qualified string using given delimiter
std::string qualifiedName(const std::string& delimiter = "") const { std::string qualifiedName(const std::string& delimiter = "") const {
std::string result; std::string result;

View File

@ -59,7 +59,7 @@ void ReturnType::wrapTypeUnwrap(FileWriter& wrapperFile) const {
} }
/* ************************************************************************* */ /* ************************************************************************* */
ReturnValue ReturnValue::substituteTemplate(const string& templateArg, ReturnValue ReturnValue::expandTemplate(const string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) const { const Qualified& qualifiedType, const Qualified& expandedClass) const {
ReturnValue instRetVal = *this; ReturnValue instRetVal = *this;
if (type1.name == templateArg) { if (type1.name == templateArg) {

View File

@ -86,9 +86,22 @@ struct ReturnValue {
} }
/// Substitute template argument /// Substitute template argument
ReturnValue substituteTemplate(const std::string& templateArg, ReturnValue expandTemplate(const std::string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) const; const Qualified& qualifiedType, const Qualified& expandedClass) const;
// TODO use transform ?
static std::vector<ReturnValue> ExpandTemplate(
std::vector<ReturnValue> returnVals, const std::string& templateArg,
const Qualified& qualifiedType, const Qualified& expandedClass) {
std::vector<ReturnValue> result;
BOOST_FOREACH(const ReturnValue& retVal, returnVals) {
ReturnValue instRetVal = retVal.expandTemplate(templateArg,
qualifiedType, expandedClass);
result.push_back(instRetVal);
}
return result;
}
std::string return_type(bool add_ptr) const; std::string return_type(bool add_ptr) const;
std::string matlab_returnType() const; std::string matlab_returnType() const;
@ -102,18 +115,4 @@ struct ReturnValue {
}; };
template<class T>
inline void verifyReturnTypes(const std::vector<std::string>& validtypes,
const std::map<std::string, T>& vt) {
typedef typename std::map<std::string, T>::value_type NamedMethod;
BOOST_FOREACH(const NamedMethod& namedMethod, vt) {
const T& t = namedMethod.second;
BOOST_FOREACH(const ReturnValue& retval, t.returnVals) {
retval.type1.verify(validtypes, t.name);
if (retval.isPair)
retval.type2.verify(validtypes, t.name);
}
}
}
} // \namespace wrap } // \namespace wrap

View File

@ -29,15 +29,6 @@
using namespace std; using namespace std;
using namespace wrap; using namespace wrap;
/* ************************************************************************* */
void StaticMethod::addOverload(bool verbose, const std::string& name,
const ArgumentList& args, const ReturnValue& retVal) {
this->verbose = verbose;
this->name = name;
this->argLists.push_back(args);
this->returnVals.push_back(retVal);
}
/* ************************************************************************* */ /* ************************************************************************* */
void StaticMethod::proxy_wrapper_fragments(FileWriter& file, void StaticMethod::proxy_wrapper_fragments(FileWriter& file,
FileWriter& wrapperFile, const string& cppClassName, FileWriter& wrapperFile, const string& cppClassName,
@ -45,16 +36,16 @@ void StaticMethod::proxy_wrapper_fragments(FileWriter& file,
const string& wrapperName, const TypeAttributesTable& typeAttributes, const string& wrapperName, const TypeAttributesTable& typeAttributes,
vector<string>& functionNames) const { vector<string>& functionNames) const {
string upperName = name; string upperName = name_;
upperName[0] = std::toupper(upperName[0], std::locale()); upperName[0] = std::toupper(upperName[0], std::locale());
file.oss << " function varargout = " << upperName << "(varargin)\n"; file.oss << " function varargout = " << upperName << "(varargin)\n";
//Comments for documentation //Comments for documentation
string up_name = boost::to_upper_copy(name); string up_name = boost::to_upper_copy(name_);
file.oss << " % " << up_name << " usage: "; file.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(file, name_);
if (argLCount != argLists.size() - 1) if (argLCount != argLists.size() - 1)
file.oss << ", "; file.oss << ", ";
else else
@ -105,7 +96,7 @@ string StaticMethod::wrapper_fragment(FileWriter& file,
// generate code // generate code
const string wrapFunctionName = matlabUniqueName + "_" + name + "_" const string wrapFunctionName = matlabUniqueName + "_" + name_ + "_"
+ boost::lexical_cast<string>(id); + boost::lexical_cast<string>(id);
const ArgumentList& args = argLists[overload]; const ArgumentList& args = argLists[overload];
@ -124,7 +115,7 @@ string StaticMethod::wrapper_fragment(FileWriter& file,
// check arguments // check arguments
// NOTE: for static functions, there is no object passed // NOTE: for static functions, there is no object passed
file.oss << " checkArguments(\"" << matlabUniqueName << "." << name file.oss << " checkArguments(\"" << matlabUniqueName << "." << name_
<< "\",nargout,nargin," << args.size() << ");\n"; << "\",nargout,nargin," << args.size() << ");\n";
// unwrap arguments, see Argument.cpp // unwrap arguments, see Argument.cpp
@ -132,10 +123,10 @@ string StaticMethod::wrapper_fragment(FileWriter& file,
// call method with default type and wrap result // call method with default type and wrap result
if (returnVal.type1.name != "void") if (returnVal.type1.name != "void")
returnVal.wrap_result(cppClassName + "::" + name + "(" + args.names() + ")", returnVal.wrap_result(cppClassName + "::" + name_ + "(" + args.names() + ")",
file, typeAttributes); file, typeAttributes);
else else
file.oss << cppClassName + "::" + name + "(" + args.names() + ");\n"; file.oss << cppClassName + "::" + name_ + "(" + args.names() + ");\n";
// finish // finish
file.oss << "}\n"; file.oss << "}\n";

View File

@ -19,32 +19,18 @@
#pragma once #pragma once
#include "Argument.h" #include "Function.h"
#include "ReturnValue.h"
#include "TypeAttributesTable.h"
namespace wrap { namespace wrap {
/// StaticMethod class /// StaticMethod class
struct StaticMethod { struct StaticMethod: public Function {
/// Constructor creates empty object /// Constructor creates empty object
StaticMethod(bool verbosity = true) : StaticMethod(bool verbosity = true) :
verbose(verbosity) { Function(verbosity) {
} }
// Then the instance variables are set directly by the Module constructor
bool verbose;
std::string name;
std::vector<ArgumentList> argLists;
std::vector<ReturnValue> returnVals;
// The first time this function is called, it initializes the class members
// with those in rhs, but in subsequent calls it adds additional argument
// lists as function overloads.
void addOverload(bool verbose, const std::string& name,
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,

View File

@ -97,7 +97,7 @@ TEST( wrap, Small ) {
// Method 1 // Method 1
Method m1 = cls.method("x"); Method m1 = cls.method("x");
EXPECT(assert_equal("x", m1.name)); EXPECT(assert_equal("x", m1.name_));
EXPECT(m1.is_const_); EXPECT(m1.is_const_);
LONGS_EQUAL(1, m1.argLists.size()); LONGS_EQUAL(1, m1.argLists.size());
LONGS_EQUAL(1, m1.returnVals.size()); LONGS_EQUAL(1, m1.returnVals.size());
@ -110,7 +110,7 @@ TEST( wrap, Small ) {
// Method 2 // Method 2
Method m2 = cls.method("returnMatrix"); Method m2 = cls.method("returnMatrix");
EXPECT(assert_equal("returnMatrix", m2.name)); EXPECT(assert_equal("returnMatrix", m2.name_));
EXPECT(m2.is_const_); EXPECT(m2.is_const_);
LONGS_EQUAL(1, m2.argLists.size()); LONGS_EQUAL(1, m2.argLists.size());
LONGS_EQUAL(1, m2.returnVals.size()); LONGS_EQUAL(1, m2.returnVals.size());
@ -123,7 +123,7 @@ TEST( wrap, Small ) {
// Method 3 // Method 3
Method m3 = cls.method("returnPoint2"); Method m3 = cls.method("returnPoint2");
EXPECT(assert_equal("returnPoint2", m3.name)); EXPECT(assert_equal("returnPoint2", m3.name_));
EXPECT(m3.is_const_); EXPECT(m3.is_const_);
LONGS_EQUAL(1, m3.argLists.size()); LONGS_EQUAL(1, m3.argLists.size());
LONGS_EQUAL(1, m3.returnVals.size()); LONGS_EQUAL(1, m3.returnVals.size());
@ -137,7 +137,7 @@ TEST( wrap, Small ) {
// Static Method 1 // Static Method 1
// static Vector returnVector(); // static Vector returnVector();
StaticMethod sm1 = cls.static_methods.at("returnVector"); StaticMethod sm1 = cls.static_methods.at("returnVector");
EXPECT(assert_equal("returnVector", sm1.name)); EXPECT(assert_equal("returnVector", sm1.name_));
LONGS_EQUAL(1, sm1.argLists.size()); LONGS_EQUAL(1, sm1.argLists.size());
LONGS_EQUAL(1, sm1.returnVals.size()); LONGS_EQUAL(1, sm1.returnVals.size());
@ -199,7 +199,7 @@ TEST( wrap, Geometry ) {
EXPECT(assert_equal("char", m1.returnVals.front().type1.name)); EXPECT(assert_equal("char", m1.returnVals.front().type1.name));
EXPECT_LONGS_EQUAL(ReturnType::BASIS, m1.returnVals.front().type1.category); EXPECT_LONGS_EQUAL(ReturnType::BASIS, m1.returnVals.front().type1.category);
EXPECT(!m1.returnVals.front().isPair); EXPECT(!m1.returnVals.front().isPair);
EXPECT(assert_equal("returnChar", m1.name)); EXPECT(assert_equal("returnChar", m1.name_));
LONGS_EQUAL(1, m1.argLists.size()); LONGS_EQUAL(1, m1.argLists.size());
EXPECT_LONGS_EQUAL(0, m1.argLists.front().size()); EXPECT_LONGS_EQUAL(0, m1.argLists.front().size());
EXPECT(m1.is_const_); EXPECT(m1.is_const_);
@ -213,7 +213,7 @@ TEST( wrap, Geometry ) {
EXPECT(assert_equal("VectorNotEigen", m1.returnVals.front().type1.name)); EXPECT(assert_equal("VectorNotEigen", m1.returnVals.front().type1.name));
EXPECT_LONGS_EQUAL(ReturnType::CLASS, m1.returnVals.front().type1.category); EXPECT_LONGS_EQUAL(ReturnType::CLASS, m1.returnVals.front().type1.category);
EXPECT(!m1.returnVals.front().isPair); EXPECT(!m1.returnVals.front().isPair);
EXPECT(assert_equal("vectorConfusion", m1.name)); EXPECT(assert_equal("vectorConfusion", m1.name_));
LONGS_EQUAL(1, m1.argLists.size()); LONGS_EQUAL(1, m1.argLists.size());
EXPECT_LONGS_EQUAL(0, m1.argLists.front().size()); EXPECT_LONGS_EQUAL(0, m1.argLists.front().size());
EXPECT(!m1.is_const_); EXPECT(!m1.is_const_);
@ -255,7 +255,7 @@ TEST( wrap, Geometry ) {
LONGS_EQUAL(1, m1.returnVals.size()); LONGS_EQUAL(1, m1.returnVals.size());
EXPECT(assert_equal("double", m1.returnVals.front().type1.name)); EXPECT(assert_equal("double", m1.returnVals.front().type1.name));
EXPECT_LONGS_EQUAL(ReturnType::BASIS, m1.returnVals.front().type1.category); EXPECT_LONGS_EQUAL(ReturnType::BASIS, m1.returnVals.front().type1.category);
EXPECT(assert_equal("norm", m1.name)); EXPECT(assert_equal("norm", m1.name_));
LONGS_EQUAL(1, m1.argLists.size()); LONGS_EQUAL(1, m1.argLists.size());
EXPECT_LONGS_EQUAL(0, m1.argLists.front().size()); EXPECT_LONGS_EQUAL(0, m1.argLists.front().size());
EXPECT(m1.is_const_); EXPECT(m1.is_const_);
@ -316,7 +316,7 @@ TEST( wrap, Geometry ) {
CHECK(module.global_functions.find("aGlobalFunction") != module.global_functions.end()); CHECK(module.global_functions.find("aGlobalFunction") != module.global_functions.end());
{ {
GlobalFunction gfunc = module.global_functions.at("aGlobalFunction"); GlobalFunction gfunc = module.global_functions.at("aGlobalFunction");
EXPECT(assert_equal("aGlobalFunction", gfunc.name)); EXPECT(assert_equal("aGlobalFunction", gfunc.name_));
LONGS_EQUAL(1, gfunc.returnVals.size()); LONGS_EQUAL(1, gfunc.returnVals.size());
EXPECT(assert_equal("Vector", gfunc.returnVals.front().type1.name)); EXPECT(assert_equal("Vector", gfunc.returnVals.front().type1.name));
EXPECT_LONGS_EQUAL(1, gfunc.argLists.size()); EXPECT_LONGS_EQUAL(1, gfunc.argLists.size());
@ -390,7 +390,7 @@ TEST( wrap, parse_namespaces ) {
CHECK(module.global_functions.find("aGlobalFunction") != module.global_functions.end()); CHECK(module.global_functions.find("aGlobalFunction") != module.global_functions.end());
{ {
GlobalFunction gfunc = module.global_functions.at("aGlobalFunction"); GlobalFunction gfunc = module.global_functions.at("aGlobalFunction");
EXPECT(assert_equal("aGlobalFunction", gfunc.name)); EXPECT(assert_equal("aGlobalFunction", gfunc.name_));
LONGS_EQUAL(2, gfunc.returnVals.size()); LONGS_EQUAL(2, gfunc.returnVals.size());
EXPECT(assert_equal("Vector", gfunc.returnVals.front().type1.name)); EXPECT(assert_equal("Vector", gfunc.returnVals.front().type1.name));
EXPECT_LONGS_EQUAL(2, gfunc.argLists.size()); EXPECT_LONGS_EQUAL(2, gfunc.argLists.size());