diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 2d4bd86de6..807a3b4217 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -31,7 +31,8 @@ "includePath": [ "/usr/include", "/usr/local/include", - "${workspaceRoot}" + "${workspaceRoot}", + "${workspaceRoot}/src" ], "defines": [], "intelliSenseMode": "clang-x64", diff --git a/src/openrct2-cli/Cli.cpp b/src/openrct2-cli/Cli.cpp index e3f354a71c..07d0a7444e 100644 --- a/src/openrct2-cli/Cli.cpp +++ b/src/openrct2-cli/Cli.cpp @@ -36,7 +36,6 @@ int main(int argc, const char * * argv) // Run OpenRCT2 with a plain context auto context = CreateContext(); context->RunOpenRCT2(argc, argv); - delete context; } return gExitCode; } diff --git a/src/openrct2-ui/Ui.cpp b/src/openrct2-ui/Ui.cpp index b94c3e6720..7a6b834e2e 100644 --- a/src/openrct2-ui/Ui.cpp +++ b/src/openrct2-ui/Ui.cpp @@ -29,6 +29,12 @@ using namespace OpenRCT2; using namespace OpenRCT2::Audio; using namespace OpenRCT2::Ui; +template +static std::shared_ptr to_shared(std::unique_ptr&& src) +{ + return std::shared_ptr(std::move(src)); +} + /** * Main entry point for non-Windows systems. Windows instead uses its own DLL proxy. */ @@ -47,21 +53,16 @@ int main(int argc, const char * * argv) // Run OpenRCT2 with a plain context auto context = CreateContext(); context->RunOpenRCT2(argc, argv); - delete context; } else { // Run OpenRCT2 with a UI context - auto env = CreatePlatformEnvironment(); - auto audioContext = CreateAudioContext(); - auto uiContext = CreateUiContext(env); + auto env = to_shared(CreatePlatformEnvironment()); + auto audioContext = to_shared(CreateAudioContext()); + auto uiContext = to_shared(CreateUiContext(env)); auto context = CreateContext(env, audioContext, uiContext); context->RunOpenRCT2(argc, argv); - - delete context; - delete uiContext; - delete audioContext; } } return gExitCode; diff --git a/src/openrct2-ui/UiContext.cpp b/src/openrct2-ui/UiContext.cpp index e652f041de..bf63f3222f 100644 --- a/src/openrct2-ui/UiContext.cpp +++ b/src/openrct2-ui/UiContext.cpp @@ -94,7 +94,7 @@ private: public: InGameConsole& GetInGameConsole() { return _inGameConsole; } - explicit UiContext(IPlatformEnvironment * env) + explicit UiContext(std::shared_ptr env) : _platformUiContext(CreatePlatformUiContext()), _windowManager(CreateWindowManager()), _keyboardShortcuts(env) @@ -814,9 +814,9 @@ private: } }; -IUiContext * OpenRCT2::Ui::CreateUiContext(IPlatformEnvironment * env) +std::unique_ptr OpenRCT2::Ui::CreateUiContext(std::shared_ptr env) { - return new UiContext(env); + return std::make_unique(env); } InGameConsole& OpenRCT2::Ui::GetInGameConsole() diff --git a/src/openrct2-ui/UiContext.h b/src/openrct2-ui/UiContext.h index 0f879e0e8a..31708474c9 100644 --- a/src/openrct2-ui/UiContext.h +++ b/src/openrct2-ui/UiContext.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include @@ -43,7 +44,7 @@ namespace OpenRCT2 virtual std::string ShowDirectoryDialog(SDL_Window * window, const std::string &title) abstract; }; - IUiContext * CreateUiContext(IPlatformEnvironment * env); + std::unique_ptr CreateUiContext(std::shared_ptr env); IPlatformUiContext * CreatePlatformUiContext(); InGameConsole& GetInGameConsole(); diff --git a/src/openrct2-ui/audio/AudioContext.cpp b/src/openrct2-ui/audio/AudioContext.cpp index f5c0616a2a..d66b5bda7d 100644 --- a/src/openrct2-ui/audio/AudioContext.cpp +++ b/src/openrct2-ui/audio/AudioContext.cpp @@ -94,8 +94,8 @@ namespace OpenRCT2::Audio void StopVehicleSounds() override { } }; - IAudioContext * CreateAudioContext() + std::unique_ptr CreateAudioContext() { - return new AudioContext(); + return std::make_unique(); } } // namespace OpenRCT2::Audio diff --git a/src/openrct2-ui/audio/AudioContext.h b/src/openrct2-ui/audio/AudioContext.h index 3a04fe6381..b2a9f62ff5 100644 --- a/src/openrct2-ui/audio/AudioContext.h +++ b/src/openrct2-ui/audio/AudioContext.h @@ -1,6 +1,7 @@ #pragma once +#include #include #include #include @@ -69,5 +70,6 @@ namespace OpenRCT2::Audio IAudioMixer * Create(); } - IAudioContext * CreateAudioContext(); + std::unique_ptr CreateAudioContext(); + } // namespace OpenRCT2::Audio diff --git a/src/openrct2-ui/input/KeyboardShortcuts.cpp b/src/openrct2-ui/input/KeyboardShortcuts.cpp index e111c7b878..2c05711a45 100644 --- a/src/openrct2-ui/input/KeyboardShortcuts.cpp +++ b/src/openrct2-ui/input/KeyboardShortcuts.cpp @@ -32,7 +32,7 @@ using namespace OpenRCT2::Input; // Remove when the C calls are removed static KeyboardShortcuts * _instance; -KeyboardShortcuts::KeyboardShortcuts(IPlatformEnvironment * env) +KeyboardShortcuts::KeyboardShortcuts(std::shared_ptr env) : _env(env) { _instance = this; diff --git a/src/openrct2-ui/input/KeyboardShortcuts.h b/src/openrct2-ui/input/KeyboardShortcuts.h index 28c9482bf3..acd50b1247 100644 --- a/src/openrct2-ui/input/KeyboardShortcuts.h +++ b/src/openrct2-ui/input/KeyboardShortcuts.h @@ -16,6 +16,7 @@ #pragma once +#include #include #define SHIFT 0x100 @@ -119,11 +120,11 @@ namespace OpenRCT2 constexpr static sint32 CURRENT_FILE_VERSION = 1; static const uint16 DefaultKeys[SHORTCUT_COUNT]; - IPlatformEnvironment * const _env; + std::shared_ptr const _env; uint16 _keys[SHORTCUT_COUNT]; public: - KeyboardShortcuts(IPlatformEnvironment * env); + KeyboardShortcuts(std::shared_ptr env); void Reset(); bool Load(); diff --git a/src/openrct2/Context.cpp b/src/openrct2/Context.cpp index c49da24781..74fc2a1ab0 100644 --- a/src/openrct2/Context.cpp +++ b/src/openrct2/Context.cpp @@ -82,9 +82,9 @@ namespace OpenRCT2 { private: // Dependencies - IPlatformEnvironment * const _env = nullptr; - IAudioContext * const _audioContext = nullptr; - IUiContext * const _uiContext = nullptr; + std::shared_ptr const _env; + std::shared_ptr const _audioContext; + std::shared_ptr const _uiContext; // Services IObjectRepository * _objectRepository = nullptr; @@ -116,7 +116,10 @@ namespace OpenRCT2 static Context * Instance; public: - Context(IPlatformEnvironment * env, IAudioContext * audioContext, IUiContext * uiContext) + Context( + std::shared_ptr env, + std::shared_ptr audioContext, + std::shared_ptr uiContext) : _env(env), _audioContext(audioContext), _uiContext(uiContext) @@ -153,17 +156,17 @@ namespace OpenRCT2 IAudioContext * GetAudioContext() override { - return _audioContext; + return _audioContext.get(); } IUiContext * GetUiContext() override { - return _uiContext; + return _uiContext.get(); } IPlatformEnvironment * GetPlatformEnvironment() override { - return _env; + return _env.get(); } IObjectManager * GetObjectManager() override @@ -548,7 +551,7 @@ namespace OpenRCT2 bool LoadBaseGraphics() { - if (!gfx_load_g1(_env)) + if (!gfx_load_g1(*_env)) { return false; } @@ -902,9 +905,9 @@ namespace OpenRCT2 class PlainContext final : public Context { - std::unique_ptr _env; - std::unique_ptr _audioContext; - std::unique_ptr _uiContext; + std::shared_ptr _env; + std::shared_ptr _audioContext; + std::shared_ptr _uiContext; public: PlainContext() @@ -912,25 +915,31 @@ namespace OpenRCT2 { } - PlainContext(IPlatformEnvironment * env, IAudioContext * audioContext, IUiContext * uiContext) - : Context(env, audioContext, uiContext) + PlainContext( + std::shared_ptr env, + std::shared_ptr audioContext, + std::shared_ptr uiContext) + : Context(env, audioContext, uiContext), + _env(env), + _audioContext(audioContext), + _uiContext(uiContext) { - _env = std::unique_ptr(env); - _audioContext = std::unique_ptr(audioContext); - _uiContext = std::unique_ptr(uiContext); } }; Context * Context::Instance = nullptr; - IContext * CreateContext() + std::unique_ptr CreateContext() { - return new PlainContext(); + return std::make_unique(); } - IContext * CreateContext(IPlatformEnvironment * env, Audio::IAudioContext * audioContext, IUiContext * uiContext) + std::unique_ptr CreateContext( + std::shared_ptr env, + std::shared_ptr audioContext, + std::shared_ptr uiContext) { - return new Context(env, audioContext, uiContext); + return std::make_unique(env, audioContext, uiContext); } IContext * GetContext() diff --git a/src/openrct2/Context.h b/src/openrct2/Context.h index 9d9bc1b2e9..e368935b2f 100644 --- a/src/openrct2/Context.h +++ b/src/openrct2/Context.h @@ -18,6 +18,7 @@ #include "common.h" +#include #include interface IObjectManager; @@ -111,8 +112,11 @@ namespace OpenRCT2 virtual std::string GetPathLegacy(sint32 pathId) abstract; }; - IContext * CreateContext(); - IContext * CreateContext(IPlatformEnvironment * env, Audio::IAudioContext * audioContext, Ui::IUiContext * uiContext); + std::unique_ptr CreateContext(); + std::unique_ptr CreateContext( + std::shared_ptr env, + std::shared_ptr audioContext, + std::shared_ptr uiContext); IContext * GetContext(); } // namespace OpenRCT2 diff --git a/src/openrct2/PlatformEnvironment.cpp b/src/openrct2/PlatformEnvironment.cpp index 40a56f3364..814d454f27 100644 --- a/src/openrct2/PlatformEnvironment.cpp +++ b/src/openrct2/PlatformEnvironment.cpp @@ -109,9 +109,9 @@ private: } }; -IPlatformEnvironment * OpenRCT2::CreatePlatformEnvironment(DIRBASE_VALUES basePaths) +std::unique_ptr OpenRCT2::CreatePlatformEnvironment(DIRBASE_VALUES basePaths) { - return new PlatformEnvironment(basePaths); + return std::make_unique(basePaths); } static std::string GetOpenRCT2DirectoryName() @@ -123,7 +123,7 @@ static std::string GetOpenRCT2DirectoryName() #endif } -IPlatformEnvironment * OpenRCT2::CreatePlatformEnvironment() +std::unique_ptr OpenRCT2::CreatePlatformEnvironment() { auto subDirectory = GetOpenRCT2DirectoryName(); diff --git a/src/openrct2/PlatformEnvironment.h b/src/openrct2/PlatformEnvironment.h index 36223a4694..77e0c3b865 100644 --- a/src/openrct2/PlatformEnvironment.h +++ b/src/openrct2/PlatformEnvironment.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include "common.h" @@ -82,6 +83,7 @@ namespace OpenRCT2 virtual void SetBasePath(DIRBASE base, const std::string &path) abstract; }; - IPlatformEnvironment * CreatePlatformEnvironment(DIRBASE_VALUES basePaths); - IPlatformEnvironment * CreatePlatformEnvironment(); + std::unique_ptr CreatePlatformEnvironment(DIRBASE_VALUES basePaths); + std::unique_ptr CreatePlatformEnvironment(); + } // namespace OpenRCT2 diff --git a/src/openrct2/audio/AudioContext.h b/src/openrct2/audio/AudioContext.h index fc6a00f75b..c7079f5706 100644 --- a/src/openrct2/audio/AudioContext.h +++ b/src/openrct2/audio/AudioContext.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include "../common.h" @@ -59,5 +60,6 @@ namespace OpenRCT2::Audio virtual void StopVehicleSounds() abstract; }; - IAudioContext * CreateDummyAudioContext(); + std::unique_ptr CreateDummyAudioContext(); + } // namespace OpenRCT2::Audio diff --git a/src/openrct2/audio/DummyAudioContext.cpp b/src/openrct2/audio/DummyAudioContext.cpp index 7161ae21e5..a4b94a9321 100644 --- a/src/openrct2/audio/DummyAudioContext.cpp +++ b/src/openrct2/audio/DummyAudioContext.cpp @@ -45,8 +45,8 @@ namespace OpenRCT2::Audio void StopVehicleSounds() override { } }; - IAudioContext * CreateDummyAudioContext() + std::unique_ptr CreateDummyAudioContext() { - return new DummyAudioContext(); + return std::make_unique(); } } // namespace OpenRCT2::Audio diff --git a/src/openrct2/cmdline/RootCommands.cpp b/src/openrct2/cmdline/RootCommands.cpp index 37e246fdb3..16841f7a8e 100644 --- a/src/openrct2/cmdline/RootCommands.cpp +++ b/src/openrct2/cmdline/RootCommands.cpp @@ -399,7 +399,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer auto context = std::unique_ptr(OpenRCT2::CreateContext()); auto env = context->GetPlatformEnvironment(); - auto objectRepository = std::unique_ptr(CreateObjectRepository(env)); + auto objectRepository = CreateObjectRepository(env); // HACK: set gCurrentLanguage otherwise it be wrong for the index file gCurrentLanguage = gConfigGeneral.language; diff --git a/src/openrct2/core/Guard.hpp b/src/openrct2/core/Guard.hpp index eb127feb5e..a8b32197a2 100644 --- a/src/openrct2/core/Guard.hpp +++ b/src/openrct2/core/Guard.hpp @@ -16,6 +16,7 @@ #pragma once +#include #include #include @@ -52,6 +53,15 @@ namespace Guard va_end(args); } + template + static void ArgumentNotNull(const std::shared_ptr& argument, const char * message = nullptr, ...) + { + va_list args; + va_start(args, message); + Assert_VA(argument != nullptr, message, args); + va_end(args); + } + template static void ArgumentInRange(T argument, T min, T max, const char * message = nullptr, ...) { diff --git a/src/openrct2/drawing/Drawing.h b/src/openrct2/drawing/Drawing.h index 7e4b8378fb..b2023e5dee 100644 --- a/src/openrct2/drawing/Drawing.h +++ b/src/openrct2/drawing/Drawing.h @@ -20,6 +20,11 @@ #include "../common.h" #include "../interface/Colour.h" +namespace OpenRCT2 +{ + interface IPlatformEnvironment; +} + struct rct_g1_element { uint8* offset; // 0x00 sint16 width; // 0x04 @@ -281,7 +286,7 @@ void gfx_fill_rect_inset(rct_drawpixelinfo* dpi, sint16 left, sint16 top, sint16 void gfx_filter_rect(rct_drawpixelinfo *dpi, sint32 left, sint32 top, sint32 right, sint32 bottom, FILTER_PALETTE_ID palette); // sprite -bool gfx_load_g1(void * platformEnvironment); +bool gfx_load_g1(const OpenRCT2::IPlatformEnvironment& env); bool gfx_load_g2(); bool gfx_load_csg(); void gfx_unload_g1(); diff --git a/src/openrct2/drawing/Sprite.cpp b/src/openrct2/drawing/Sprite.cpp index 1d4ca49e9a..ab212555f8 100644 --- a/src/openrct2/drawing/Sprite.cpp +++ b/src/openrct2/drawing/Sprite.cpp @@ -227,14 +227,12 @@ bool gTinyFontAntiAliased = false; * * rct2: 0x00678998 */ -bool gfx_load_g1(void * platformEnvironment) +bool gfx_load_g1(const IPlatformEnvironment& env) { - auto env = (IPlatformEnvironment *)platformEnvironment; - log_verbose("gfx_load_g1(...)"); try { - auto path = Path::Combine(env->GetDirectoryPath(DIRBASE::RCT2, DIRID::DATA), "g1.dat"); + auto path = Path::Combine(env.GetDirectoryPath(DIRBASE::RCT2, DIRID::DATA), "g1.dat"); auto fs = FileStream(path, FILE_MODE_OPEN); _g1.header = fs.ReadValue(); diff --git a/src/openrct2/interface/Screenshot.cpp b/src/openrct2/interface/Screenshot.cpp index e559f09eda..fc47453757 100644 --- a/src/openrct2/interface/Screenshot.cpp +++ b/src/openrct2/interface/Screenshot.cpp @@ -448,7 +448,6 @@ sint32 cmdline_for_screenshot(const char * * argv, sint32 argc, ScreenshotOption { std::printf("%s\n", e.what()); drawing_engine_dispose(); - delete context; return -1; } @@ -518,7 +517,6 @@ sint32 cmdline_for_screenshot(const char * * argv, sint32 argc, ScreenshotOption { std::printf("Weather can only be set to an integer value from 1 till 6."); drawing_engine_dispose(); - delete context; return -1; } @@ -583,6 +581,5 @@ sint32 cmdline_for_screenshot(const char * * argv, sint32 argc, ScreenshotOption free(dpi.bits); drawing_engine_dispose(); } - delete context; return 1; } diff --git a/src/openrct2/network/Network.cpp b/src/openrct2/network/Network.cpp index 35802c7ac2..cd86316fda 100644 --- a/src/openrct2/network/Network.cpp +++ b/src/openrct2/network/Network.cpp @@ -142,7 +142,7 @@ Network::~Network() CloseConnection(); } -void Network::SetEnvironment(IPlatformEnvironment * env) +void Network::SetEnvironment(std::shared_ptr env) { _env = env; } @@ -2545,9 +2545,9 @@ void Network::Client_Handle_GAMEINFO(NetworkConnection& connection, NetworkPacke network_chat_show_server_greeting(); } -void network_set_env(void * env) +void network_set_env(std::shared_ptr env) { - gNetwork.SetEnvironment((IPlatformEnvironment *)env); + gNetwork.SetEnvironment(env); } void network_close() @@ -3327,7 +3327,7 @@ sint32 network_get_pickup_peep_old_x(uint8 playerid) { return _pickup_peep_old_x void network_send_chat(const char* text) {} void network_send_password(const char* password) {} void network_close() {} -void network_set_env(void * env) {} +void network_set_env(std::shared_ptr) {} void network_shutdown_client() {} void network_set_password(const char* password) {} uint8 network_get_current_player_id() { return 0; } diff --git a/src/openrct2/network/network.h b/src/openrct2/network/network.h index 271df19d1c..2b9fe070ee 100644 --- a/src/openrct2/network/network.h +++ b/src/openrct2/network/network.h @@ -16,6 +16,7 @@ #pragma once +#include #include enum { @@ -49,12 +50,16 @@ struct GameAction; struct rct_peep; struct LocationXYZ16; +namespace OpenRCT2 +{ + interface IPlatformEnvironment; +} + #ifndef DISABLE_NETWORK #include #include #include -#include #include #include #include @@ -79,17 +84,12 @@ enum { struct ObjectRepositoryItem; -namespace OpenRCT2 -{ - interface IPlatformEnvironment; -} - class Network { public: Network(); ~Network(); - void SetEnvironment(OpenRCT2::IPlatformEnvironment * env); + void SetEnvironment(std::shared_ptr env); bool Init(); void Close(); bool BeginClient(const char* host, uint16 port); @@ -259,7 +259,7 @@ private: std::string _chatLogFilenameFormat = "%Y%m%d-%H%M%S.txt"; std::string _serverLogPath; std::string _serverLogFilenameFormat = "%Y%m%d-%H%M%S.txt"; - OpenRCT2::IPlatformEnvironment * _env = nullptr; + std::shared_ptr _env; void UpdateServer(); void UpdateClient(); @@ -301,7 +301,7 @@ private: #endif /* DISABLE_NETWORK */ -void network_set_env(void * env); +void network_set_env(std::shared_ptr env); void network_close(); void network_shutdown_client(); sint32 network_begin_client(const char *host, sint32 port); diff --git a/src/openrct2/object/ObjectRepository.cpp b/src/openrct2/object/ObjectRepository.cpp index 42a5f18799..c29cc1401a 100644 --- a/src/openrct2/object/ObjectRepository.cpp +++ b/src/openrct2/object/ObjectRepository.cpp @@ -86,15 +86,15 @@ private: IObjectRepository& _objectRepository; public: - explicit ObjectFileIndex(IObjectRepository& objectRepository, IPlatformEnvironment * env) : + explicit ObjectFileIndex(IObjectRepository& objectRepository, const IPlatformEnvironment& env) : FileIndex("object index", MAGIC_NUMBER, VERSION, - env->GetFilePath(PATHID::CACHE_OBJECTS), + env.GetFilePath(PATHID::CACHE_OBJECTS), std::string(PATTERN), std::vector({ - env->GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::OBJECT), - env->GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT) })), + env.GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::OBJECT), + env.GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT) })), _objectRepository(objectRepository) { } @@ -208,15 +208,15 @@ private: class ObjectRepository final : public IObjectRepository { - IPlatformEnvironment * const _env = nullptr; - ObjectFileIndex const _fileIndex; - std::vector _items; - ObjectEntryMap _itemMap; + std::shared_ptr const _env; + ObjectFileIndex const _fileIndex; + std::vector _items; + ObjectEntryMap _itemMap; public: - explicit ObjectRepository(IPlatformEnvironment * env) + explicit ObjectRepository(std::shared_ptr env) : _env(env), - _fileIndex(*this, env) + _fileIndex(*this, *env) { } @@ -637,7 +637,7 @@ private: } }; -IObjectRepository * CreateObjectRepository(IPlatformEnvironment * env) +IObjectRepository * CreateObjectRepository(std::shared_ptr env) { return new ObjectRepository(env); } diff --git a/src/openrct2/object/ObjectRepository.h b/src/openrct2/object/ObjectRepository.h index 998f0ce379..2e62a595bd 100644 --- a/src/openrct2/object/ObjectRepository.h +++ b/src/openrct2/object/ObjectRepository.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include "../common.h" #include "../object/Object.h" @@ -77,7 +78,7 @@ interface IObjectRepository virtual void WritePackedObjects(IStream * stream, std::vector &objects) abstract; }; -IObjectRepository * CreateObjectRepository(OpenRCT2::IPlatformEnvironment * env); +IObjectRepository * CreateObjectRepository(std::shared_ptr env); bool IsObjectCustom(const ObjectRepositoryItem * object); diff --git a/src/openrct2/ride/TrackDesignRepository.cpp b/src/openrct2/ride/TrackDesignRepository.cpp index 66e59538c3..b2e61d5f9e 100644 --- a/src/openrct2/ride/TrackDesignRepository.cpp +++ b/src/openrct2/ride/TrackDesignRepository.cpp @@ -66,16 +66,16 @@ private: static constexpr auto PATTERN = "*.td4;*.td6"; public: - explicit TrackDesignFileIndex(IPlatformEnvironment * env) : + explicit TrackDesignFileIndex(const IPlatformEnvironment &env) : FileIndex("track design index", MAGIC_NUMBER, VERSION, - env->GetFilePath(PATHID::CACHE_TRACKS), + env.GetFilePath(PATHID::CACHE_TRACKS), std::string(PATTERN), std::vector({ - env->GetDirectoryPath(DIRBASE::RCT1, DIRID::TRACK), - env->GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK), - env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK) })) + env.GetDirectoryPath(DIRBASE::RCT1, DIRID::TRACK), + env.GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK), + env.GetDirectoryPath(DIRBASE::USER, DIRID::TRACK) })) { } @@ -137,14 +137,14 @@ private: class TrackDesignRepository final : public ITrackDesignRepository { private: - IPlatformEnvironment * const _env; + std::shared_ptr const _env; TrackDesignFileIndex const _fileIndex; std::vector _items; public: - explicit TrackDesignRepository(IPlatformEnvironment * env) + explicit TrackDesignRepository(std::shared_ptr env) : _env(env), - _fileIndex(env) + _fileIndex(*env) { Guard::ArgumentNotNull(env); } @@ -396,7 +396,7 @@ private: } }; -ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env) +ITrackDesignRepository * CreateTrackDesignRepository(std::shared_ptr env) { return new TrackDesignRepository(env); } diff --git a/src/openrct2/ride/TrackDesignRepository.h b/src/openrct2/ride/TrackDesignRepository.h index 7da6a7fdcc..c5d09129bd 100644 --- a/src/openrct2/ride/TrackDesignRepository.h +++ b/src/openrct2/ride/TrackDesignRepository.h @@ -16,6 +16,7 @@ #pragma once +#include #include "../common.h" #include "RideGroupManager.h" @@ -51,7 +52,7 @@ interface ITrackDesignRepository virtual std::string Install(const std::string &path) abstract; }; -ITrackDesignRepository * CreateTrackDesignRepository(OpenRCT2::IPlatformEnvironment * env); +ITrackDesignRepository * CreateTrackDesignRepository(std::shared_ptr env); std::string GetNameFromTrackPath(const std::string &path); void track_repository_scan(); diff --git a/src/openrct2/scenario/ScenarioRepository.cpp b/src/openrct2/scenario/ScenarioRepository.cpp index 04c84c70d9..2992a65796 100644 --- a/src/openrct2/scenario/ScenarioRepository.cpp +++ b/src/openrct2/scenario/ScenarioRepository.cpp @@ -128,16 +128,16 @@ private: static constexpr auto PATTERN = "*.sc4;*.sc6"; public: - explicit ScenarioFileIndex(IPlatformEnvironment * env) : + explicit ScenarioFileIndex(const IPlatformEnvironment& env) : FileIndex("scenario index", MAGIC_NUMBER, VERSION, - env->GetFilePath(PATHID::CACHE_SCENARIOS), + env.GetFilePath(PATHID::CACHE_SCENARIOS), std::string(PATTERN), std::vector({ - env->GetDirectoryPath(DIRBASE::RCT1, DIRID::SCENARIO), - env->GetDirectoryPath(DIRBASE::RCT2, DIRID::SCENARIO), - env->GetDirectoryPath(DIRBASE::USER, DIRID::SCENARIO) })) + env.GetDirectoryPath(DIRBASE::RCT1, DIRID::SCENARIO), + env.GetDirectoryPath(DIRBASE::RCT2, DIRID::SCENARIO), + env.GetDirectoryPath(DIRBASE::USER, DIRID::SCENARIO) })) { } @@ -322,15 +322,15 @@ class ScenarioRepository final : public IScenarioRepository private: static constexpr uint32 HighscoreFileVersion = 1; - IPlatformEnvironment * const _env; + std::shared_ptr const _env; ScenarioFileIndex const _fileIndex; std::vector _scenarios; std::vector _highscores; public: - explicit ScenarioRepository(IPlatformEnvironment * env) + explicit ScenarioRepository(std::shared_ptr env) : _env(env), - _fileIndex(env) + _fileIndex(*env) { } @@ -729,7 +729,7 @@ private: static ScenarioRepository * _scenarioRepository; -IScenarioRepository * CreateScenarioRepository(IPlatformEnvironment * env) +IScenarioRepository * CreateScenarioRepository(std::shared_ptr env) { _scenarioRepository = new ScenarioRepository(env); return _scenarioRepository; diff --git a/src/openrct2/scenario/ScenarioRepository.h b/src/openrct2/scenario/ScenarioRepository.h index 4c83ed3fbe..90e4c912c7 100644 --- a/src/openrct2/scenario/ScenarioRepository.h +++ b/src/openrct2/scenario/ScenarioRepository.h @@ -16,6 +16,7 @@ #pragma once +#include #include "../common.h" struct rct_object_entry; @@ -77,7 +78,7 @@ interface IScenarioRepository virtual bool TryRecordHighscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) abstract; }; -IScenarioRepository * CreateScenarioRepository(OpenRCT2::IPlatformEnvironment * env); +IScenarioRepository * CreateScenarioRepository(std::shared_ptr env); IScenarioRepository * GetScenarioRepository(); void scenario_repository_scan(); diff --git a/src/openrct2/ui/DummyUiContext.cpp b/src/openrct2/ui/DummyUiContext.cpp index 9d882d292c..24bd70554f 100644 --- a/src/openrct2/ui/DummyUiContext.cpp +++ b/src/openrct2/ui/DummyUiContext.cpp @@ -92,8 +92,8 @@ namespace OpenRCT2::Ui ~DummyUiContext() { delete _windowManager; } }; - IUiContext * CreateDummyUiContext() + std::shared_ptr CreateDummyUiContext() { - return new DummyUiContext(); + return std::make_unique(); } } // namespace OpenRCT2::Ui diff --git a/src/openrct2/ui/UiContext.h b/src/openrct2/ui/UiContext.h index a511f794f5..c90b55a18d 100644 --- a/src/openrct2/ui/UiContext.h +++ b/src/openrct2/ui/UiContext.h @@ -149,6 +149,6 @@ namespace OpenRCT2 virtual bool SetClipboardText(const utf8* target) abstract; }; - IUiContext * CreateDummyUiContext(); + std::shared_ptr CreateDummyUiContext(); } // namespace Ui } // namespace OpenRCT2 diff --git a/test/tests/MultiLaunch.cpp b/test/tests/MultiLaunch.cpp index c0ab60b66c..dbd8b939cf 100644 --- a/test/tests/MultiLaunch.cpp +++ b/test/tests/MultiLaunch.cpp @@ -46,8 +46,6 @@ TEST(MultiLaunchTest, all) // Check ride count again ASSERT_EQ(gRideCount, 134); - - delete context; } SUCCEED(); } diff --git a/test/tests/RideRatings.cpp b/test/tests/RideRatings.cpp index 8b31312cae..52178af78b 100644 --- a/test/tests/RideRatings.cpp +++ b/test/tests/RideRatings.cpp @@ -91,6 +91,4 @@ TEST_F(RideRatings, all) expI++; } } - - delete context; } diff --git a/test/tests/TileElements.cpp b/test/tests/TileElements.cpp index 37d57dad6d..6d11e4b1ec 100644 --- a/test/tests/TileElements.cpp +++ b/test/tests/TileElements.cpp @@ -28,18 +28,11 @@ protected: SUCCEED(); } - static void TearDownTestCase() - { - delete _context; - _context = nullptr; - SUCCEED(); - } - private: - static IContext * _context; + static std::shared_ptr _context; }; -IContext * TileElementWantsFootpathConnection::_context = nullptr; +std::shared_ptr TileElementWantsFootpathConnection::_context; TEST_F(TileElementWantsFootpathConnection, FlatPath) {