From 062ab24139032806350537a42005d14e46944838 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Wed, 10 Jun 2020 21:51:38 +0100 Subject: [PATCH] Make File::GetName() return a Filename --- include/loot/metadata/file.h | 9 +++--- src/api/metadata/file.cpp | 10 +++---- src/api/metadata/yaml/file.h | 13 +++++---- src/api/sorting/plugin_graph.cpp | 28 +++++++++--------- src/tests/api/internals/metadata/file_test.h | 30 ++++++++++---------- src/tests/printers.h | 2 +- 6 files changed, 47 insertions(+), 45 deletions(-) diff --git a/include/loot/metadata/file.h b/include/loot/metadata/file.h index 73a25205..2557f53c 100644 --- a/include/loot/metadata/file.h +++ b/include/loot/metadata/file.h @@ -28,6 +28,7 @@ #include "loot/api_decorator.h" #include "loot/metadata/conditional_metadata.h" +#include "loot/metadata/filename.h" namespace loot { /** @@ -73,7 +74,7 @@ public: * Get the filename of the file. * @return The file's filename. */ - LOOT_API std::string GetName() const; + LOOT_API Filename GetName() const; /** * Get the display name of the file. @@ -82,7 +83,7 @@ public: LOOT_API std::string GetDisplayName() const; private: - std::string name_; + Filename name_; std::string display_; }; @@ -94,7 +95,7 @@ LOOT_API bool operator!=(const File& lhs, const File& rhs); /** * Check if the first File object is greater than the second File object. - * @returns True if the second File object is less than the first File object, + * @returns True if the second File object is less than the first File object, * false otherwise. */ LOOT_API bool operator>(const File& lhs, const File& rhs); @@ -102,7 +103,7 @@ LOOT_API bool operator>(const File& lhs, const File& rhs); /** * Check if the first File object is less than or equal to the second File * object. - * @returns True if the first File object is not greater than the second File + * @returns True if the first File object is not greater than the second File * object, false otherwise. */ LOOT_API bool operator<=(const File& lhs, const File& rhs); diff --git a/src/api/metadata/file.cpp b/src/api/metadata/file.cpp index 0d535505..5976b566 100644 --- a/src/api/metadata/file.cpp +++ b/src/api/metadata/file.cpp @@ -33,7 +33,7 @@ File::File() {} File::File(const std::string& name, const std::string& display, const std::string& condition) : - name_(name), + name_(Filename(name)), display_(display), ConditionalMetadata(condition) {} @@ -54,19 +54,19 @@ bool File::operator<(const File& rhs) const { return false; } - return CompareFilenames(name_, rhs.name_) < 0; + return name_ < rhs.name_; } bool File::operator==(const File& rhs) const { return display_ == rhs.display_ && GetCondition() == rhs.GetCondition() && - CompareFilenames(name_, rhs.name_) == 0; + name_ == rhs.name_; } -std::string File::GetName() const { return name_; } +Filename File::GetName() const { return name_; } std::string File::GetDisplayName() const { if (display_.empty()) - return name_; + return std::string(name_); else return display_; } diff --git a/src/api/metadata/yaml/file.h b/src/api/metadata/yaml/file.h index ec2bffdf..4ba02d84 100644 --- a/src/api/metadata/yaml/file.h +++ b/src/api/metadata/yaml/file.h @@ -37,12 +37,12 @@ template<> struct convert { static Node encode(const loot::File& rhs) { Node node; - node["name"] = rhs.GetName(); + node["name"] = std::string(rhs.GetName()); if (rhs.IsConditional()) node["condition"] = rhs.GetCondition(); - if (rhs.GetDisplayName() != rhs.GetName()) + if (rhs.GetDisplayName() != std::string(rhs.GetName())) node["display"] = rhs.GetDisplayName(); return node; @@ -84,17 +84,18 @@ struct convert { inline Emitter& operator<<(Emitter& out, const loot::File& rhs) { if (!rhs.IsConditional() && - (rhs.GetDisplayName().empty() || rhs.GetDisplayName() == rhs.GetName())) - out << YAML::SingleQuoted << rhs.GetName(); + (rhs.GetDisplayName().empty() || + rhs.GetDisplayName() == std::string(rhs.GetName()))) + out << YAML::SingleQuoted << std::string(rhs.GetName()); else { out << BeginMap << Key << "name" << Value << YAML::SingleQuoted - << rhs.GetName(); + << std::string(rhs.GetName()); if (rhs.IsConditional()) out << Key << "condition" << Value << YAML::SingleQuoted << rhs.GetCondition(); - if (rhs.GetDisplayName() != rhs.GetName()) + if (rhs.GetDisplayName() != std::string(rhs.GetName())) out << Key << "display" << Value << YAML::SingleQuoted << rhs.GetDisplayName(); diff --git a/src/api/sorting/plugin_graph.cpp b/src/api/sorting/plugin_graph.cpp index 1fc7de35..8567ecff 100644 --- a/src/api/sorting/plugin_graph.cpp +++ b/src/api/sorting/plugin_graph.cpp @@ -190,18 +190,18 @@ void PluginGraph::AddPluginVertices(Game& game, // implement it generally. auto loadedPlugins = game.GetCache()->GetPlugins(); std::sort(loadedPlugins.begin(), - loadedPlugins.end(), - [](const auto& lhs, const auto& rhs) { - if (!lhs) { - return false; - } + loadedPlugins.end(), + [](const auto& lhs, const auto& rhs) { + if (!lhs) { + return false; + } - if (!rhs) { - return true; - } + if (!rhs) { + return true; + } - return *lhs < *rhs; - }); + return *lhs < *rhs; + }); for (const auto& plugin : loadedPlugins) { auto masterlistMetadata = @@ -472,26 +472,26 @@ void PluginGraph::AddSpecificEdges() { } for (const auto& file : graph_[*vit].GetMasterlistRequirements()) { - auto parentVertex = GetVertexByName(file.GetName()); + auto parentVertex = GetVertexByName(std::string(file.GetName())); if (parentVertex.has_value()) { AddEdge(parentVertex.value(), *vit, EdgeType::masterlistRequirement); } } for (const auto& file : graph_[*vit].GetUserRequirements()) { - auto parentVertex = GetVertexByName(file.GetName()); + auto parentVertex = GetVertexByName(std::string(file.GetName())); if (parentVertex.has_value()) { AddEdge(parentVertex.value(), *vit, EdgeType::userRequirement); } } for (const auto& file : graph_[*vit].GetMasterlistLoadAfterFiles()) { - auto parentVertex = GetVertexByName(file.GetName()); + auto parentVertex = GetVertexByName(std::string(file.GetName())); if (parentVertex.has_value()) { AddEdge(parentVertex.value(), *vit, EdgeType::masterlistLoadAfter); } } for (const auto& file : graph_[*vit].GetUserLoadAfterFiles()) { - auto parentVertex = GetVertexByName(file.GetName()); + auto parentVertex = GetVertexByName(std::string(file.GetName())); if (parentVertex.has_value()) { AddEdge(parentVertex.value(), *vit, EdgeType::userLoadAfter); } diff --git a/src/tests/api/internals/metadata/file_test.h b/src/tests/api/internals/metadata/file_test.h index b5f7b7aa..e4070b8c 100644 --- a/src/tests/api/internals/metadata/file_test.h +++ b/src/tests/api/internals/metadata/file_test.h @@ -36,7 +36,7 @@ namespace test { TEST(File, defaultConstructorShouldInitialiseEmptyStrings) { File file; - EXPECT_EQ("", file.GetName()); + EXPECT_EQ("", std::string(file.GetName())); EXPECT_EQ("", file.GetDisplayName()); EXPECT_EQ("", file.GetCondition()); } @@ -44,7 +44,7 @@ TEST(File, defaultConstructorShouldInitialiseEmptyStrings) { TEST(File, stringsConstructorShouldStoreGivenStrings) { File file("name", "display", "condition"); - EXPECT_EQ("name", file.GetName()); + EXPECT_EQ("name", std::string(file.GetName())); EXPECT_EQ("display", file.GetDisplayName()); EXPECT_EQ("condition", file.GetCondition()); } @@ -279,9 +279,9 @@ TEST(File, emittingAsYamlShouldSingleQuoteValues) { File file("name1", "display1", "condition1"); YAML::Emitter emitter; emitter << file; - std::string expected = "name: '" + file.GetName() + "'\ncondition: '" + - file.GetCondition() + "'\ndisplay: '" + - file.GetDisplayName() + "'"; + std::string expected = "name: '" + std::string(file.GetName()) + + "'\ncondition: '" + file.GetCondition() + + "'\ndisplay: '" + file.GetDisplayName() + "'"; EXPECT_EQ(expected, emitter.c_str()); } @@ -291,7 +291,7 @@ TEST(File, emittingAsYamlShouldOutputAsAScalarIfOnlyTheNameStringIsNotEmpty) { YAML::Emitter emitter; emitter << file; - EXPECT_EQ("'" + file.GetName() + "'", emitter.c_str()); + EXPECT_EQ("'" + std::string(file.GetName()) + "'", emitter.c_str()); } TEST(File, emittingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameField) { @@ -299,15 +299,15 @@ TEST(File, emittingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameField) { YAML::Emitter emitter; emitter << file; - EXPECT_EQ("'" + file.GetName() + "'", emitter.c_str()); + EXPECT_EQ("'" + std::string(file.GetName()) + "'", emitter.c_str()); } TEST(File, emittingAsYamlShouldOmitAnEmptyConditionString) { File file("name1", "display1"); YAML::Emitter emitter; emitter << file; - std::string expected = "name: '" + file.GetName() + "'\ndisplay: '" + - file.GetDisplayName() + "'"; + std::string expected = "name: '" + std::string(file.GetName()) + + "'\ndisplay: '" + file.GetDisplayName() + "'"; EXPECT_EQ(expected, emitter.c_str()); } @@ -317,7 +317,7 @@ TEST(File, encodingAsYamlShouldStoreDataCorrectly) { YAML::Node node; node = file; - EXPECT_EQ(file.GetName(), node["name"].as()); + EXPECT_EQ(std::string(file.GetName()), node["name"].as()); EXPECT_EQ(file.GetDisplayName(), node["display"].as()); EXPECT_EQ(file.GetCondition(), node["condition"].as()); } @@ -327,7 +327,7 @@ TEST(File, encodingAsYamlShouldOmitEmptyFields) { YAML::Node node; node = file; - EXPECT_EQ(file.GetName(), node["name"].as()); + EXPECT_EQ(std::string(file.GetName()), node["name"].as()); EXPECT_FALSE(node["display"]); EXPECT_FALSE(node["condition"]); } @@ -337,7 +337,7 @@ TEST(File, encodingAsYamlShouldOmitDisplayFieldIfItMatchesTheNameField) { YAML::Node node; node = file; - EXPECT_EQ(file.GetName(), node["name"].as()); + EXPECT_EQ(std::string(file.GetName()), node["name"].as()); EXPECT_FALSE(node["display"]); EXPECT_FALSE(node["condition"]); } @@ -347,7 +347,7 @@ TEST(File, decodingFromYamlShouldSetDataCorrectly) { "{name: name1, display: display1, condition: 'file(\"Foo.esp\")'}"); File file = node.as(); - EXPECT_EQ(node["name"].as(), file.GetName()); + EXPECT_EQ(node["name"].as(), std::string(file.GetName())); EXPECT_EQ(node["display"].as(), file.GetDisplayName()); EXPECT_EQ(node["condition"].as(), file.GetCondition()); } @@ -357,7 +357,7 @@ TEST(File, YAML::Node node = YAML::Load("{name: name1, display: display1}"); File file = node.as(); - EXPECT_EQ(node["name"].as(), file.GetName()); + EXPECT_EQ(node["name"].as(), std::string(file.GetName())); EXPECT_EQ(node["display"].as(), file.GetDisplayName()); EXPECT_TRUE(file.GetCondition().empty()); } @@ -368,7 +368,7 @@ TEST( YAML::Node node = YAML::Load("name1"); File file = node.as(); - EXPECT_EQ(node.as(), file.GetName()); + EXPECT_EQ(node.as(), std::string(file.GetName())); EXPECT_EQ(node.as(), file.GetDisplayName()); EXPECT_TRUE(file.GetCondition().empty()); } diff --git a/src/tests/printers.h b/src/tests/printers.h index 7a4770df..1ba2a11c 100644 --- a/src/tests/printers.h +++ b/src/tests/printers.h @@ -41,7 +41,7 @@ along with LOOT. If not, see namespace loot { namespace test { void PrintTo(const File& value, ::std::ostream* os) { - *os << "File(\"" << value.GetName() << "\", " + *os << "File(\"" << std::string(value.GetName()) << "\", " << "\"" << value.GetDisplayName() << "\", " << "\"" << value.GetCondition() << "\"" << ")";