diff --git a/apps/opencs/model/tools/reportmodel.cpp b/apps/opencs/model/tools/reportmodel.cpp index d8e99969a7..84a8c71f95 100644 --- a/apps/opencs/model/tools/reportmodel.cpp +++ b/apps/opencs/model/tools/reportmodel.cpp @@ -59,10 +59,19 @@ QVariant CSMTools::ReportModel::data(const QModelIndex& index, int role) const { CSMWorld::UniversalId id = mRows.at(index.row()).mId; - if (id.getArgumentType() == CSMWorld::UniversalId::ArgumentType_Id) - return QString::fromUtf8(id.getId().c_str()); - - return QString("-"); + switch (id.getArgumentType()) + { + case CSMWorld::UniversalId::ArgumentType_None: + return QString("-"); + case CSMWorld::UniversalId::ArgumentType_Index: + return QString::number(id.getIndex()); + case CSMWorld::UniversalId::ArgumentType_Id: + return QString::fromStdString(id.getId()); + case CSMWorld::UniversalId::ArgumentType_RefId: + return QString::fromStdString(id.getRefId().toString()); + } + + return QString("unsupported"); } case Column_Hint: diff --git a/apps/opencs/model/world/universalid.cpp b/apps/opencs/model/world/universalid.cpp index 8a6651b124..d7a820ad5d 100644 --- a/apps/opencs/model/world/universalid.cpp +++ b/apps/opencs/model/world/universalid.cpp @@ -201,6 +201,23 @@ namespace return sIdArg; } }; + + std::string toString(CSMWorld::UniversalId::ArgumentType value) + { + switch (value) + { + case CSMWorld::UniversalId::ArgumentType_None: + return "None"; + case CSMWorld::UniversalId::ArgumentType_Id: + return "Id"; + case CSMWorld::UniversalId::ArgumentType_Index: + return "Index"; + case CSMWorld::UniversalId::ArgumentType_RefId: + return "RefId"; + } + + return std::to_string(value); + } } CSMWorld::UniversalId::UniversalId(const std::string& universalId) @@ -354,6 +371,14 @@ int CSMWorld::UniversalId::getIndex() const throw std::logic_error("invalid access to index of non-index UniversalId"); } +ESM::RefId CSMWorld::UniversalId::getRefId() const +{ + if (const ESM::RefId* result = std::get_if(&mValue)) + return *result; + + throw std::logic_error("invalid access to RefId of " + ::toString(getArgumentType()) + " UniversalId"); +} + std::string CSMWorld::UniversalId::getTypeName() const { const std::span typeData = std::visit(GetTypeData{}, mValue); diff --git a/apps/opencs/model/world/universalid.hpp b/apps/opencs/model/world/universalid.hpp index 8ade978a5c..2d3385bcb4 100644 --- a/apps/opencs/model/world/universalid.hpp +++ b/apps/opencs/model/world/universalid.hpp @@ -170,6 +170,8 @@ namespace CSMWorld int getIndex() const; ///< Calling this function for a non-index type will throw an exception. + ESM::RefId getRefId() const; + std::string getTypeName() const; std::string toString() const; diff --git a/apps/opencs_tests/model/world/testuniversalid.cpp b/apps/opencs_tests/model/world/testuniversalid.cpp index 960903c8de..564d65a1ac 100644 --- a/apps/opencs_tests/model/world/testuniversalid.cpp +++ b/apps/opencs_tests/model/world/testuniversalid.cpp @@ -27,6 +27,11 @@ namespace CSMWorld EXPECT_THROW(UniversalId(UniversalId::Type_Activator, 42), std::logic_error); } + TEST(CSMWorldUniversalIdTest, shouldFailToConstructFromRefIdWithInvalidType) + { + EXPECT_THROW(UniversalId(UniversalId::Type_Search, ESM::RefId()), std::logic_error); + } + TEST(CSMWorldUniversalIdTest, shouldFailToConstructFromInvalidUniversalIdString) { EXPECT_THROW(UniversalId("invalid"), std::runtime_error); @@ -62,6 +67,18 @@ namespace CSMWorld EXPECT_EQ(id.getId(), "a"); } + TEST(CSMWorldUniversalIdTest, getRefIdShouldThrowExceptionForDefaultConstructed) + { + const UniversalId id; + EXPECT_THROW(id.getRefId(), std::logic_error); + } + + TEST(CSMWorldUniversalIdTest, getRefIdShouldReturnValueForConstructedFromRefId) + { + const UniversalId id(UniversalId::Type_Skill, ESM::IndexRefId(ESM::REC_SKIL, 42)); + EXPECT_EQ(id.getRefId(), ESM::IndexRefId(ESM::REC_SKIL, 42)); + } + struct Params { UniversalId mId;