Stream operator for many classes

release/4.3a0
dellaert 2014-11-13 21:11:29 +01:00
parent 482dbd9226
commit efd544527f
9 changed files with 75 additions and 9 deletions

View File

@ -45,6 +45,13 @@ struct Argument {
/// MATLAB code generation, MATLAB to C++
void matlab_unwrap(FileWriter& file, const std::string& matlabName) const;
friend std::ostream& operator<<(std::ostream& os, const Argument& arg) {
os << (arg.is_const ? "const " : "") << arg.type << (arg.is_ptr ? "*" : "")
<< (arg.is_ref ? "&" : "");
return os;
}
};
/// Argument list is just a container with Arguments
@ -100,6 +107,19 @@ struct ArgumentList: public std::vector<Argument> {
void emit_conditional_call(FileWriter& proxyFile,
const ReturnValue& returnVal, const std::string& wrapperName, int id,
bool staticMethod = false) const;
friend std::ostream& operator<<(std::ostream& os,
const ArgumentList& argList) {
os << "(";
if (argList.size() > 0)
os << argList.front();
if (argList.size() > 1)
for (size_t i = 1; i < argList.size(); i++)
os << ", " << argList[i];
os << ")";
return os;
}
};
template<class T>
@ -108,7 +128,7 @@ inline void verifyArguments(const std::vector<std::string>& validArgs,
typedef typename std::map<std::string, T>::value_type NamedMethod;
BOOST_FOREACH(const NamedMethod& namedMethod, vt) {
const T& t = namedMethod.second;
t.verifyArguments(validArgs,t.name_);
t.verifyArguments(validArgs, t.name_);
}
}

View File

@ -247,6 +247,7 @@ Class Class::expandTemplate(const TemplateSubstitution& ts) const {
inst.constructor.args_list = inst.constructor.expandArgumentListsTemplate(ts);
inst.constructor.name = inst.name;
inst.deconstructor.name = inst.name;
cout << inst << endl;
return inst;
}
@ -254,10 +255,12 @@ Class Class::expandTemplate(const TemplateSubstitution& ts) const {
vector<Class> Class::expandTemplate(Str templateArg,
const vector<Qualified>& instantiations) const {
vector<Class> result;
cout << *this << endl;
BOOST_FOREACH(const Qualified& instName, instantiations) {
Qualified expandedClass = (Qualified) (*this);
expandedClass.name += instName.name;
const TemplateSubstitution ts(templateArg, instName, expandedClass);
cout << ts << endl;
Class inst = expandTemplate(ts);
inst.name = expandedClass.name;
inst.templateArgs.clear();
@ -274,12 +277,10 @@ void Class::addMethod(bool verbose, bool is_const, Str methodName,
Str templateArgName, const vector<Qualified>& templateArgValues) {
// Check if templated
if (!templateArgName.empty() && templateArgValues.size() > 0) {
cout << methodName << endl;
// Create method to expand
// For all values of the template argument, create a new method
BOOST_FOREACH(const Qualified& instName, templateArgValues) {
const TemplateSubstitution ts(templateArgName, instName, this->name);
cout << ts << endl;
// substitute template in arguments
ArgumentList expandedArgs = argumentList.expandTemplate(ts);
// do the same for return type

View File

@ -19,15 +19,18 @@
#pragma once
#include <string>
#include <map>
#include "Constructor.h"
#include "Deconstructor.h"
#include "Method.h"
#include "StaticMethod.h"
#include "TypeAttributesTable.h"
#include <boost/foreach.hpp>
#include <boost/range/adaptor/map.hpp>
#include <string>
#include <map>
namespace wrap {
/// Class has name, constructors, methods
@ -108,6 +111,14 @@ public:
void deserialization_fragments(FileWriter& proxyFile, FileWriter& wrapperFile,
Str wrapperName, std::vector<std::string>& functionNames) const;
friend std::ostream& operator<<(std::ostream& os, const Class& cls) {
os << "class " << cls.name << "{\n";
BOOST_FOREACH(const Method& m, cls.methods | boost::adaptors::map_values)
os << m << ";\n";
os << "};" << std::endl;
return os;
}
private:
void pointer_constructor_fragments(FileWriter& proxyFile,

View File

@ -93,6 +93,13 @@ public:
}
}
friend std::ostream& operator<<(std::ostream& os,
const ArgumentOverloads& overloads) {
BOOST_FOREACH(const ArgumentList& argList, overloads.argLists_)
os << argList << std::endl;
return os;
}
};
/**
@ -168,6 +175,13 @@ public:
}
}
friend std::ostream& operator<<(std::ostream& os,
const SignatureOverloads& overloads) {
for (size_t i = 0; i < overloads.nrOverloads(); i++)
os << overloads.returnVals_[i] << overloads.argLists_[i] << std::endl;
return os;
}
};
// Templated checking functions

View File

@ -41,6 +41,12 @@ struct Method: public StaticMethod {
const ArgumentList& args, const ReturnValue& retVal,
const Qualified& instName = Qualified());
friend std::ostream& operator<<(std::ostream& os, const Method& m) {
for (size_t i = 0; i < m.nrOverloads(); i++)
os << m.returnVals_[i] << " " << m.name_ << m.argLists_[i];
return os;
}
private:
// Emit method header

View File

@ -45,7 +45,7 @@ struct Qualified {
}
bool operator!=(const Qualified& other) const {
return other.name!=name || other.namespaces != namespaces;
return other.name != name || other.namespaces != namespaces;
}
/// Return a qualified string using given delimiter
@ -66,6 +66,11 @@ struct Qualified {
return result;
}
friend std::ostream& operator<<(std::ostream& os, const Qualified& q) {
os << q.qualifiedName("::");
return os;
}
};
} // \namespace wrap

View File

@ -18,7 +18,8 @@ using namespace wrap;
ReturnValue ReturnValue::expandTemplate(const TemplateSubstitution& ts) const {
ReturnValue instRetVal = *this;
instRetVal.type1 = ts(type1);
if (isPair) instRetVal.type2 = ts(type2);
if (isPair)
instRetVal.type2 = ts(type2);
return instRetVal;
}

View File

@ -49,6 +49,14 @@ struct ReturnValue {
void emit_matlab(FileWriter& proxyFile) const;
friend std::ostream& operator<<(std::ostream& os, const ReturnValue& r) {
if (!r.isPair && r.type1.category == ReturnType::VOID)
os << "void";
else
os << r.return_type(true);
return os;
}
};
} // \namespace wrap

View File

@ -51,7 +51,7 @@ public:
// Substitute if needed
ReturnType operator()(const ReturnType& type) const {
ReturnType instType;
ReturnType instType = type;
if (type.name == templateArg_ && type.namespaces.empty())
instType.rename(qualifiedType_);
else if (type.name == "This")