diff options
author | randomdan <randomdan@localhost> | 2014-09-18 20:44:35 +0000 |
---|---|---|
committer | randomdan <randomdan@localhost> | 2014-09-18 20:44:35 +0000 |
commit | 1364952ac17304a73f45b537f8fe9022c2027492 (patch) | |
tree | eeac7b18361e931e24bf32cc7bc50c1debb1b66c | |
parent | Make slice metadata for the slicer namespace available at runtime (diff) | |
download | slicer-1364952ac17304a73f45b537f8fe9022c2027492.tar.bz2 slicer-1364952ac17304a73f45b537f8fe9022c2027492.tar.xz slicer-1364952ac17304a73f45b537f8fe9022c2027492.zip |
Allow a class to specify a specific name when referenced in a documentslicer-0.8
-rw-r--r-- | slicer/slicer/modelParts.cpp | 27 | ||||
-rw-r--r-- | slicer/slicer/modelParts.h | 8 | ||||
-rw-r--r-- | slicer/slicer/parser.cpp | 12 | ||||
-rw-r--r-- | slicer/test/initial/inherit-mapped.json | 1 | ||||
-rw-r--r-- | slicer/test/run-slicer.cpp | 1 | ||||
-rw-r--r-- | slicer/test/types.ice | 7 |
6 files changed, 54 insertions, 2 deletions
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 <stdexcept> #include <boost/function.hpp> #include <boost/foreach.hpp> +#include <boost/bimap.hpp> #include <vector> namespace Slicer { @@ -86,6 +87,8 @@ namespace Slicer { typedef boost::function<ModelPartPtr(void *)> ClassRef; typedef std::map<std::string, ClassRef> ClassRefMap; ClassRefMap * & classRefMap(); + typedef boost::bimap<std::string, std::string> ClassNameMap; + ClassNameMap * & classNameMap(); typedef std::set<std::string> 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<typename T> @@ -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<TestModule::Optionals, Slicer::JsonFile>(root, tmpf, "optionals-areset2.json", checkOptionals_areset); verifyByFile<TestModule::InheritanceCont, Slicer::JsonFile>(root, tmpf, "inherit-c.json", checkInherits_types); verifyByFile<TestModule::InheritanceCont2, Slicer::JsonFile>(root, tmpf, "inherit-d.json"); + verifyByFile<TestModule::InheritanceContMapped, Slicer::JsonFile>(root, tmpf, "inherit-mapped.json"); verifyByHelper<TestModule::Optionals, Slicer::JsonValue, json::Value>(root, tmph, "optionals-areset2.json", readJson, writeJson, freeJson, checkOptionals_areset); verifyByHelper<TestModule::Optionals, Slicer::XmlDocument, xmlpp::Document *>(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; + }; }; |