From 16fc41e6bfe74ab047d258d5719f089e49225be9 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 16 Sep 2020 19:32:55 +0300 Subject: [PATCH] Always return unique_ptr for the IniReader/IniWriter interface --- src/openrct2/config/IniReader.cpp | 8 ++++---- src/openrct2/config/IniReader.hpp | 5 +++-- src/openrct2/config/IniWriter.cpp | 4 ++-- src/openrct2/config/IniWriter.hpp | 3 ++- test/tests/IniReaderTest.cpp | 15 +++++---------- test/tests/IniWriterTest.cpp | 30 ++++++++++-------------------- 6 files changed, 26 insertions(+), 39 deletions(-) diff --git a/src/openrct2/config/IniReader.cpp b/src/openrct2/config/IniReader.cpp index fd34c6f0c0..cfdab415c8 100644 --- a/src/openrct2/config/IniReader.cpp +++ b/src/openrct2/config/IniReader.cpp @@ -437,12 +437,12 @@ utf8* IIniReader::GetCString(const std::string& name, const utf8* defaultValue) return String::Duplicate(szValue.c_str()); } -IIniReader* CreateIniReader(OpenRCT2::IStream* stream) +std::unique_ptr CreateIniReader(OpenRCT2::IStream* stream) { - return new IniReader(stream); + return std::make_unique(stream); } -IIniReader* CreateDefaultIniReader() +std::unique_ptr CreateDefaultIniReader() { - return new DefaultIniReader(); + return std::make_unique(); } diff --git a/src/openrct2/config/IniReader.hpp b/src/openrct2/config/IniReader.hpp index 741c090525..23329091cf 100644 --- a/src/openrct2/config/IniReader.hpp +++ b/src/openrct2/config/IniReader.hpp @@ -11,6 +11,7 @@ #include "../common.h" +#include #include namespace OpenRCT2 @@ -47,5 +48,5 @@ struct IIniReader utf8* GetCString(const std::string& name, const utf8* defaultValue) const; }; -IIniReader* CreateIniReader(OpenRCT2::IStream* stream); -IIniReader* CreateDefaultIniReader(); +std::unique_ptr CreateIniReader(OpenRCT2::IStream* stream); +std::unique_ptr CreateDefaultIniReader(); diff --git a/src/openrct2/config/IniWriter.cpp b/src/openrct2/config/IniWriter.cpp index c649c2b947..d539c02160 100644 --- a/src/openrct2/config/IniWriter.cpp +++ b/src/openrct2/config/IniWriter.cpp @@ -103,7 +103,7 @@ void IIniWriter::WriteString(const std::string& name, const utf8* value) WriteString(name, String::ToStd(value)); } -IIniWriter* CreateIniWriter(OpenRCT2::IStream* stream) +std::unique_ptr CreateIniWriter(OpenRCT2::IStream* stream) { - return new IniWriter(stream); + return std::make_unique(stream); } diff --git a/src/openrct2/config/IniWriter.hpp b/src/openrct2/config/IniWriter.hpp index e87f111f51..ab5cd6f501 100644 --- a/src/openrct2/config/IniWriter.hpp +++ b/src/openrct2/config/IniWriter.hpp @@ -11,6 +11,7 @@ #include "../common.h" +#include #include namespace OpenRCT2 @@ -51,4 +52,4 @@ struct IIniWriter void WriteString(const std::string& name, const utf8* value); }; -IIniWriter* CreateIniWriter(OpenRCT2::IStream* stream); +std::unique_ptr CreateIniWriter(OpenRCT2::IStream* stream); diff --git a/test/tests/IniReaderTest.cpp b/test/tests/IniReaderTest.cpp index 0eebf9e50a..62eb5fd09d 100644 --- a/test/tests/IniReaderTest.cpp +++ b/test/tests/IniReaderTest.cpp @@ -32,7 +32,7 @@ TEST_F(IniReaderTest, create_empty) OpenRCT2::MemoryStream ms(0); ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanWrite(), true); - IIniReader* ir = CreateIniReader(&ms); + auto ir = CreateIniReader(&ms); ASSERT_NE(ir, nullptr); ASSERT_EQ(ir->GetBoolean("nobody", true), true); ASSERT_EQ(ir->GetCString("expects", nullptr), nullptr); @@ -41,7 +41,6 @@ TEST_F(IniReaderTest, create_empty) ASSERT_EQ(ir->GetInt32("universal_answer", 42), 42); ASSERT_EQ( ir->GetInt64("heat_death_of_the_universe", std::numeric_limits::max()), std::numeric_limits::max()); - delete ir; } TEST_F(IniReaderTest, read_prepared) @@ -49,7 +48,7 @@ TEST_F(IniReaderTest, read_prepared) OpenRCT2::MemoryStream ms(predefined.c_str(), predefined.size()); ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanWrite(), false); - IIniReader* ir = CreateIniReader(&ms); + auto ir = CreateIniReader(&ms); ASSERT_NE(ir, nullptr); ASSERT_EQ(ir->ReadSection("doesnt_exist"), false); ASSERT_EQ(ir->ReadSection("bool"), true); @@ -71,7 +70,6 @@ TEST_F(IniReaderTest, read_prepared) // go back a section ASSERT_EQ(ir->ReadSection("int"), true); ASSERT_EQ(ir->GetInt32("one", 42), 1); - delete ir; } TEST_F(IniReaderTest, read_duplicate) @@ -79,7 +77,7 @@ TEST_F(IniReaderTest, read_duplicate) OpenRCT2::MemoryStream ms(duplicate.c_str(), duplicate.size()); ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanWrite(), false); - IIniReader* ir = CreateIniReader(&ms); + auto ir = CreateIniReader(&ms); ASSERT_NE(ir, nullptr); // there should only be data from the last section ASSERT_EQ(ir->ReadSection("section"), true); @@ -97,7 +95,6 @@ TEST_F(IniReaderTest, read_duplicate) ASSERT_EQ(ir->ReadSection("section"), true); // test 4 times, there are only 3 sections ASSERT_EQ(ir->ReadSection("section"), true); - delete ir; } TEST_F(IniReaderTest, read_untrimmed) @@ -105,7 +102,7 @@ TEST_F(IniReaderTest, read_untrimmed) OpenRCT2::MemoryStream ms(untrimmed.c_str(), untrimmed.size()); ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanWrite(), false); - IIniReader* ir = CreateIniReader(&ms); + auto ir = CreateIniReader(&ms); ASSERT_NE(ir, nullptr); // there should only be data from the last section ASSERT_EQ(ir->ReadSection("section"), true); @@ -115,7 +112,6 @@ TEST_F(IniReaderTest, read_untrimmed) Memory::Free(str); ASSERT_EQ(ir->GetString("str", "yyy"), " xxx "); ASSERT_EQ(ir->GetString("nosuchthing", " yyy "), " yyy "); - delete ir; } TEST_F(IniReaderTest, read_case_insensitive) @@ -123,12 +119,11 @@ TEST_F(IniReaderTest, read_case_insensitive) OpenRCT2::MemoryStream ms(caseInsensitive.c_str(), caseInsensitive.size()); ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanWrite(), false); - IIniReader* ir = CreateIniReader(&ms); + auto ir = CreateIniReader(&ms); ASSERT_NE(ir, nullptr); ASSERT_EQ(ir->ReadSection("section"), true); ASSERT_EQ(ir->GetString("foo", "yyy"), "bar"); ASSERT_EQ(ir->ReadSection("SeCtIoN"), true); - delete ir; } const std::string IniReaderTest::predefined = "[bool]\n" diff --git a/test/tests/IniWriterTest.cpp b/test/tests/IniWriterTest.cpp index 87e5b1a08b..a50988f9f2 100644 --- a/test/tests/IniWriterTest.cpp +++ b/test/tests/IniWriterTest.cpp @@ -29,15 +29,14 @@ TEST_F(IniWriterTest, create_empty) OpenRCT2::MemoryStream ms(0); ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanWrite(), true); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); - delete iw; } TEST_F(IniWriterTest, create_one_section) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteSection("OpenRCT2"); uint8_t null_terminator = 0; @@ -49,13 +48,12 @@ TEST_F(IniWriterTest, create_one_section) const char* ini = reinterpret_cast(ms.ReadString()); ASSERT_STREQ(ini, "[OpenRCT2]" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_multiple_sections) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteSection("OpenRCT1"); iw->WriteSection("OpenRCT2"); @@ -73,13 +71,12 @@ TEST_F(IniWriterTest, create_multiple_sections) "[OpenRCT1]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT2]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT3]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT4]" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_loose_bool_entry) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteBoolean("boolval", true); uint8_t null_terminator = 0; @@ -91,13 +88,12 @@ TEST_F(IniWriterTest, create_loose_bool_entry) const char* ini = reinterpret_cast(ms.ReadString()); ASSERT_STREQ(ini, "boolval = true" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_loose_enum_entry) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteEnum("by_string", "stringval"); iw->WriteEnum("int32_t", 0, Enum_Currency); @@ -110,13 +106,12 @@ TEST_F(IniWriterTest, create_loose_enum_entry) const char* ini = reinterpret_cast(ms.ReadString()); ASSERT_STREQ(ini, "by_string = stringval" PLATFORM_NEWLINE "int32_t = 0" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_loose_float_entry) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteFloat("one", 1.); uint8_t null_terminator = 0; @@ -129,13 +124,12 @@ TEST_F(IniWriterTest, create_loose_float_entry) // This will be non-fatal due to float. EXPECT_STREQ(ini, "one = 1.000000" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_loose_int32_t_entry) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteInt32("one", 1); iw->WriteInt32("zero", 0); @@ -154,13 +148,12 @@ TEST_F(IniWriterTest, create_loose_int32_t_entry) "one = 1" PLATFORM_NEWLINE "zero = 0" PLATFORM_NEWLINE "minusone = -1" PLATFORM_NEWLINE "intmin = -2147483648" PLATFORM_NEWLINE "intmax = 2147483647" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_loose_string_entry) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteString("path", u8"C:'\\some/dir\\here/神鷹暢遊"); uint8_t null_terminator = 0; @@ -172,13 +165,12 @@ TEST_F(IniWriterTest, create_loose_string_entry) const char* ini = reinterpret_cast(ms.ReadString()); ASSERT_STREQ(ini, "path = \"C:'\\\\some/dir\\\\here/\xE7\xA5\x9E\xE9\xB7\xB9\xE6\x9A\xA2\xE9\x81\x8A\"" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_multiple_section_with_values) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteSection("bool"); iw->WriteBoolean("boolval", true); @@ -200,13 +192,12 @@ TEST_F(IniWriterTest, create_multiple_section_with_values) "one = 1" PLATFORM_NEWLINE "zero = 0" PLATFORM_NEWLINE PLATFORM_NEWLINE "[string]" PLATFORM_NEWLINE "path = " "\"C:'\\\\some/dir\\\\here/\xE7\xA5\x9E\xE9\xB7\xB9\xE6\x9A\xA2\xE9\x81\x8A\"" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; } TEST_F(IniWriterTest, create_duplicate_sections) { OpenRCT2::MemoryStream ms(1000); - IIniWriter* iw = CreateIniWriter(&ms); + auto iw = CreateIniWriter(&ms); ASSERT_NE(iw, nullptr); iw->WriteSection("section"); iw->WriteSection("section"); @@ -223,5 +214,4 @@ TEST_F(IniWriterTest, create_duplicate_sections) "[section]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[section]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[section]" PLATFORM_NEWLINE); Memory::Free(ini); - delete iw; }