diff options
author | Dan Goodliffe <daniel.goodliffe@pressassociation.com> | 2016-10-05 18:43:14 +0100 |
---|---|---|
committer | Dan Goodliffe <daniel.goodliffe@pressassociation.com> | 2016-10-05 18:43:14 +0100 |
commit | c40e03e7e3cb7ce66d58040baacd30fe93ba599d (patch) | |
tree | fc15349537e6eff6e3c7224c23de4fe0261cbd81 | |
parent | Implement class model part creation in a real function, not a machine built l... (diff) | |
download | slicer-c40e03e7e3cb7ce66d58040baacd30fe93ba599d.tar.bz2 slicer-c40e03e7e3cb7ce66d58040baacd30fe93ba599d.tar.xz slicer-c40e03e7e3cb7ce66d58040baacd30fe93ba599d.zip |
Use template functions instead of specializations for class name register and type identification
-rw-r--r-- | slicer/slicer/modelPartsTypes.h | 4 | ||||
-rw-r--r-- | slicer/slicer/modelPartsTypes.impl.h | 26 | ||||
-rw-r--r-- | slicer/tool/parser.cpp | 30 |
3 files changed, 39 insertions, 21 deletions
diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index 886dcb0..a1230e9 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -168,8 +168,12 @@ namespace Slicer { virtual IceUtil::Optional<std::string> GetTypeIdProperty() const override; static const std::string typeIdProperty; + static const std::string className; + static const IceUtil::Optional<std::string> typeName; static ModelPartPtr CreateModelPart(void *); + static void registerClass() __attribute__ ((constructor(210))); + static void unregisterClass() __attribute__ ((destructor(210))); }; template<typename T> diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 2ed8996..d8d86bc 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -304,6 +304,32 @@ namespace Slicer { return new ModelPartForClass<T>(*static_cast<T *>(p)); } + template<typename T> + void ModelPartForClass<T>::registerClass() + { + Slicer::classRefMap()->insert({ className, &ModelPartForClass<T>::CreateModelPart }); + if (typeName) { + Slicer::classNameMap()->insert({ className, *typeName }); + } + } + + template<typename T> + void ModelPartForClass<T>::unregisterClass() + { + Slicer::classRefMap()->erase(className); + if (typeName) { + Slicer::classNameMap()->left.erase(className); + } + } + + template<typename T> + TypeId + ModelPartForClass<T>::GetTypeId() const + { + auto id = this->Model->ice_id(); + return (id == className) ? TypeId() : ModelPart::ToExchangeTypeName(id); + } + // ModelPartForStruct template<typename T> ModelPartForStruct<T>::ModelPartForStruct(T & o) : diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 50f7408..a15fb34 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -195,32 +195,20 @@ namespace Slicer { defineRootName(typeToString(decl), name ? *name : c->name()); auto typeName = metaDataValue("slicer:typename:", c->getMetaData()); - fprintbf(cpp, "static void registerClass_%u() __attribute__ ((constructor(210)));\n", classNo); - fprintbf(cpp, "static void registerClass_%u()\n{\n", classNo); - fprintbf(cpp, "\tSlicer::classRefMap()->insert({ \"%s\", &ModelPartForClass< %s >::CreateModelPart });\n", - c->scoped(), + fprintbf(cpp, "template<> DLL_PUBLIC\n"); + fprintbf(cpp, "__attribute__ ((init_priority(209)))\nconst std::string ModelPartForClass< %s >::className(\"%s\");\n", + typeToString(decl), c->scoped()); + fprintbf(cpp, "template<> DLL_PUBLIC\n"); + fprintbf(cpp, "__attribute__ ((init_priority(209)))\nconst IceUtil::Optional<std::string> ModelPartForClass< %s >::typeName", typeToString(decl)); if (typeName) { - fprintbf(cpp, "\tSlicer::classNameMap()->insert({ \"%s\", \"%s\" });\n", - c->scoped(), + fprintbf(cpp, "(\"%s\")", *typeName); } - fprintbf(cpp, "}\n\n"); - fprintbf(cpp, "static void unregisterClass_%u() __attribute__ ((destructor(210)));\n", classNo); - fprintbf(cpp, "static void unregisterClass_%u()\n{\n", classNo); - fprintbf(cpp, "\tSlicer::classRefMap()->erase(\"%s\");\n", - c->scoped()); - if (typeName) { - fprintbf(cpp, "\tSlicer::classNameMap()->left.erase(\"%s\");\n", - c->scoped()); + else { + fprintbf(cpp, "(IceUtil::None)"); } - fprintbf(cpp, "}\n\n"); - - fprintbf(cpp, "template<> DLL_PUBLIC\nTypeId\nModelPartForClass< %s >::GetTypeId() const\n{\n", - typeToString(decl)); - fprintbf(cpp, "\tauto id = this->Model->ice_id();\n"); - fprintbf(cpp, "\treturn (id == \"%s\") ? TypeId() : ModelPart::ToExchangeTypeName(id);\n}\n\n", - c->scoped()); + fprintbf(cpp, ";\n\n"); fprintbf(cpp, "template<> DLL_PUBLIC\nconst Metadata ModelPartForComplex< %s >::metadata ", c->scoped()); |