From 7b909c8d247a5f0eea4bfac8a0bcef77b01b74a4 Mon Sep 17 00:00:00 2001 From: Evil Eye Date: Thu, 13 Jul 2023 20:30:16 +0200 Subject: [PATCH] Create getRefIds --- components/esm/stringrefid.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/components/esm/stringrefid.cpp b/components/esm/stringrefid.cpp index 2657734c7a..2f9a8fd6b3 100644 --- a/components/esm/stringrefid.cpp +++ b/components/esm/stringrefid.cpp @@ -21,17 +21,18 @@ namespace ESM const std::string emptyString; - Misc::NotNullPtr getOrInsertString(std::string_view id, bool insert) + Misc::ScopeGuarded& getRefIds() { static Misc::ScopeGuarded refIds; - const auto locked = refIds.lock(); + return refIds; + } + + Misc::NotNullPtr getOrInsertString(std::string_view id) + { + const auto locked = getRefIds().lock(); auto it = locked->find(id); if (it == locked->end()) - { - if (!insert) - return &emptyString; it = locked->emplace(id).first; - } return &*it; } @@ -54,7 +55,7 @@ namespace ESM } StringRefId::StringRefId(std::string_view value) - : mValue(getOrInsertString(value, true)) + : mValue(getOrInsertString(value)) { } @@ -136,11 +137,12 @@ namespace ESM std::optional StringRefId::deserializeExisting(std::string_view value) { - auto string = getOrInsertString(value, false); - if (string->empty()) + const auto locked = getRefIds().lock(); + auto it = locked->find(value); + if (it == locked->end()) return {}; StringRefId id; - id.mValue = string; + id.mValue = &*it; return id; } }