diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-08-06 17:04:46 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2023-08-06 17:04:46 +0100 |
commit | 1adc252d8b7cdff8a6707f40ba6a68d0afee11cd (patch) | |
tree | e50029c24c4609af6db81660574a717ef7bf4860 | |
parent | Make [de]serializers on stack (diff) | |
download | slicer-1adc252d8b7cdff8a6707f40ba6a68d0afee11cd.tar.bz2 slicer-1adc252d8b7cdff8a6707f40ba6a68d0afee11cd.tar.xz slicer-1adc252d8b7cdff8a6707f40ba6a68d0afee11cd.zip |
Modernize set up of class name/ref maps
-rw-r--r-- | slicer/slicer/modelPartsTypes.cpp | 66 |
1 files changed, 23 insertions, 43 deletions
diff --git a/slicer/slicer/modelPartsTypes.cpp b/slicer/slicer/modelPartsTypes.cpp index cd22c05..bd130bf 100644 --- a/slicer/slicer/modelPartsTypes.cpp +++ b/slicer/slicer/modelPartsTypes.cpp @@ -38,45 +38,26 @@ namespace Slicer { boost::multi_index::member<ClassNamePair, const std::string, &ClassNamePair::second>, std::less<>>>>; - static void createClassMaps() __attribute__((constructor(208))); - static void deleteClassMaps() __attribute__((destructor(208))); - static ClassNameMap * names; - static ClassRefMap * refs; + namespace { + constinit std::unique_ptr<ClassNameMap> names; + constinit std::unique_ptr<ClassRefMap> refs; - void - createClassMaps() - { - names = new ClassNameMap(); - refs = new ClassRefMap(); - } + void createClassMaps() __attribute__((constructor(208))); - static void - deleteClassMaps() - { - delete names; - delete refs; - names = nullptr; - refs = nullptr; - } - - ClassNameMap * - classNameMap() - { - return names; - } - - ClassRefMap * - classRefMap() - { - return refs; + void + createClassMaps() + { + names = std::make_unique<ClassNameMap>(); + refs = std::make_unique<ClassRefMap>(); + } } const std::string & ModelPartForComplexBase::ToModelTypeName(const std::string & name) { - auto & right = classNameMap()->get<1>(); - auto mapped = right.find(name); - if (mapped != right.end()) { + const auto & right = names->get<1>(); + + if (const auto mapped = right.find(name); mapped != right.end()) { return mapped->first; } return name; @@ -85,9 +66,9 @@ namespace Slicer { const std::string & ModelPartForComplexBase::ToExchangeTypeName(const std::string & name) { - auto & left = classNameMap()->get<0>(); - auto mapped = left.find(name); - if (mapped != left.end()) { + const auto & left = names->get<0>(); + + if (const auto mapped = left.find(name); mapped != left.end()) { return mapped->second; } return name; @@ -249,29 +230,28 @@ namespace Slicer { ModelPartForComplexBase::registerClass( const std::string & className, const std::string * typeName, const ClassRef & cr) { - Slicer::classRefMap()->insert({className, cr}); + refs->emplace(className, cr); if (typeName) { - Slicer::classNameMap()->insert({className, *typeName}); + names->emplace(className, *typeName); } } void ModelPartForComplexBase::unregisterClass(const std::string & className, const std::string * typeName) { - Slicer::classRefMap()->erase(className); + refs->erase(className); if (typeName) { - classNameMap()->get<0>().erase(className); + names->get<0>().erase(className); } } ModelPartPtr ModelPartForComplexBase::getSubclassModelPart(const std::string & name, void * m) { - auto ref = classRefMap()->find(ToModelTypeName(name)); - if (ref == classRefMap()->end()) { - throw UnknownType(name); + if (const auto ref = refs->find(ToModelTypeName(name)); ref != refs->end()) { + return ref->second(m); } - return ref->second(m); + throw UnknownType(name); } TypeId |