Merge branch 'ref_id_cmp_string_view' into 'master'

Support RefId comparison with std::string_view

See merge request OpenMW/openmw!2784
depth-refraction
psi29a 2 years ago
commit 340a2aa826

@ -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);

@ -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<ESM::GameSetting> settings;
EXPECT_THROW(EsmLoader::getGameSetting(settings, ESM::RefId::stringRefId("setting")), std::runtime_error);
EXPECT_THROW(EsmLoader::getGameSetting(settings, "setting"), std::runtime_error);
}
}

@ -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();

@ -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 <std::size_t size>
bool operator==(const char (&rhs)[size]) const
{
return *this == std::string_view(rhs);
}
private:
std::string mId;
bool operator==(std::string_view rhs) const;
};
}

@ -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<ESM::GameSetting>& records, const ESM::RefId& id)
ESM::Variant getGameSetting(const std::vector<ESM::GameSetting>& 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;
}
}

@ -3,6 +3,7 @@
#include <components/esm/defs.hpp>
#include <components/esm/refid.hpp>
#include <string_view>
#include <vector>
@ -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<ESM::GameSetting>& records, const ESM::RefId& id);
ESM::Variant getGameSetting(const std::vector<ESM::GameSetting>& records, std::string_view id);
}
#endif

@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_CONTENT_LESSBYID_H
#include <components/esm/refid.hpp>
#include <string_view>
namespace EsmLoader
@ -19,6 +20,12 @@ namespace EsmLoader
{
return lhs.mId < rhs;
}
template <class T>
bool operator()(const T& lhs, std::string_view rhs) const
{
return lhs.mId < rhs;
}
};
}

Loading…
Cancel
Save