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++ /// MATLAB code generation, MATLAB to C++
void matlab_unwrap(FileWriter& file, const std::string& matlabName) const; 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 /// Argument list is just a container with Arguments
@ -100,6 +107,19 @@ struct ArgumentList: public std::vector<Argument> {
void emit_conditional_call(FileWriter& proxyFile, void emit_conditional_call(FileWriter& proxyFile,
const ReturnValue& returnVal, const std::string& wrapperName, int id, const ReturnValue& returnVal, const std::string& wrapperName, int id,
bool staticMethod = false) const; 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> 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; typedef typename std::map<std::string, T>::value_type NamedMethod;
BOOST_FOREACH(const NamedMethod& namedMethod, vt) { BOOST_FOREACH(const NamedMethod& namedMethod, vt) {
const T& t = namedMethod.second; 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.args_list = inst.constructor.expandArgumentListsTemplate(ts);
inst.constructor.name = inst.name; inst.constructor.name = inst.name;
inst.deconstructor.name = inst.name; inst.deconstructor.name = inst.name;
cout << inst << endl;
return inst; return inst;
} }
@ -254,10 +255,12 @@ Class Class::expandTemplate(const TemplateSubstitution& ts) const {
vector<Class> Class::expandTemplate(Str templateArg, vector<Class> Class::expandTemplate(Str templateArg,
const vector<Qualified>& instantiations) const { const vector<Qualified>& instantiations) const {
vector<Class> result; vector<Class> result;
cout << *this << endl;
BOOST_FOREACH(const Qualified& instName, instantiations) { BOOST_FOREACH(const Qualified& instName, instantiations) {
Qualified expandedClass = (Qualified) (*this); Qualified expandedClass = (Qualified) (*this);
expandedClass.name += instName.name; expandedClass.name += instName.name;
const TemplateSubstitution ts(templateArg, instName, expandedClass); const TemplateSubstitution ts(templateArg, instName, expandedClass);
cout << ts << endl;
Class inst = expandTemplate(ts); Class inst = expandTemplate(ts);
inst.name = expandedClass.name; inst.name = expandedClass.name;
inst.templateArgs.clear(); inst.templateArgs.clear();
@ -274,12 +277,10 @@ void Class::addMethod(bool verbose, bool is_const, Str methodName,
Str templateArgName, const vector<Qualified>& templateArgValues) { Str templateArgName, const vector<Qualified>& templateArgValues) {
// Check if templated // Check if templated
if (!templateArgName.empty() && templateArgValues.size() > 0) { if (!templateArgName.empty() && templateArgValues.size() > 0) {
cout << methodName << endl;
// Create method to expand // Create method to expand
// 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) {
const TemplateSubstitution ts(templateArgName, instName, this->name); const TemplateSubstitution ts(templateArgName, instName, this->name);
cout << ts << endl;
// substitute template in arguments // substitute template in arguments
ArgumentList expandedArgs = argumentList.expandTemplate(ts); ArgumentList expandedArgs = argumentList.expandTemplate(ts);
// do the same for return type // do the same for return type

View File

@ -19,15 +19,18 @@
#pragma once #pragma once
#include <string>
#include <map>
#include "Constructor.h" #include "Constructor.h"
#include "Deconstructor.h" #include "Deconstructor.h"
#include "Method.h" #include "Method.h"
#include "StaticMethod.h" #include "StaticMethod.h"
#include "TypeAttributesTable.h" #include "TypeAttributesTable.h"
#include <boost/foreach.hpp>
#include <boost/range/adaptor/map.hpp>
#include <string>
#include <map>
namespace wrap { namespace wrap {
/// Class has name, constructors, methods /// Class has name, constructors, methods
@ -108,6 +111,14 @@ public:
void deserialization_fragments(FileWriter& proxyFile, FileWriter& wrapperFile, void deserialization_fragments(FileWriter& proxyFile, FileWriter& wrapperFile,
Str wrapperName, std::vector<std::string>& functionNames) const; 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: private:
void pointer_constructor_fragments(FileWriter& proxyFile, 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 // Templated checking functions

View File

@ -41,6 +41,12 @@ struct Method: public StaticMethod {
const ArgumentList& args, const ReturnValue& retVal, const ArgumentList& args, const ReturnValue& retVal,
const Qualified& instName = Qualified()); 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: private:
// Emit method header // Emit method header

View File

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

View File

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

View File

@ -49,6 +49,14 @@ struct ReturnValue {
void emit_matlab(FileWriter& proxyFile) const; 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 } // \namespace wrap

View File

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