From 2fcb94064db936b7b4c17ce6157b8d17e581a8f6 Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 28 Feb 2023 23:01:04 +0100 Subject: [PATCH] Support RefId comparison with std::string_view --- apps/navmeshtool/main.cpp | 3 +-- apps/openmw_test_suite/esmloader/esmdata.cpp | 4 ++-- components/esm/refid.cpp | 5 +++++ components/esm/refid.hpp | 12 ++++-------- components/esmloader/esmdata.cpp | 4 ++-- components/esmloader/esmdata.hpp | 3 ++- components/esmloader/lessbyid.hpp | 7 +++++++ 7 files changed, 23 insertions(+), 15 deletions(-) diff --git a/apps/navmeshtool/main.cpp b/apps/navmeshtool/main.cpp index b93e6cd7c8..edbf521020 100644 --- a/apps/navmeshtool/main.cpp +++ b/apps/navmeshtool/main.cpp @@ -232,8 +232,7 @@ namespace NavMeshTool DetourNavigator::RecastGlobalAllocator::init(); DetourNavigator::Settings navigatorSettings = DetourNavigator::makeSettingsFromSettingsManager(); navigatorSettings.mRecast.mSwimHeightScale - = EsmLoader::getGameSetting(esmData.mGameSettings, ESM::RefId::stringRefId("fSwimHeightScale")) - .getFloat(); + = EsmLoader::getGameSetting(esmData.mGameSettings, "fSwimHeightScale").getFloat(); WorldspaceData cellsData = gatherWorldspaceData( navigatorSettings, readers, vfs, bulletShapeManager, esmData, processInteriorCells, writeBinaryLog); diff --git a/apps/openmw_test_suite/esmloader/esmdata.cpp b/apps/openmw_test_suite/esmloader/esmdata.cpp index 3a571ecb30..ae1453c632 100644 --- a/apps/openmw_test_suite/esmloader/esmdata.cpp +++ b/apps/openmw_test_suite/esmloader/esmdata.cpp @@ -102,12 +102,12 @@ namespace setting.mId = ESM::RefId::stringRefId("setting"); setting.mValue = ESM::Variant(42); settings.push_back(setting); - EXPECT_EQ(EsmLoader::getGameSetting(settings, ESM::RefId::stringRefId("setting")), ESM::Variant(42)); + EXPECT_EQ(EsmLoader::getGameSetting(settings, "setting"), ESM::Variant(42)); } TEST(EsmLoaderGetGameSettingTest, shouldThrowExceptionWhenNotFound) { const std::vector settings; - EXPECT_THROW(EsmLoader::getGameSetting(settings, ESM::RefId::stringRefId("setting")), std::runtime_error); + EXPECT_THROW(EsmLoader::getGameSetting(settings, "setting"), std::runtime_error); } } diff --git a/components/esm/refid.cpp b/components/esm/refid.cpp index 854d403bd5..48a06ea6e5 100644 --- a/components/esm/refid.cpp +++ b/components/esm/refid.cpp @@ -16,6 +16,11 @@ namespace ESM return Misc::StringUtils::ciLess(mId, rhs.mId); } + bool RefId::operator<(std::string_view rhs) const + { + return Misc::StringUtils::ciLess(mId, rhs); + } + std::ostream& operator<<(std::ostream& os, const RefId& refId) { os << refId.getRefIdString(); diff --git a/components/esm/refid.hpp b/components/esm/refid.hpp index 16f42b6915..df5a71bfda 100644 --- a/components/esm/refid.hpp +++ b/components/esm/refid.hpp @@ -22,8 +22,12 @@ namespace ESM bool operator==(const RefId& rhs) const; + bool operator==(std::string_view rhs) const; + bool operator<(const RefId& rhs) const; + bool operator<(std::string_view rhs) const; + friend std::ostream& operator<<(std::ostream& os, const RefId& dt); // The 2 following functions are used to move back and forth between string and RefID. Used for hard coded @@ -33,16 +37,8 @@ namespace ESM static RefId formIdRefId(const ESM4::FormId id); const std::string& getRefIdString() const { return mId; } - template - bool operator==(const char (&rhs)[size]) const - { - return *this == std::string_view(rhs); - } - private: std::string mId; - - bool operator==(std::string_view rhs) const; }; } diff --git a/components/esmloader/esmdata.cpp b/components/esmloader/esmdata.cpp index c7dabfdf1c..31679ec756 100644 --- a/components/esmloader/esmdata.cpp +++ b/components/esmloader/esmdata.cpp @@ -71,11 +71,11 @@ namespace EsmLoader return withStatic(refId, type, content, [](const auto& v) { return std::string_view(v.mModel); }); } - ESM::Variant getGameSetting(const std::vector& records, const ESM::RefId& id) + ESM::Variant getGameSetting(const std::vector& records, std::string_view id) { auto it = std::lower_bound(records.begin(), records.end(), id, LessById{}); if (it == records.end() || it->mId != id) - throw std::runtime_error("Game settings \"" + id.getRefIdString() + "\" is not found"); + throw std::runtime_error("Game settings \"" + std::string(id) + "\" is not found"); return it->mValue; } } diff --git a/components/esmloader/esmdata.hpp b/components/esmloader/esmdata.hpp index a123210c11..75a9d7e18f 100644 --- a/components/esmloader/esmdata.hpp +++ b/components/esmloader/esmdata.hpp @@ -3,6 +3,7 @@ #include #include + #include #include @@ -47,7 +48,7 @@ namespace EsmLoader std::string_view getModel(const EsmData& content, const ESM::RefId& refId, ESM::RecNameInts type); - ESM::Variant getGameSetting(const std::vector& records, const ESM::RefId& id); + ESM::Variant getGameSetting(const std::vector& records, std::string_view id); } #endif diff --git a/components/esmloader/lessbyid.hpp b/components/esmloader/lessbyid.hpp index adc93c63de..58468ccb86 100644 --- a/components/esmloader/lessbyid.hpp +++ b/components/esmloader/lessbyid.hpp @@ -2,6 +2,7 @@ #define OPENMW_COMPONENTS_CONTENT_LESSBYID_H #include + #include namespace EsmLoader @@ -19,6 +20,12 @@ namespace EsmLoader { return lhs.mId < rhs; } + + template + bool operator()(const T& lhs, std::string_view rhs) const + { + return lhs.mId < rhs; + } }; }