From 476de9026a2b50b911181a5c0aab81a653c94464 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 3 Sep 2023 20:09:44 +0100 Subject: Simplify className and typeName setup with string_views Replaces the pointer to string and initialisers function complication. --- slicer/slicer/modelPartsTypes.cpp | 17 ++++++++++------- slicer/slicer/modelPartsTypes.h | 15 +++++++-------- slicer/slicer/modelPartsTypes.impl.h | 18 +++--------------- slicer/tool/parser.cpp | 16 +++++----------- 4 files changed, 25 insertions(+), 41 deletions(-) diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index df281b2..dcfd338 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -28,11 +28,11 @@ namespace Ice { namespace Slicer { using ClassRefMap = std::map>; - using ClassNamePair = std::pair; + using ClassNamePair = std::pair; using ClassNameMap = boost::multi_index_container, + boost::multi_index::member, std::less<>>, boost::multi_index::ordered_unique< boost::multi_index::member, @@ -52,7 +52,7 @@ namespace Slicer { } } - const std::string & + std::string_view ModelPartForComplexBase::ToModelTypeName(const std::string & name) { const auto & right = names->get<1>(); @@ -196,7 +196,7 @@ namespace Slicer { void ModelPartForComplexBase::registerClass( - const std::string & className, const std::string * typeName, const ClassRef & cr) + const std::string_view className, const std::optional typeName, const ClassRef & cr) { refs->emplace(className, cr); if (typeName) { @@ -205,9 +205,12 @@ namespace Slicer { } void - ModelPartForComplexBase::unregisterClass(const std::string & className, const std::string * typeName) + ModelPartForComplexBase::unregisterClass( + const std::string_view className, const std::optional typeName) { - refs->erase(className); + if (const auto i = refs->find(className); i != refs->end()) { + refs->erase(i); + } if (typeName) { names->get<0>().erase(className); } @@ -223,7 +226,7 @@ namespace Slicer { } TypeId - ModelPartForComplexBase::getTypeId(const std::string & id, const std::string & className) + ModelPartForComplexBase::getTypeId(const std::string & id, const std::string_view className) { return (id == className) ? TypeId() : ToExchangeTypeName(id); } diff --git a/slicer/slicer/modelPartsTypes.h b/slicer/slicer/modelPartsTypes.h index b1e28c5..0403650 100644 --- a/slicer/slicer/modelPartsTypes.h +++ b/slicer/slicer/modelPartsTypes.h @@ -139,13 +139,14 @@ namespace Slicer { protected: void onSubclass(const std::string & name, void * m, const ModelPartHandler &); - static void registerClass(const std::string & className, const std::string * typeName, const ClassRef &); - static void unregisterClass(const std::string & className, const std::string * typeName); - static TypeId getTypeId(const std::string & id, const std::string & className); + static void registerClass( + const std::string_view className, const std::optional typeName, const ClassRef &); + static void unregisterClass(const std::string_view className, const std::optional typeName); + static TypeId getTypeId(const std::string & id, const std::string_view className); static std::string demangle(const char * mangled); static const std::string & ToExchangeTypeName(const std::string &); - static const std::string & ToModelTypeName(const std::string &); + static std::string_view ToModelTypeName(const std::string &); }; template class Hooks; @@ -192,14 +193,12 @@ namespace Slicer { [[nodiscard]] std::optional GetTypeIdProperty() const override; static const std::string typeIdProperty; - static const std::string * className; - static const std::string * typeName; + constinit static const std::string_view className; + constinit static const std::optional typeName; static void CreateModelPart(void *, const ModelPartHandler &); private: - static void initClassName(); - static void deleteClassName(); static void registerClass() __attribute__((constructor(210))); static void unregisterClass() __attribute__((destructor(210))); }; diff --git a/slicer/slicer/modelPartsTypes.impl.h b/slicer/slicer/modelPartsTypes.impl.h index 03ac8c8..718f654 100644 --- a/slicer/slicer/modelPartsTypes.impl.h +++ b/slicer/slicer/modelPartsTypes.impl.h @@ -500,29 +500,18 @@ namespace Slicer { return ::Slicer::ModelPart::CreateFor(*static_cast(p), h); } - template - void - ModelPartForClass::deleteClassName() - { - delete className; - delete typeName; - } - template void ModelPartForClass::registerClass() { - initClassName(); - ModelPartForComplexBase::registerClass(*className, typeName, &ModelPartForClass::CreateModelPart); + ModelPartForComplexBase::registerClass(className, typeName, &ModelPartForClass::CreateModelPart); } template void ModelPartForClass::unregisterClass() { - BOOST_ASSERT(className); - ModelPartForComplexBase::unregisterClass(*className, typeName); - deleteClassName(); + ModelPartForComplexBase::unregisterClass(className, typeName); } template @@ -530,7 +519,6 @@ namespace Slicer { ModelPartForClass::GetTypeId() const { BOOST_ASSERT(this->Model); - BOOST_ASSERT(className); return ModelPartForComplexBase::getTypeId( [this]() { if constexpr (std::is_base_of_v) { @@ -540,7 +528,7 @@ namespace Slicer { return ModelPartForComplexBase::demangle(typeid(*this->Model->get()).name()); } }(), - *className); + className); } // ModelPartForStruct diff --git a/slicer/tool/parser.cpp b/slicer/tool/parser.cpp index 55a06fe..e40b2b7 100644 --- a/slicer/tool/parser.cpp +++ b/slicer/tool/parser.cpp @@ -418,20 +418,14 @@ namespace Slicer { auto typeName = md.value("slicer:typename:"); fprintbf(cpp, "template<>\n"); - fprintbf(cpp, "const std::string * ModelPartForClass< %s >::className = nullptr;\n", decl->typeId()); + fprintbf(cpp, "const std::string_view ModelPartForClass< %s >::className{\"%s\"};\n", decl->typeId(), + c->scoped()); fprintbf(cpp, "template<>\n"); - fprintbf(cpp, "const std::string * ModelPartForClass< %s >::typeName = nullptr;\n", decl->typeId()); - fprintbf(cpp, - "template<>\nvoid ModelPartForClass< %s >::initClassName() {\n\tclassName = new " - "std::string(\"%s\");\n\t", - decl->typeId(), c->scoped()); + fprintbf(cpp, "const std::optional ModelPartForClass< %s >::typeName{", decl->typeId()); if (typeName) { - fprintbf(cpp, "typeName = new std::string(\"%s\");", *typeName); - } - else { - fprintbf(cpp, "typeName = nullptr;"); + fprintbf(cpp, "\"%s\"", *typeName); } - fprintbf(cpp, "\n}\n"); + fprintbf(cpp, "};\n"); defineGetMetadata(md, c, "ModelPartForComplex"); -- cgit v1.2.3