1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2025-12-23 07:43:01 +01:00

Always return unique_ptr for the IniReader/IniWriter interface

This commit is contained in:
Matt
2020-09-16 19:32:55 +03:00
parent d0d5cc5179
commit 16fc41e6bf
6 changed files with 26 additions and 39 deletions

View File

@@ -437,12 +437,12 @@ utf8* IIniReader::GetCString(const std::string& name, const utf8* defaultValue)
return String::Duplicate(szValue.c_str()); return String::Duplicate(szValue.c_str());
} }
IIniReader* CreateIniReader(OpenRCT2::IStream* stream) std::unique_ptr<IIniReader> CreateIniReader(OpenRCT2::IStream* stream)
{ {
return new IniReader(stream); return std::make_unique<IniReader>(stream);
} }
IIniReader* CreateDefaultIniReader() std::unique_ptr<IIniReader> CreateDefaultIniReader()
{ {
return new DefaultIniReader(); return std::make_unique<DefaultIniReader>();
} }

View File

@@ -11,6 +11,7 @@
#include "../common.h" #include "../common.h"
#include <memory>
#include <string> #include <string>
namespace OpenRCT2 namespace OpenRCT2
@@ -47,5 +48,5 @@ struct IIniReader
utf8* GetCString(const std::string& name, const utf8* defaultValue) const; utf8* GetCString(const std::string& name, const utf8* defaultValue) const;
}; };
IIniReader* CreateIniReader(OpenRCT2::IStream* stream); std::unique_ptr<IIniReader> CreateIniReader(OpenRCT2::IStream* stream);
IIniReader* CreateDefaultIniReader(); std::unique_ptr<IIniReader> CreateDefaultIniReader();

View File

@@ -103,7 +103,7 @@ void IIniWriter::WriteString(const std::string& name, const utf8* value)
WriteString(name, String::ToStd(value)); WriteString(name, String::ToStd(value));
} }
IIniWriter* CreateIniWriter(OpenRCT2::IStream* stream) std::unique_ptr<IIniWriter> CreateIniWriter(OpenRCT2::IStream* stream)
{ {
return new IniWriter(stream); return std::make_unique<IniWriter>(stream);
} }

View File

@@ -11,6 +11,7 @@
#include "../common.h" #include "../common.h"
#include <memory>
#include <string> #include <string>
namespace OpenRCT2 namespace OpenRCT2
@@ -51,4 +52,4 @@ struct IIniWriter
void WriteString(const std::string& name, const utf8* value); void WriteString(const std::string& name, const utf8* value);
}; };
IIniWriter* CreateIniWriter(OpenRCT2::IStream* stream); std::unique_ptr<IIniWriter> CreateIniWriter(OpenRCT2::IStream* stream);

View File

@@ -32,7 +32,7 @@ TEST_F(IniReaderTest, create_empty)
OpenRCT2::MemoryStream ms(0); OpenRCT2::MemoryStream ms(0);
ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), true); ASSERT_EQ(ms.CanWrite(), true);
IIniReader* ir = CreateIniReader(&ms); auto ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr); ASSERT_NE(ir, nullptr);
ASSERT_EQ(ir->GetBoolean("nobody", true), true); ASSERT_EQ(ir->GetBoolean("nobody", true), true);
ASSERT_EQ(ir->GetCString("expects", nullptr), nullptr); 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->GetInt32("universal_answer", 42), 42);
ASSERT_EQ( ASSERT_EQ(
ir->GetInt64("heat_death_of_the_universe", std::numeric_limits<int64_t>::max()), std::numeric_limits<int64_t>::max()); ir->GetInt64("heat_death_of_the_universe", std::numeric_limits<int64_t>::max()), std::numeric_limits<int64_t>::max());
delete ir;
} }
TEST_F(IniReaderTest, read_prepared) TEST_F(IniReaderTest, read_prepared)
@@ -49,7 +48,7 @@ TEST_F(IniReaderTest, read_prepared)
OpenRCT2::MemoryStream ms(predefined.c_str(), predefined.size()); OpenRCT2::MemoryStream ms(predefined.c_str(), predefined.size());
ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), false); ASSERT_EQ(ms.CanWrite(), false);
IIniReader* ir = CreateIniReader(&ms); auto ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr); ASSERT_NE(ir, nullptr);
ASSERT_EQ(ir->ReadSection("doesnt_exist"), false); ASSERT_EQ(ir->ReadSection("doesnt_exist"), false);
ASSERT_EQ(ir->ReadSection("bool"), true); ASSERT_EQ(ir->ReadSection("bool"), true);
@@ -71,7 +70,6 @@ TEST_F(IniReaderTest, read_prepared)
// go back a section // go back a section
ASSERT_EQ(ir->ReadSection("int"), true); ASSERT_EQ(ir->ReadSection("int"), true);
ASSERT_EQ(ir->GetInt32("one", 42), 1); ASSERT_EQ(ir->GetInt32("one", 42), 1);
delete ir;
} }
TEST_F(IniReaderTest, read_duplicate) TEST_F(IniReaderTest, read_duplicate)
@@ -79,7 +77,7 @@ TEST_F(IniReaderTest, read_duplicate)
OpenRCT2::MemoryStream ms(duplicate.c_str(), duplicate.size()); OpenRCT2::MemoryStream ms(duplicate.c_str(), duplicate.size());
ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), false); ASSERT_EQ(ms.CanWrite(), false);
IIniReader* ir = CreateIniReader(&ms); auto ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr); ASSERT_NE(ir, nullptr);
// there should only be data from the last section // there should only be data from the last section
ASSERT_EQ(ir->ReadSection("section"), true); ASSERT_EQ(ir->ReadSection("section"), true);
@@ -97,7 +95,6 @@ TEST_F(IniReaderTest, read_duplicate)
ASSERT_EQ(ir->ReadSection("section"), true); ASSERT_EQ(ir->ReadSection("section"), true);
// test 4 times, there are only 3 sections // test 4 times, there are only 3 sections
ASSERT_EQ(ir->ReadSection("section"), true); ASSERT_EQ(ir->ReadSection("section"), true);
delete ir;
} }
TEST_F(IniReaderTest, read_untrimmed) TEST_F(IniReaderTest, read_untrimmed)
@@ -105,7 +102,7 @@ TEST_F(IniReaderTest, read_untrimmed)
OpenRCT2::MemoryStream ms(untrimmed.c_str(), untrimmed.size()); OpenRCT2::MemoryStream ms(untrimmed.c_str(), untrimmed.size());
ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), false); ASSERT_EQ(ms.CanWrite(), false);
IIniReader* ir = CreateIniReader(&ms); auto ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr); ASSERT_NE(ir, nullptr);
// there should only be data from the last section // there should only be data from the last section
ASSERT_EQ(ir->ReadSection("section"), true); ASSERT_EQ(ir->ReadSection("section"), true);
@@ -115,7 +112,6 @@ TEST_F(IniReaderTest, read_untrimmed)
Memory::Free(str); Memory::Free(str);
ASSERT_EQ(ir->GetString("str", "yyy"), " xxx "); ASSERT_EQ(ir->GetString("str", "yyy"), " xxx ");
ASSERT_EQ(ir->GetString("nosuchthing", " yyy "), " yyy "); ASSERT_EQ(ir->GetString("nosuchthing", " yyy "), " yyy ");
delete ir;
} }
TEST_F(IniReaderTest, read_case_insensitive) TEST_F(IniReaderTest, read_case_insensitive)
@@ -123,12 +119,11 @@ TEST_F(IniReaderTest, read_case_insensitive)
OpenRCT2::MemoryStream ms(caseInsensitive.c_str(), caseInsensitive.size()); OpenRCT2::MemoryStream ms(caseInsensitive.c_str(), caseInsensitive.size());
ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), false); ASSERT_EQ(ms.CanWrite(), false);
IIniReader* ir = CreateIniReader(&ms); auto ir = CreateIniReader(&ms);
ASSERT_NE(ir, nullptr); ASSERT_NE(ir, nullptr);
ASSERT_EQ(ir->ReadSection("section"), true); ASSERT_EQ(ir->ReadSection("section"), true);
ASSERT_EQ(ir->GetString("foo", "yyy"), "bar"); ASSERT_EQ(ir->GetString("foo", "yyy"), "bar");
ASSERT_EQ(ir->ReadSection("SeCtIoN"), true); ASSERT_EQ(ir->ReadSection("SeCtIoN"), true);
delete ir;
} }
const std::string IniReaderTest::predefined = "[bool]\n" const std::string IniReaderTest::predefined = "[bool]\n"

View File

@@ -29,15 +29,14 @@ TEST_F(IniWriterTest, create_empty)
OpenRCT2::MemoryStream ms(0); OpenRCT2::MemoryStream ms(0);
ASSERT_EQ(ms.CanRead(), true); ASSERT_EQ(ms.CanRead(), true);
ASSERT_EQ(ms.CanWrite(), true); ASSERT_EQ(ms.CanWrite(), true);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
delete iw;
} }
TEST_F(IniWriterTest, create_one_section) TEST_F(IniWriterTest, create_one_section)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteSection("OpenRCT2"); iw->WriteSection("OpenRCT2");
uint8_t null_terminator = 0; uint8_t null_terminator = 0;
@@ -49,13 +48,12 @@ TEST_F(IniWriterTest, create_one_section)
const char* ini = reinterpret_cast<const char*>(ms.ReadString()); const char* ini = reinterpret_cast<const char*>(ms.ReadString());
ASSERT_STREQ(ini, "[OpenRCT2]" PLATFORM_NEWLINE); ASSERT_STREQ(ini, "[OpenRCT2]" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_multiple_sections) TEST_F(IniWriterTest, create_multiple_sections)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteSection("OpenRCT1"); iw->WriteSection("OpenRCT1");
iw->WriteSection("OpenRCT2"); iw->WriteSection("OpenRCT2");
@@ -73,13 +71,12 @@ TEST_F(IniWriterTest, create_multiple_sections)
"[OpenRCT1]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT2]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT1]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT2]" PLATFORM_NEWLINE PLATFORM_NEWLINE
"[OpenRCT3]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT4]" PLATFORM_NEWLINE); "[OpenRCT3]" PLATFORM_NEWLINE PLATFORM_NEWLINE "[OpenRCT4]" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_loose_bool_entry) TEST_F(IniWriterTest, create_loose_bool_entry)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteBoolean("boolval", true); iw->WriteBoolean("boolval", true);
uint8_t null_terminator = 0; uint8_t null_terminator = 0;
@@ -91,13 +88,12 @@ TEST_F(IniWriterTest, create_loose_bool_entry)
const char* ini = reinterpret_cast<const char*>(ms.ReadString()); const char* ini = reinterpret_cast<const char*>(ms.ReadString());
ASSERT_STREQ(ini, "boolval = true" PLATFORM_NEWLINE); ASSERT_STREQ(ini, "boolval = true" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_loose_enum_entry) TEST_F(IniWriterTest, create_loose_enum_entry)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteEnum("by_string", "stringval"); iw->WriteEnum("by_string", "stringval");
iw->WriteEnum<int32_t>("int32_t", 0, Enum_Currency); iw->WriteEnum<int32_t>("int32_t", 0, Enum_Currency);
@@ -110,13 +106,12 @@ TEST_F(IniWriterTest, create_loose_enum_entry)
const char* ini = reinterpret_cast<const char*>(ms.ReadString()); const char* ini = reinterpret_cast<const char*>(ms.ReadString());
ASSERT_STREQ(ini, "by_string = stringval" PLATFORM_NEWLINE "int32_t = 0" PLATFORM_NEWLINE); ASSERT_STREQ(ini, "by_string = stringval" PLATFORM_NEWLINE "int32_t = 0" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_loose_float_entry) TEST_F(IniWriterTest, create_loose_float_entry)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteFloat("one", 1.); iw->WriteFloat("one", 1.);
uint8_t null_terminator = 0; uint8_t null_terminator = 0;
@@ -129,13 +124,12 @@ TEST_F(IniWriterTest, create_loose_float_entry)
// This will be non-fatal due to float. // This will be non-fatal due to float.
EXPECT_STREQ(ini, "one = 1.000000" PLATFORM_NEWLINE); EXPECT_STREQ(ini, "one = 1.000000" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_loose_int32_t_entry) TEST_F(IniWriterTest, create_loose_int32_t_entry)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteInt32("one", 1); iw->WriteInt32("one", 1);
iw->WriteInt32("zero", 0); 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 "one = 1" PLATFORM_NEWLINE "zero = 0" PLATFORM_NEWLINE "minusone = -1" PLATFORM_NEWLINE
"intmin = -2147483648" PLATFORM_NEWLINE "intmax = 2147483647" PLATFORM_NEWLINE); "intmin = -2147483648" PLATFORM_NEWLINE "intmax = 2147483647" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_loose_string_entry) TEST_F(IniWriterTest, create_loose_string_entry)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteString("path", u8"C:'\\some/dir\\here/神鷹暢遊"); iw->WriteString("path", u8"C:'\\some/dir\\here/神鷹暢遊");
uint8_t null_terminator = 0; uint8_t null_terminator = 0;
@@ -172,13 +165,12 @@ TEST_F(IniWriterTest, create_loose_string_entry)
const char* ini = reinterpret_cast<const char*>(ms.ReadString()); const char* ini = reinterpret_cast<const char*>(ms.ReadString());
ASSERT_STREQ(ini, "path = \"C:'\\\\some/dir\\\\here/\xE7\xA5\x9E\xE9\xB7\xB9\xE6\x9A\xA2\xE9\x81\x8A\"" PLATFORM_NEWLINE); 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); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_multiple_section_with_values) TEST_F(IniWriterTest, create_multiple_section_with_values)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteSection("bool"); iw->WriteSection("bool");
iw->WriteBoolean("boolval", true); 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 = " "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); "\"C:'\\\\some/dir\\\\here/\xE7\xA5\x9E\xE9\xB7\xB9\xE6\x9A\xA2\xE9\x81\x8A\"" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }
TEST_F(IniWriterTest, create_duplicate_sections) TEST_F(IniWriterTest, create_duplicate_sections)
{ {
OpenRCT2::MemoryStream ms(1000); OpenRCT2::MemoryStream ms(1000);
IIniWriter* iw = CreateIniWriter(&ms); auto iw = CreateIniWriter(&ms);
ASSERT_NE(iw, nullptr); ASSERT_NE(iw, nullptr);
iw->WriteSection("section"); iw->WriteSection("section");
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 PLATFORM_NEWLINE "[section]" PLATFORM_NEWLINE PLATFORM_NEWLINE
"[section]" PLATFORM_NEWLINE); "[section]" PLATFORM_NEWLINE);
Memory::Free(ini); Memory::Free(ini);
delete iw;
} }