Using TemplateGrammar

release/4.3a0
dellaert 2014-12-01 14:35:02 +01:00
parent 32852eeec7
commit 8eb6393c92
3 changed files with 26 additions and 27 deletions

View File

@ -43,7 +43,8 @@ void Class::assignParent(const Qualified& parent) {
/* ************************************************************************* */
boost::optional<string> Class::qualifiedParent() const {
boost::optional<string> result = boost::none;
if (parentClass) result = parentClass->qualifiedName("::");
if (parentClass)
result = parentClass->qualifiedName("::");
return result;
}
@ -316,13 +317,13 @@ vector<Class> Class::expandTemplate(Str templateArg,
/* ************************************************************************* */
void Class::addMethod(bool verbose, bool is_const, Str methodName,
const ArgumentList& argumentList, const ReturnValue& returnValue,
Str templateArgName, const vector<Qualified>& templateArgValues) {
const Template& tmplate) {
// Check if templated
if (!templateArgName.empty() && templateArgValues.size() > 0) {
if (!tmplate.argName.empty() && tmplate.argValues.size() > 0) {
// 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);
BOOST_FOREACH(const Qualified& instName, tmplate.argValues) {
const TemplateSubstitution ts(tmplate.argName, instName, *this);
// substitute template in arguments
ArgumentList expandedArgs = argumentList.expandTemplate(ts);
// do the same for return type

View File

@ -19,6 +19,7 @@
#pragma once
#include "Template.h"
#include "Constructor.h"
#include "Deconstructor.h"
#include "Method.h"
@ -95,7 +96,7 @@ public:
/// Add potentially overloaded, potentially templated method
void addMethod(bool verbose, bool is_const, Str methodName,
const ArgumentList& argumentList, const ReturnValue& returnValue,
Str templateArgName, const std::vector<Qualified>& templateArgValues);
const Template& tmplate);
/// Post-process classes for serialization markers
void erase_serialization(); // non-const !

View File

@ -114,15 +114,6 @@ void Module::parseMarkup(const std::string& data) {
// TODO, do we really need cls here? Non-local
Class cls0(verbose),cls(verbose);
// template<CALIBRATION = {gtsam::Cal3DS2}>
string templateArgName;
vector<Qualified> templateArgValues;
TypeListGrammar<'{','}'> templateArgValues_g(templateArgValues);
Rule templateArgValues_p =
(str_p("template") >>
'<' >> basic.name_p[assign_a(templateArgName)] >> '=' >>
templateArgValues_g >> '>');
// parse "gtsam::Pose2" and add to singleInstantiation.typeList
TemplateInstantiationTypedef singleInstantiation, singleInstantiation0;
TypeListGrammar<'<','>'> typelist_g(singleInstantiation.typeList);
@ -164,20 +155,24 @@ void Module::parseMarkup(const std::string& data) {
Rule methodName_p = lexeme_d[(upper_p | lower_p) >> *(alnum_p | '_')];
// template<CALIBRATION = {gtsam::Cal3DS2}>
Template methodTemplate;
TemplateGrammar methodTemplate_g(methodTemplate);
// gtsam::Values retract(const gtsam::VectorValues& delta) const;
string methodName;
bool isConst, isConst0 = false;
Rule method_p =
!templateArgValues_p >>
!methodTemplate_g >>
(returnValue_g >> methodName_p[assign_a(methodName)] >>
argumentList_g >>
!str_p("const")[assign_a(isConst,true)] >> ';' >> *basic.comments_p)
[bl::bind(&Class::addMethod, bl::var(cls), verbose, bl::var(isConst),
bl::var(methodName), bl::var(args), bl::var(retVal),
bl::var(templateArgName), bl::var(templateArgValues))]
bl::var(methodTemplate))]
[assign_a(retVal,retVal0)]
[clear_a(args)]
[clear_a(templateArgValues)]
[clear_a(methodTemplate)]
[assign_a(isConst,isConst0)];
Rule staticMethodName_p = lexeme_d[(upper_p | lower_p) >> *(alnum_p | '_')];
@ -193,17 +188,19 @@ void Module::parseMarkup(const std::string& data) {
Rule functions_p = constructor_p | method_p | static_method_p;
// template<CALIBRATION = {gtsam::Cal3DS2}>
Template classTemplate;
TemplateGrammar classTemplate_g(classTemplate);
// Parent class
Qualified possibleParent;
TypeGrammar classParent_p(possibleParent);
// parse a full class
vector<Qualified> templateInstantiations;
Rule class_p =
eps_p[assign_a(cls,cls0)]
>> (!(templateArgValues_p
[push_back_a(cls.templateArgs, templateArgName)]
[assign_a(templateInstantiations,templateArgValues)]
[clear_a(templateArgValues)]
>> (!(classTemplate_g
[push_back_a(cls.templateArgs, classTemplate.argName)]
| templateList_p)
>> !(str_p("virtual")[assign_a(cls.isVirtual, true)])
>> str_p("class")
@ -219,8 +216,8 @@ void Module::parseMarkup(const std::string& data) {
[assign_a(cls.namespaces_, namespaces)]
[assign_a(cls.deconstructor.name,cls.name_)]
[bl::bind(&handle_possible_template, bl::var(classes), bl::var(cls),
bl::var(templateInstantiations))]
[clear_a(templateInstantiations)]
bl::var(classTemplate.argValues))]
[clear_a(classTemplate)]
[assign_a(constructor, constructor0)]
[assign_a(cls,cls0)];