From ed40fc2c7af761b2be588b908731fed36e656521 Mon Sep 17 00:00:00 2001 From: randomdan Date: Thu, 18 Sep 2014 20:44:35 +0000 Subject: Allow a class to specify a specific name when referenced in a document --- slicer/slicer/modelParts.cpp | 27 +++++++++++++++++++++++++++ slicer/slicer/modelParts.h | 8 +++++++- slicer/slicer/parser.cpp | 12 +++++++++++- slicer/test/initial/inherit-mapped.json | 1 + slicer/test/run-slicer.cpp | 1 + slicer/test/types.ice | 7 +++++++ 6 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 slicer/test/initial/inherit-mapped.json diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index f902b92..80d90cf 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -13,6 +13,33 @@ namespace Slicer { { } + ClassNameMap * & + classNameMap() + { + static ClassNameMap * refs = new ClassNameMap(); + return refs; + } + + const std::string & + ModelPart::ToModelTypeName(const std::string & name) + { + auto mapped = classNameMap()->right.find(name); + if (mapped != classNameMap()->right.end()) { + return mapped->second; + } + return name; + } + + const std::string & + ModelPart::ToExchangeTypeName(const std::string & name) + { + auto mapped = classNameMap()->left.find(name); + if (mapped != classNameMap()->left.end()) { + return mapped->second; + } + return name; + } + ClassRefMap * & classRefMap() { diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 2fc43fe..da118bd 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -10,6 +10,7 @@ #include #include #include +#include #include namespace Slicer { @@ -86,6 +87,8 @@ namespace Slicer { typedef boost::function ClassRef; typedef std::map ClassRefMap; ClassRefMap * & classRefMap(); + typedef boost::bimap ClassNameMap; + ClassNameMap * & classNameMap(); typedef std::set Metadata; enum ModelPartType { mpt_Null, @@ -111,6 +114,9 @@ namespace Slicer { virtual void GetValue(ValueTargetPtr); virtual bool HasValue() const = 0; virtual const Metadata & GetMetadata() const; + + static const std::string & ToExchangeTypeName(const std::string &); + static const std::string & ToModelTypeName(const std::string &); }; template @@ -340,7 +346,7 @@ namespace Slicer { virtual ModelPartPtr GetSubclassModelPart(const std::string & name) override { - auto ref = classRefMap()->find(name); + auto ref = classRefMap()->find(ModelPart::ToModelTypeName(name)); if (ref == classRefMap()->end()) { throw UnknownType(name); } diff --git a/slicer/slicer/parser.cpp b/slicer/slicer/parser.cpp index fac9ff7..998a0cb 100644 --- a/slicer/slicer/parser.cpp +++ b/slicer/slicer/parser.cpp @@ -134,23 +134,33 @@ namespace Slicer { typeToString(decl).c_str(), name ? name->c_str() : c->name().c_str()); + auto typeName = metaDataValue("slicer:typename:", c->getMetaData()); fprintf(cpp, "static void registerClass_%u() __attribute__ ((constructor(210)));\n", classNo); fprintf(cpp, "static void registerClass_%u()\n{\n", classNo); fprintf(cpp, "\tSlicer::classRefMap()->insert({ \"%s::%s\", [](void * p){ return new ModelPartForClass< %s >(*static_cast< %s *>(p)); } });\n", modulePath().c_str(), c->name().c_str(), typeToString(decl).c_str(), typeToString(decl).c_str()); + if (typeName) { + fprintf(cpp, "\tSlicer::classNameMap()->insert({ \"%s::%s\", \"%s\" });\n", + modulePath().c_str(), c->name().c_str(), + typeName->c_str()); + } fprintf(cpp, "}\n\n"); fprintf(cpp, "static void unregisterClass_%u() __attribute__ ((destructor(210)));\n", classNo); fprintf(cpp, "static void unregisterClass_%u()\n{\n", classNo); fprintf(cpp, "\tSlicer::classRefMap()->erase(\"%s::%s\");\n", modulePath().c_str(), c->name().c_str()); + if (typeName) { + fprintf(cpp, "\tSlicer::classNameMap()->left.erase(\"%s::%s\");\n", + modulePath().c_str(), c->name().c_str()); + } fprintf(cpp, "}\n\n"); fprintf(cpp, "template<>\nTypeId\nModelPartForClass< %s >::GetTypeId() const\n{\n", typeToString(decl).c_str()); fprintf(cpp, "\tauto id = ModelObject->ice_id();\n"); - fprintf(cpp, "\treturn (id == \"%s::%s\") ? TypeId() : id;\n}\n\n", + fprintf(cpp, "\treturn (id == \"%s::%s\") ? TypeId() : ModelPart::ToExchangeTypeName(id);\n}\n\n", modulePath().c_str(), c->name().c_str()); fprintf(cpp, "template<>\nMetadata ModelPartForComplex< %s::%s >::metadata ", diff --git a/slicer/test/initial/inherit-mapped.json b/slicer/test/initial/inherit-mapped.json new file mode 100644 index 0000000..9157035 --- /dev/null +++ b/slicer/test/initial/inherit-mapped.json @@ -0,0 +1 @@ +{"b":{"a":12.000000,"c":10.000000,"mytype":"onetwo"}} diff --git a/slicer/test/run-slicer.cpp b/slicer/test/run-slicer.cpp index 9cbf74a..8231866 100644 --- a/slicer/test/run-slicer.cpp +++ b/slicer/test/run-slicer.cpp @@ -279,6 +279,7 @@ main(int, char ** argv) verifyByFile(root, tmpf, "optionals-areset2.json", checkOptionals_areset); verifyByFile(root, tmpf, "inherit-c.json", checkInherits_types); verifyByFile(root, tmpf, "inherit-d.json"); + verifyByFile(root, tmpf, "inherit-mapped.json"); verifyByHelper(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); verifyByHelper(root, tmph, "optionals-areset.xml", readXml, writeXml, freeXml, checkOptionals_areset); diff --git a/slicer/test/types.ice b/slicer/test/types.ice index eb7a8c9..469886d 100644 --- a/slicer/test/types.ice +++ b/slicer/test/types.ice @@ -85,4 +85,11 @@ module TestModule { class InheritanceCont2 { Base2 b; }; + ["slicer:typename:onetwo"] + class D13 extends Base2 { + int c; + }; + class InheritanceContMapped { + Base2 b; + }; }; -- cgit v1.2.3