From 11ab035380c5c8bf8a4ed3decee73900993dc50e Mon Sep 17 00:00:00 2001 From: dellaert Date: Thu, 22 Jan 2015 03:34:39 +0100 Subject: [PATCH] Expand with intList --- wrap/Class.cpp | 20 ++++++++++++++++++++ wrap/Class.h | 4 ++++ wrap/tests/testClass.cpp | 23 +++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/wrap/Class.cpp b/wrap/Class.cpp index 6e415065d..e62e31bc1 100644 --- a/wrap/Class.cpp +++ b/wrap/Class.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include // std::ostream_iterator //#include // on Linux GCC: fails with error regarding needing C++0x std flags //#include // same failure as above @@ -314,6 +315,25 @@ vector Class::expandTemplate(Str templateArg, return result; } +/* ************************************************************************* */ +vector Class::expandTemplate(Str templateArg, + const vector& integers) const { + vector result; + BOOST_FOREACH(int i, integers) { + Qualified expandedClass = (Qualified) (*this); + stringstream ss; ss << i; + string instName = ss.str(); + expandedClass.expand(instName); + const TemplateSubstitution ts(templateArg, instName, expandedClass); + Class inst = expandTemplate(ts); + inst.name_ = expandedClass.name(); + inst.templateArgs.clear(); + inst.typedefName = qualifiedName("::") + "<" + instName + ">"; + result.push_back(inst); + } + return result; +} + /* ************************************************************************* */ void Class::addMethod(bool verbose, bool is_const, Str methodName, const ArgumentList& argumentList, const ReturnValue& returnValue, diff --git a/wrap/Class.h b/wrap/Class.h index f4c687eca..2f7457f06 100644 --- a/wrap/Class.h +++ b/wrap/Class.h @@ -106,6 +106,10 @@ public: std::vector expandTemplate(Str templateArg, const std::vector& instantiations) const; + // Create new classes with integer template arguments + std::vector expandTemplate(Str templateArg, + const std::vector& integers) const; + /// Add potentially overloaded, potentially templated method void addMethod(bool verbose, bool is_const, Str methodName, const ArgumentList& argumentList, const ReturnValue& returnValue, diff --git a/wrap/tests/testClass.cpp b/wrap/tests/testClass.cpp index a133e15ac..3d8d79b33 100644 --- a/wrap/tests/testClass.cpp +++ b/wrap/tests/testClass.cpp @@ -221,6 +221,29 @@ TEST( Class, TemplateSubstition ) { } +//****************************************************************************** +TEST( Class, TemplateSubstitionIntList ) { + + using classic::space_p; + + // Create type grammar that will place result in cls + Class cls; + Template t; + ClassGrammar g(cls, t); + + string markup(string("template" + "class Point2 {" + " void myMethod(Matrix A) const;" + "};")); + + EXPECT(parse(markup.c_str(), g, space_p).full); + + vector classes = cls.expandTemplate(t.argName(), t.intList()); + + // check the number of new classes is 2 + EXPECT_LONGS_EQUAL(2, classes.size()); +} + //****************************************************************************** TEST(Class, Template) {