Use more smart ptrs and u8strings (#17611)

* Use smart ptr for PlatformUiContext and WindowManager

* Remove more delete calls

* Apply PR feedback
This commit is contained in:
Raymond Zhao 2022-07-28 10:19:38 -07:00 committed by GitHub
parent e5d2e3001d
commit 7399163a3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 25 additions and 33 deletions

View File

@ -88,9 +88,9 @@ namespace OpenRCT2::Ui
}
};
IPlatformUiContext* CreatePlatformUiContext()
std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext()
{
return new AndroidContext();
return std::make_unique<AndroidContext>();
}
} // namespace OpenRCT2::Ui

View File

@ -417,9 +417,9 @@ namespace OpenRCT2::Ui
}
};
IPlatformUiContext* CreatePlatformUiContext()
std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext()
{
return new LinuxContext();
return std::make_unique<LinuxContext>();
}
} // namespace OpenRCT2::Ui

View File

@ -241,9 +241,9 @@ namespace OpenRCT2::Ui
}
};
IPlatformUiContext* CreatePlatformUiContext()
std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext()
{
return new Win32Context();
return std::make_unique<Win32Context>();
}
} // namespace OpenRCT2::Ui

View File

@ -65,8 +65,8 @@ class UiContext final : public IUiContext
private:
constexpr static uint32_t TOUCH_DOUBLE_TIMEOUT = 300;
IPlatformUiContext* const _platformUiContext;
IWindowManager* const _windowManager;
const std::unique_ptr<IPlatformUiContext> _platformUiContext;
const std::unique_ptr<IWindowManager> _windowManager;
CursorRepository _cursorRepository;
@ -126,9 +126,7 @@ public:
~UiContext() override
{
UiContext::CloseWindow();
delete _windowManager;
SDL_QuitSubSystem(SDL_INIT_VIDEO);
delete _platformUiContext;
}
void Initialise() override
@ -679,7 +677,7 @@ public:
IWindowManager* GetWindowManager() override
{
return _windowManager;
return _windowManager.get();
}
bool SetClipboardText(const utf8* target) override

View File

@ -47,7 +47,7 @@ namespace OpenRCT2
};
[[nodiscard]] std::unique_ptr<IUiContext> CreateUiContext(const std::shared_ptr<IPlatformEnvironment>& env);
[[nodiscard]] IPlatformUiContext* CreatePlatformUiContext();
[[nodiscard]] std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext();
[[nodiscard]] InGameConsole& GetInGameConsole();
} // namespace Ui

View File

@ -220,9 +220,9 @@ namespace OpenRCT2::Ui
}
};
IPlatformUiContext* CreatePlatformUiContext()
std::unique_ptr<IPlatformUiContext> CreatePlatformUiContext()
{
return new macOSContext();
return std::make_unique<macOSContext>();
}
} // namespace OpenRCT2::Ui

View File

@ -592,7 +592,7 @@ public:
}
};
IWindowManager* OpenRCT2::Ui::CreateWindowManager()
std::unique_ptr<IWindowManager> OpenRCT2::Ui::CreateWindowManager()
{
return new WindowManager();
return std::make_unique<WindowManager>();
}

View File

@ -9,11 +9,12 @@
#pragma once
#include <memory>
#include <openrct2/common.h>
namespace OpenRCT2::Ui
{
struct IWindowManager;
IWindowManager* CreateWindowManager();
std::unique_ptr<IWindowManager> CreateWindowManager();
} // namespace OpenRCT2::Ui

View File

@ -348,9 +348,8 @@ static ScreenCoordsXY WindowMultiplayerInformationGetSize()
// Server name is displayed word-wrapped, so figure out how high it will be.
{
utf8* buffer = String::Duplicate(network_get_server_name());
gfx_wrap_string(buffer, width, FontSpriteBase::MEDIUM, &numLines);
delete buffer;
u8string buffer = network_get_server_name();
gfx_wrap_string(buffer.data(), width, FontSpriteBase::MEDIUM, &numLines);
height += ++numLines * lineHeight + (LIST_ROW_HEIGHT / 2);
}
@ -358,9 +357,8 @@ static ScreenCoordsXY WindowMultiplayerInformationGetSize()
const utf8* descString = network_get_server_description();
if (!str_is_null_or_empty(descString))
{
utf8* buffer = String::Duplicate(descString);
gfx_wrap_string(buffer, width, FontSpriteBase::MEDIUM, &numLines);
delete buffer;
u8string buffer = descString;
gfx_wrap_string(buffer.data(), width, FontSpriteBase::MEDIUM, &numLines);
height += ++numLines * lineHeight + (LIST_ROW_HEIGHT / 2);
}

View File

@ -22,7 +22,7 @@ namespace OpenRCT2::Ui
class DummyUiContext final : public IUiContext
{
private:
IWindowManager* const _windowManager = CreateDummyWindowManager();
std::unique_ptr<IWindowManager> const _windowManager = CreateDummyWindowManager();
public:
void Initialise() override
@ -190,7 +190,7 @@ namespace OpenRCT2::Ui
// In-game UI
IWindowManager* GetWindowManager() override
{
return _windowManager;
return _windowManager.get();
}
// Clipboard
@ -204,11 +204,6 @@ namespace OpenRCT2::Ui
return nullptr;
}
~DummyUiContext()
{
delete _windowManager;
}
bool HasFilePicker() const override
{
return false;

View File

@ -70,8 +70,8 @@ namespace OpenRCT2::Ui
}
};
IWindowManager* CreateDummyWindowManager()
std::unique_ptr<IWindowManager> CreateDummyWindowManager()
{
return new DummyWindowManager();
return std::make_unique<DummyWindowManager>();
}
} // namespace OpenRCT2::Ui

View File

@ -44,5 +44,5 @@ namespace OpenRCT2::Ui
virtual rct_window* GetOwner(const rct_viewport* viewport) abstract;
};
IWindowManager* CreateDummyWindowManager();
std::unique_ptr<IWindowManager> CreateDummyWindowManager();
} // namespace OpenRCT2::Ui