From fd5c0f2a8a114ce11390fee445976d7cc0d213f3 Mon Sep 17 00:00:00 2001 From: Hugo Parente Lima Date: Mon, 25 Apr 2011 15:01:35 -0300 Subject: Add support to template types as arguments on user added functions. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewer: Renato Araújo Marcelo Lira --- abstractmetabuilder.cpp | 31 +++++++++++++++++++++++++++++-- tests/testaddfunction.cpp | 17 +++++++++++++++++ tests/testaddfunction.h | 1 + 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/abstractmetabuilder.cpp b/abstractmetabuilder.cpp index 41c09a3..4b8f2e1 100644 --- a/abstractmetabuilder.cpp +++ b/abstractmetabuilder.cpp @@ -1897,10 +1897,27 @@ AbstractMetaType* AbstractMetaBuilder::translateType(double vr, const AddedFunct TypeDatabase* typeDb = TypeDatabase::instance(); TypeEntry* type; - if (typeInfo.name == "void") + QString typeName = typeInfo.name; + + if (typeName == "void") return 0; - type = typeDb->findType(typeInfo.name); + type = typeDb->findType(typeName); + + // test if the type is a template, like a container + bool isTemplate = false; + QString templateArg; + if (!type) { + QRegExp r("(.*)<(.*)>$"); + if (r.indexIn(typeInfo.name) != -1) { + templateArg = r.cap(2); + if (templateArg.contains(',')) + ReportHandler::warning("add-function tag doesn't support container types with more than one argument or template arguments."); + else + isTemplate = (type = typeDb->findContainerType(r.cap(1))); + } + } + if (!type) { type = new TypeEntry(typeInfo.name, TypeEntry::CustomType, vr); typeDb->addType(type); @@ -1911,6 +1928,16 @@ AbstractMetaType* AbstractMetaBuilder::translateType(double vr, const AddedFunct metaType->setIndirections(typeInfo.indirections); metaType->setReference(typeInfo.isReference); metaType->setConstant(typeInfo.isConstant); + if (isTemplate) { + type = typeDb->findType(templateArg); + if (type) { + AbstractMetaType* metaArgType = createMetaType(); + metaArgType->setTypeEntry(type); + metaType->addInstantiation(metaArgType); + metaType->setTypeUsagePattern(AbstractMetaType::ContainerPattern); + } + } + return metaType; } diff --git a/tests/testaddfunction.cpp b/tests/testaddfunction.cpp index 4e03c96..2d4ee91 100644 --- a/tests/testaddfunction.cpp +++ b/tests/testaddfunction.cpp @@ -407,6 +407,23 @@ void TestAddFunction::testAddFunctionOnTypedef() QVERIFY(method); } +void TestAddFunction::testAddFunctionWithTemplateArg() +{ + const char cppCode[] = "template class Foo { };"; + const char xmlCode[] = "\ + \ + \ + \ + \ + "; + + TestUtil t(cppCode, xmlCode); + QCOMPARE(t.builder()->globalFunctions().size(), 1); + AbstractMetaFunction* func = t.builder()->globalFunctions().first(); + AbstractMetaArgument* arg = func->arguments().first(); + QCOMPARE(arg->type()->instantiations().count(), 1); +} + QTEST_APPLESS_MAIN(TestAddFunction) #include "testaddfunction.moc" diff --git a/tests/testaddfunction.h b/tests/testaddfunction.h index 8fa4404..ce8c1fe 100644 --- a/tests/testaddfunction.h +++ b/tests/testaddfunction.h @@ -43,6 +43,7 @@ private slots: void testAddFunctionWithApiVersion(); void testModifyAddedFunction(); void testAddFunctionOnTypedef(); + void testAddFunctionWithTemplateArg(); }; #endif -- cgit v1.2.3