Use shared_ptr for long-life objects

Use unique_ptr and shared_ptr for IContext, IPlatformEnvironment, IUiContext, and IAudioContext.
This commit is contained in:
Ted John 2018-04-20 19:58:59 +01:00
parent c7b40cec7c
commit 7dc170ef85
33 changed files with 144 additions and 120 deletions

View File

@ -31,7 +31,8 @@
"includePath": [ "includePath": [
"/usr/include", "/usr/include",
"/usr/local/include", "/usr/local/include",
"${workspaceRoot}" "${workspaceRoot}",
"${workspaceRoot}/src"
], ],
"defines": [], "defines": [],
"intelliSenseMode": "clang-x64", "intelliSenseMode": "clang-x64",

View File

@ -36,7 +36,6 @@ int main(int argc, const char * * argv)
// Run OpenRCT2 with a plain context // Run OpenRCT2 with a plain context
auto context = CreateContext(); auto context = CreateContext();
context->RunOpenRCT2(argc, argv); context->RunOpenRCT2(argc, argv);
delete context;
} }
return gExitCode; return gExitCode;
} }

View File

@ -29,6 +29,12 @@ using namespace OpenRCT2;
using namespace OpenRCT2::Audio; using namespace OpenRCT2::Audio;
using namespace OpenRCT2::Ui; using namespace OpenRCT2::Ui;
template<typename T>
static std::shared_ptr<T> to_shared(std::unique_ptr<T>&& src)
{
return std::shared_ptr<T>(std::move(src));
}
/** /**
* Main entry point for non-Windows systems. Windows instead uses its own DLL proxy. * 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 // Run OpenRCT2 with a plain context
auto context = CreateContext(); auto context = CreateContext();
context->RunOpenRCT2(argc, argv); context->RunOpenRCT2(argc, argv);
delete context;
} }
else else
{ {
// Run OpenRCT2 with a UI context // Run OpenRCT2 with a UI context
auto env = CreatePlatformEnvironment(); auto env = to_shared(CreatePlatformEnvironment());
auto audioContext = CreateAudioContext(); auto audioContext = to_shared(CreateAudioContext());
auto uiContext = CreateUiContext(env); auto uiContext = to_shared(CreateUiContext(env));
auto context = CreateContext(env, audioContext, uiContext); auto context = CreateContext(env, audioContext, uiContext);
context->RunOpenRCT2(argc, argv); context->RunOpenRCT2(argc, argv);
delete context;
delete uiContext;
delete audioContext;
} }
} }
return gExitCode; return gExitCode;

View File

@ -94,7 +94,7 @@ private:
public: public:
InGameConsole& GetInGameConsole() { return _inGameConsole; } InGameConsole& GetInGameConsole() { return _inGameConsole; }
explicit UiContext(IPlatformEnvironment * env) explicit UiContext(std::shared_ptr<IPlatformEnvironment> env)
: _platformUiContext(CreatePlatformUiContext()), : _platformUiContext(CreatePlatformUiContext()),
_windowManager(CreateWindowManager()), _windowManager(CreateWindowManager()),
_keyboardShortcuts(env) _keyboardShortcuts(env)
@ -814,9 +814,9 @@ private:
} }
}; };
IUiContext * OpenRCT2::Ui::CreateUiContext(IPlatformEnvironment * env) std::unique_ptr<IUiContext> OpenRCT2::Ui::CreateUiContext(std::shared_ptr<IPlatformEnvironment> env)
{ {
return new UiContext(env); return std::make_unique<UiContext>(env);
} }
InGameConsole& OpenRCT2::Ui::GetInGameConsole() InGameConsole& OpenRCT2::Ui::GetInGameConsole()

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <openrct2/common.h> #include <openrct2/common.h>
@ -43,7 +44,7 @@ namespace OpenRCT2
virtual std::string ShowDirectoryDialog(SDL_Window * window, const std::string &title) abstract; virtual std::string ShowDirectoryDialog(SDL_Window * window, const std::string &title) abstract;
}; };
IUiContext * CreateUiContext(IPlatformEnvironment * env); std::unique_ptr<IUiContext> CreateUiContext(std::shared_ptr<IPlatformEnvironment> env);
IPlatformUiContext * CreatePlatformUiContext(); IPlatformUiContext * CreatePlatformUiContext();
InGameConsole& GetInGameConsole(); InGameConsole& GetInGameConsole();

View File

@ -94,8 +94,8 @@ namespace OpenRCT2::Audio
void StopVehicleSounds() override { } void StopVehicleSounds() override { }
}; };
IAudioContext * CreateAudioContext() std::unique_ptr<IAudioContext> CreateAudioContext()
{ {
return new AudioContext(); return std::make_unique<AudioContext>();
} }
} // namespace OpenRCT2::Audio } // namespace OpenRCT2::Audio

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <openrct2/common.h> #include <openrct2/common.h>
#include <openrct2/audio/AudioChannel.h> #include <openrct2/audio/AudioChannel.h>
@ -69,5 +70,6 @@ namespace OpenRCT2::Audio
IAudioMixer * Create(); IAudioMixer * Create();
} }
IAudioContext * CreateAudioContext(); std::unique_ptr<IAudioContext> CreateAudioContext();
} // namespace OpenRCT2::Audio } // namespace OpenRCT2::Audio

View File

@ -32,7 +32,7 @@ using namespace OpenRCT2::Input;
// Remove when the C calls are removed // Remove when the C calls are removed
static KeyboardShortcuts * _instance; static KeyboardShortcuts * _instance;
KeyboardShortcuts::KeyboardShortcuts(IPlatformEnvironment * env) KeyboardShortcuts::KeyboardShortcuts(std::shared_ptr<IPlatformEnvironment> env)
: _env(env) : _env(env)
{ {
_instance = this; _instance = this;

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <openrct2/common.h> #include <openrct2/common.h>
#define SHIFT 0x100 #define SHIFT 0x100
@ -119,11 +120,11 @@ namespace OpenRCT2
constexpr static sint32 CURRENT_FILE_VERSION = 1; constexpr static sint32 CURRENT_FILE_VERSION = 1;
static const uint16 DefaultKeys[SHORTCUT_COUNT]; static const uint16 DefaultKeys[SHORTCUT_COUNT];
IPlatformEnvironment * const _env; std::shared_ptr<IPlatformEnvironment> const _env;
uint16 _keys[SHORTCUT_COUNT]; uint16 _keys[SHORTCUT_COUNT];
public: public:
KeyboardShortcuts(IPlatformEnvironment * env); KeyboardShortcuts(std::shared_ptr<IPlatformEnvironment> env);
void Reset(); void Reset();
bool Load(); bool Load();

View File

@ -82,9 +82,9 @@ namespace OpenRCT2
{ {
private: private:
// Dependencies // Dependencies
IPlatformEnvironment * const _env = nullptr; std::shared_ptr<IPlatformEnvironment> const _env;
IAudioContext * const _audioContext = nullptr; std::shared_ptr<IAudioContext> const _audioContext;
IUiContext * const _uiContext = nullptr; std::shared_ptr<IUiContext> const _uiContext;
// Services // Services
IObjectRepository * _objectRepository = nullptr; IObjectRepository * _objectRepository = nullptr;
@ -116,7 +116,10 @@ namespace OpenRCT2
static Context * Instance; static Context * Instance;
public: public:
Context(IPlatformEnvironment * env, IAudioContext * audioContext, IUiContext * uiContext) Context(
std::shared_ptr<IPlatformEnvironment> env,
std::shared_ptr<IAudioContext> audioContext,
std::shared_ptr<IUiContext> uiContext)
: _env(env), : _env(env),
_audioContext(audioContext), _audioContext(audioContext),
_uiContext(uiContext) _uiContext(uiContext)
@ -153,17 +156,17 @@ namespace OpenRCT2
IAudioContext * GetAudioContext() override IAudioContext * GetAudioContext() override
{ {
return _audioContext; return _audioContext.get();
} }
IUiContext * GetUiContext() override IUiContext * GetUiContext() override
{ {
return _uiContext; return _uiContext.get();
} }
IPlatformEnvironment * GetPlatformEnvironment() override IPlatformEnvironment * GetPlatformEnvironment() override
{ {
return _env; return _env.get();
} }
IObjectManager * GetObjectManager() override IObjectManager * GetObjectManager() override
@ -548,7 +551,7 @@ namespace OpenRCT2
bool LoadBaseGraphics() bool LoadBaseGraphics()
{ {
if (!gfx_load_g1(_env)) if (!gfx_load_g1(*_env))
{ {
return false; return false;
} }
@ -902,9 +905,9 @@ namespace OpenRCT2
class PlainContext final : public Context class PlainContext final : public Context
{ {
std::unique_ptr<IPlatformEnvironment> _env; std::shared_ptr<IPlatformEnvironment> _env;
std::unique_ptr<IAudioContext> _audioContext; std::shared_ptr<IAudioContext> _audioContext;
std::unique_ptr<IUiContext> _uiContext; std::shared_ptr<IUiContext> _uiContext;
public: public:
PlainContext() PlainContext()
@ -912,25 +915,31 @@ namespace OpenRCT2
{ {
} }
PlainContext(IPlatformEnvironment * env, IAudioContext * audioContext, IUiContext * uiContext) PlainContext(
: Context(env, audioContext, uiContext) std::shared_ptr<IPlatformEnvironment> env,
std::shared_ptr<IAudioContext> audioContext,
std::shared_ptr<IUiContext> uiContext)
: Context(env, audioContext, uiContext),
_env(env),
_audioContext(audioContext),
_uiContext(uiContext)
{ {
_env = std::unique_ptr<IPlatformEnvironment>(env);
_audioContext = std::unique_ptr<IAudioContext>(audioContext);
_uiContext = std::unique_ptr<IUiContext>(uiContext);
} }
}; };
Context * Context::Instance = nullptr; Context * Context::Instance = nullptr;
IContext * CreateContext() std::unique_ptr<IContext> CreateContext()
{ {
return new PlainContext(); return std::make_unique<PlainContext>();
} }
IContext * CreateContext(IPlatformEnvironment * env, Audio::IAudioContext * audioContext, IUiContext * uiContext) std::unique_ptr<IContext> CreateContext(
std::shared_ptr<IPlatformEnvironment> env,
std::shared_ptr<Audio::IAudioContext> audioContext,
std::shared_ptr<IUiContext> uiContext)
{ {
return new Context(env, audioContext, uiContext); return std::make_unique<Context>(env, audioContext, uiContext);
} }
IContext * GetContext() IContext * GetContext()

View File

@ -18,6 +18,7 @@
#include "common.h" #include "common.h"
#include <memory>
#include <string> #include <string>
interface IObjectManager; interface IObjectManager;
@ -111,8 +112,11 @@ namespace OpenRCT2
virtual std::string GetPathLegacy(sint32 pathId) abstract; virtual std::string GetPathLegacy(sint32 pathId) abstract;
}; };
IContext * CreateContext(); std::unique_ptr<IContext> CreateContext();
IContext * CreateContext(IPlatformEnvironment * env, Audio::IAudioContext * audioContext, Ui::IUiContext * uiContext); std::unique_ptr<IContext> CreateContext(
std::shared_ptr<IPlatformEnvironment> env,
std::shared_ptr<Audio::IAudioContext> audioContext,
std::shared_ptr<Ui::IUiContext> uiContext);
IContext * GetContext(); IContext * GetContext();
} // namespace OpenRCT2 } // namespace OpenRCT2

View File

@ -109,9 +109,9 @@ private:
} }
}; };
IPlatformEnvironment * OpenRCT2::CreatePlatformEnvironment(DIRBASE_VALUES basePaths) std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment(DIRBASE_VALUES basePaths)
{ {
return new PlatformEnvironment(basePaths); return std::make_unique<PlatformEnvironment>(basePaths);
} }
static std::string GetOpenRCT2DirectoryName() static std::string GetOpenRCT2DirectoryName()
@ -123,7 +123,7 @@ static std::string GetOpenRCT2DirectoryName()
#endif #endif
} }
IPlatformEnvironment * OpenRCT2::CreatePlatformEnvironment() std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment()
{ {
auto subDirectory = GetOpenRCT2DirectoryName(); auto subDirectory = GetOpenRCT2DirectoryName();

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include "common.h" #include "common.h"
@ -82,6 +83,7 @@ namespace OpenRCT2
virtual void SetBasePath(DIRBASE base, const std::string &path) abstract; virtual void SetBasePath(DIRBASE base, const std::string &path) abstract;
}; };
IPlatformEnvironment * CreatePlatformEnvironment(DIRBASE_VALUES basePaths); std::unique_ptr<IPlatformEnvironment> CreatePlatformEnvironment(DIRBASE_VALUES basePaths);
IPlatformEnvironment * CreatePlatformEnvironment(); std::unique_ptr<IPlatformEnvironment> CreatePlatformEnvironment();
} // namespace OpenRCT2 } // namespace OpenRCT2

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "../common.h" #include "../common.h"
@ -59,5 +60,6 @@ namespace OpenRCT2::Audio
virtual void StopVehicleSounds() abstract; virtual void StopVehicleSounds() abstract;
}; };
IAudioContext * CreateDummyAudioContext(); std::unique_ptr<IAudioContext> CreateDummyAudioContext();
} // namespace OpenRCT2::Audio } // namespace OpenRCT2::Audio

View File

@ -45,8 +45,8 @@ namespace OpenRCT2::Audio
void StopVehicleSounds() override { } void StopVehicleSounds() override { }
}; };
IAudioContext * CreateDummyAudioContext() std::unique_ptr<IAudioContext> CreateDummyAudioContext()
{ {
return new DummyAudioContext(); return std::make_unique<DummyAudioContext>();
} }
} // namespace OpenRCT2::Audio } // namespace OpenRCT2::Audio

View File

@ -399,7 +399,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer
auto context = std::unique_ptr<OpenRCT2::IContext>(OpenRCT2::CreateContext()); auto context = std::unique_ptr<OpenRCT2::IContext>(OpenRCT2::CreateContext());
auto env = context->GetPlatformEnvironment(); auto env = context->GetPlatformEnvironment();
auto objectRepository = std::unique_ptr<IObjectRepository>(CreateObjectRepository(env)); auto objectRepository = CreateObjectRepository(env);
// HACK: set gCurrentLanguage otherwise it be wrong for the index file // HACK: set gCurrentLanguage otherwise it be wrong for the index file
gCurrentLanguage = gConfigGeneral.language; gCurrentLanguage = gConfigGeneral.language;

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <stdarg.h> #include <stdarg.h>
#include <stdbool.h> #include <stdbool.h>
@ -52,6 +53,15 @@ namespace Guard
va_end(args); va_end(args);
} }
template<typename T>
static void ArgumentNotNull(const std::shared_ptr<T>& argument, const char * message = nullptr, ...)
{
va_list args;
va_start(args, message);
Assert_VA(argument != nullptr, message, args);
va_end(args);
}
template<typename T> template<typename T>
static void ArgumentInRange(T argument, T min, T max, const char * message = nullptr, ...) static void ArgumentInRange(T argument, T min, T max, const char * message = nullptr, ...)
{ {

View File

@ -20,6 +20,11 @@
#include "../common.h" #include "../common.h"
#include "../interface/Colour.h" #include "../interface/Colour.h"
namespace OpenRCT2
{
interface IPlatformEnvironment;
}
struct rct_g1_element { struct rct_g1_element {
uint8* offset; // 0x00 uint8* offset; // 0x00
sint16 width; // 0x04 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); void gfx_filter_rect(rct_drawpixelinfo *dpi, sint32 left, sint32 top, sint32 right, sint32 bottom, FILTER_PALETTE_ID palette);
// sprite // sprite
bool gfx_load_g1(void * platformEnvironment); bool gfx_load_g1(const OpenRCT2::IPlatformEnvironment& env);
bool gfx_load_g2(); bool gfx_load_g2();
bool gfx_load_csg(); bool gfx_load_csg();
void gfx_unload_g1(); void gfx_unload_g1();

View File

@ -227,14 +227,12 @@ bool gTinyFontAntiAliased = false;
* *
* rct2: 0x00678998 * rct2: 0x00678998
*/ */
bool gfx_load_g1(void * platformEnvironment) bool gfx_load_g1(const IPlatformEnvironment& env)
{ {
auto env = (IPlatformEnvironment *)platformEnvironment;
log_verbose("gfx_load_g1(...)"); log_verbose("gfx_load_g1(...)");
try 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); auto fs = FileStream(path, FILE_MODE_OPEN);
_g1.header = fs.ReadValue<rct_g1_header>(); _g1.header = fs.ReadValue<rct_g1_header>();

View File

@ -448,7 +448,6 @@ sint32 cmdline_for_screenshot(const char * * argv, sint32 argc, ScreenshotOption
{ {
std::printf("%s\n", e.what()); std::printf("%s\n", e.what());
drawing_engine_dispose(); drawing_engine_dispose();
delete context;
return -1; 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."); std::printf("Weather can only be set to an integer value from 1 till 6.");
drawing_engine_dispose(); drawing_engine_dispose();
delete context;
return -1; return -1;
} }
@ -583,6 +581,5 @@ sint32 cmdline_for_screenshot(const char * * argv, sint32 argc, ScreenshotOption
free(dpi.bits); free(dpi.bits);
drawing_engine_dispose(); drawing_engine_dispose();
} }
delete context;
return 1; return 1;
} }

View File

@ -142,7 +142,7 @@ Network::~Network()
CloseConnection(); CloseConnection();
} }
void Network::SetEnvironment(IPlatformEnvironment * env) void Network::SetEnvironment(std::shared_ptr<IPlatformEnvironment> env)
{ {
_env = env; _env = env;
} }
@ -2545,9 +2545,9 @@ void Network::Client_Handle_GAMEINFO(NetworkConnection& connection, NetworkPacke
network_chat_show_server_greeting(); network_chat_show_server_greeting();
} }
void network_set_env(void * env) void network_set_env(std::shared_ptr<IPlatformEnvironment> env)
{ {
gNetwork.SetEnvironment((IPlatformEnvironment *)env); gNetwork.SetEnvironment(env);
} }
void network_close() 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_chat(const char* text) {}
void network_send_password(const char* password) {} void network_send_password(const char* password) {}
void network_close() {} void network_close() {}
void network_set_env(void * env) {} void network_set_env(std::shared_ptr<OpenRCT2::IPlatformEnvironment>) {}
void network_shutdown_client() {} void network_shutdown_client() {}
void network_set_password(const char* password) {} void network_set_password(const char* password) {}
uint8 network_get_current_player_id() { return 0; } uint8 network_get_current_player_id() { return 0; }

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
enum { enum {
@ -49,12 +50,16 @@ struct GameAction;
struct rct_peep; struct rct_peep;
struct LocationXYZ16; struct LocationXYZ16;
namespace OpenRCT2
{
interface IPlatformEnvironment;
}
#ifndef DISABLE_NETWORK #ifndef DISABLE_NETWORK
#include <array> #include <array>
#include <list> #include <list>
#include <set> #include <set>
#include <memory>
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <fstream> #include <fstream>
@ -79,17 +84,12 @@ enum {
struct ObjectRepositoryItem; struct ObjectRepositoryItem;
namespace OpenRCT2
{
interface IPlatformEnvironment;
}
class Network class Network
{ {
public: public:
Network(); Network();
~Network(); ~Network();
void SetEnvironment(OpenRCT2::IPlatformEnvironment * env); void SetEnvironment(std::shared_ptr<OpenRCT2::IPlatformEnvironment> env);
bool Init(); bool Init();
void Close(); void Close();
bool BeginClient(const char* host, uint16 port); bool BeginClient(const char* host, uint16 port);
@ -259,7 +259,7 @@ private:
std::string _chatLogFilenameFormat = "%Y%m%d-%H%M%S.txt"; std::string _chatLogFilenameFormat = "%Y%m%d-%H%M%S.txt";
std::string _serverLogPath; std::string _serverLogPath;
std::string _serverLogFilenameFormat = "%Y%m%d-%H%M%S.txt"; std::string _serverLogFilenameFormat = "%Y%m%d-%H%M%S.txt";
OpenRCT2::IPlatformEnvironment * _env = nullptr; std::shared_ptr<OpenRCT2::IPlatformEnvironment> _env;
void UpdateServer(); void UpdateServer();
void UpdateClient(); void UpdateClient();
@ -301,7 +301,7 @@ private:
#endif /* DISABLE_NETWORK */ #endif /* DISABLE_NETWORK */
void network_set_env(void * env); void network_set_env(std::shared_ptr<OpenRCT2::IPlatformEnvironment> env);
void network_close(); void network_close();
void network_shutdown_client(); void network_shutdown_client();
sint32 network_begin_client(const char *host, sint32 port); sint32 network_begin_client(const char *host, sint32 port);

View File

@ -86,15 +86,15 @@ private:
IObjectRepository& _objectRepository; IObjectRepository& _objectRepository;
public: public:
explicit ObjectFileIndex(IObjectRepository& objectRepository, IPlatformEnvironment * env) : explicit ObjectFileIndex(IObjectRepository& objectRepository, const IPlatformEnvironment& env) :
FileIndex("object index", FileIndex("object index",
MAGIC_NUMBER, MAGIC_NUMBER,
VERSION, VERSION,
env->GetFilePath(PATHID::CACHE_OBJECTS), env.GetFilePath(PATHID::CACHE_OBJECTS),
std::string(PATTERN), std::string(PATTERN),
std::vector<std::string>({ std::vector<std::string>({
env->GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::OBJECT), env.GetDirectoryPath(DIRBASE::OPENRCT2, DIRID::OBJECT),
env->GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT) })), env.GetDirectoryPath(DIRBASE::USER, DIRID::OBJECT) })),
_objectRepository(objectRepository) _objectRepository(objectRepository)
{ {
} }
@ -208,15 +208,15 @@ private:
class ObjectRepository final : public IObjectRepository class ObjectRepository final : public IObjectRepository
{ {
IPlatformEnvironment * const _env = nullptr; std::shared_ptr<IPlatformEnvironment> const _env;
ObjectFileIndex const _fileIndex; ObjectFileIndex const _fileIndex;
std::vector<ObjectRepositoryItem> _items; std::vector<ObjectRepositoryItem> _items;
ObjectEntryMap _itemMap; ObjectEntryMap _itemMap;
public: public:
explicit ObjectRepository(IPlatformEnvironment * env) explicit ObjectRepository(std::shared_ptr<IPlatformEnvironment> env)
: _env(env), : _env(env),
_fileIndex(*this, env) _fileIndex(*this, *env)
{ {
} }
@ -637,7 +637,7 @@ private:
} }
}; };
IObjectRepository * CreateObjectRepository(IPlatformEnvironment * env) IObjectRepository * CreateObjectRepository(std::shared_ptr<IPlatformEnvironment> env)
{ {
return new ObjectRepository(env); return new ObjectRepository(env);
} }

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include <vector> #include <vector>
#include "../common.h" #include "../common.h"
#include "../object/Object.h" #include "../object/Object.h"
@ -77,7 +78,7 @@ interface IObjectRepository
virtual void WritePackedObjects(IStream * stream, std::vector<const ObjectRepositoryItem *> &objects) abstract; virtual void WritePackedObjects(IStream * stream, std::vector<const ObjectRepositoryItem *> &objects) abstract;
}; };
IObjectRepository * CreateObjectRepository(OpenRCT2::IPlatformEnvironment * env); IObjectRepository * CreateObjectRepository(std::shared_ptr<OpenRCT2::IPlatformEnvironment> env);
bool IsObjectCustom(const ObjectRepositoryItem * object); bool IsObjectCustom(const ObjectRepositoryItem * object);

View File

@ -66,16 +66,16 @@ private:
static constexpr auto PATTERN = "*.td4;*.td6"; static constexpr auto PATTERN = "*.td4;*.td6";
public: public:
explicit TrackDesignFileIndex(IPlatformEnvironment * env) : explicit TrackDesignFileIndex(const IPlatformEnvironment &env) :
FileIndex("track design index", FileIndex("track design index",
MAGIC_NUMBER, MAGIC_NUMBER,
VERSION, VERSION,
env->GetFilePath(PATHID::CACHE_TRACKS), env.GetFilePath(PATHID::CACHE_TRACKS),
std::string(PATTERN), std::string(PATTERN),
std::vector<std::string>({ std::vector<std::string>({
env->GetDirectoryPath(DIRBASE::RCT1, DIRID::TRACK), env.GetDirectoryPath(DIRBASE::RCT1, DIRID::TRACK),
env->GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK), env.GetDirectoryPath(DIRBASE::RCT2, DIRID::TRACK),
env->GetDirectoryPath(DIRBASE::USER, DIRID::TRACK) })) env.GetDirectoryPath(DIRBASE::USER, DIRID::TRACK) }))
{ {
} }
@ -137,14 +137,14 @@ private:
class TrackDesignRepository final : public ITrackDesignRepository class TrackDesignRepository final : public ITrackDesignRepository
{ {
private: private:
IPlatformEnvironment * const _env; std::shared_ptr<IPlatformEnvironment> const _env;
TrackDesignFileIndex const _fileIndex; TrackDesignFileIndex const _fileIndex;
std::vector<TrackRepositoryItem> _items; std::vector<TrackRepositoryItem> _items;
public: public:
explicit TrackDesignRepository(IPlatformEnvironment * env) explicit TrackDesignRepository(std::shared_ptr<IPlatformEnvironment> env)
: _env(env), : _env(env),
_fileIndex(env) _fileIndex(*env)
{ {
Guard::ArgumentNotNull(env); Guard::ArgumentNotNull(env);
} }
@ -396,7 +396,7 @@ private:
} }
}; };
ITrackDesignRepository * CreateTrackDesignRepository(IPlatformEnvironment * env) ITrackDesignRepository * CreateTrackDesignRepository(std::shared_ptr<IPlatformEnvironment> env)
{ {
return new TrackDesignRepository(env); return new TrackDesignRepository(env);
} }

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include "../common.h" #include "../common.h"
#include "RideGroupManager.h" #include "RideGroupManager.h"
@ -51,7 +52,7 @@ interface ITrackDesignRepository
virtual std::string Install(const std::string &path) abstract; virtual std::string Install(const std::string &path) abstract;
}; };
ITrackDesignRepository * CreateTrackDesignRepository(OpenRCT2::IPlatformEnvironment * env); ITrackDesignRepository * CreateTrackDesignRepository(std::shared_ptr<OpenRCT2::IPlatformEnvironment> env);
std::string GetNameFromTrackPath(const std::string &path); std::string GetNameFromTrackPath(const std::string &path);
void track_repository_scan(); void track_repository_scan();

View File

@ -128,16 +128,16 @@ private:
static constexpr auto PATTERN = "*.sc4;*.sc6"; static constexpr auto PATTERN = "*.sc4;*.sc6";
public: public:
explicit ScenarioFileIndex(IPlatformEnvironment * env) : explicit ScenarioFileIndex(const IPlatformEnvironment& env) :
FileIndex("scenario index", FileIndex("scenario index",
MAGIC_NUMBER, MAGIC_NUMBER,
VERSION, VERSION,
env->GetFilePath(PATHID::CACHE_SCENARIOS), env.GetFilePath(PATHID::CACHE_SCENARIOS),
std::string(PATTERN), std::string(PATTERN),
std::vector<std::string>({ std::vector<std::string>({
env->GetDirectoryPath(DIRBASE::RCT1, DIRID::SCENARIO), env.GetDirectoryPath(DIRBASE::RCT1, DIRID::SCENARIO),
env->GetDirectoryPath(DIRBASE::RCT2, DIRID::SCENARIO), env.GetDirectoryPath(DIRBASE::RCT2, DIRID::SCENARIO),
env->GetDirectoryPath(DIRBASE::USER, DIRID::SCENARIO) })) env.GetDirectoryPath(DIRBASE::USER, DIRID::SCENARIO) }))
{ {
} }
@ -322,15 +322,15 @@ class ScenarioRepository final : public IScenarioRepository
private: private:
static constexpr uint32 HighscoreFileVersion = 1; static constexpr uint32 HighscoreFileVersion = 1;
IPlatformEnvironment * const _env; std::shared_ptr<IPlatformEnvironment> const _env;
ScenarioFileIndex const _fileIndex; ScenarioFileIndex const _fileIndex;
std::vector<scenario_index_entry> _scenarios; std::vector<scenario_index_entry> _scenarios;
std::vector<scenario_highscore_entry*> _highscores; std::vector<scenario_highscore_entry*> _highscores;
public: public:
explicit ScenarioRepository(IPlatformEnvironment * env) explicit ScenarioRepository(std::shared_ptr<IPlatformEnvironment> env)
: _env(env), : _env(env),
_fileIndex(env) _fileIndex(*env)
{ {
} }
@ -729,7 +729,7 @@ private:
static ScenarioRepository * _scenarioRepository; static ScenarioRepository * _scenarioRepository;
IScenarioRepository * CreateScenarioRepository(IPlatformEnvironment * env) IScenarioRepository * CreateScenarioRepository(std::shared_ptr<IPlatformEnvironment> env)
{ {
_scenarioRepository = new ScenarioRepository(env); _scenarioRepository = new ScenarioRepository(env);
return _scenarioRepository; return _scenarioRepository;

View File

@ -16,6 +16,7 @@
#pragma once #pragma once
#include <memory>
#include "../common.h" #include "../common.h"
struct rct_object_entry; struct rct_object_entry;
@ -77,7 +78,7 @@ interface IScenarioRepository
virtual bool TryRecordHighscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) abstract; virtual bool TryRecordHighscore(const utf8 * scenarioFileName, money32 companyValue, const utf8 * name) abstract;
}; };
IScenarioRepository * CreateScenarioRepository(OpenRCT2::IPlatformEnvironment * env); IScenarioRepository * CreateScenarioRepository(std::shared_ptr<OpenRCT2::IPlatformEnvironment> env);
IScenarioRepository * GetScenarioRepository(); IScenarioRepository * GetScenarioRepository();
void scenario_repository_scan(); void scenario_repository_scan();

View File

@ -92,8 +92,8 @@ namespace OpenRCT2::Ui
~DummyUiContext() { delete _windowManager; } ~DummyUiContext() { delete _windowManager; }
}; };
IUiContext * CreateDummyUiContext() std::shared_ptr<IUiContext> CreateDummyUiContext()
{ {
return new DummyUiContext(); return std::make_unique<DummyUiContext>();
} }
} // namespace OpenRCT2::Ui } // namespace OpenRCT2::Ui

View File

@ -149,6 +149,6 @@ namespace OpenRCT2
virtual bool SetClipboardText(const utf8* target) abstract; virtual bool SetClipboardText(const utf8* target) abstract;
}; };
IUiContext * CreateDummyUiContext(); std::shared_ptr<IUiContext> CreateDummyUiContext();
} // namespace Ui } // namespace Ui
} // namespace OpenRCT2 } // namespace OpenRCT2

View File

@ -46,8 +46,6 @@ TEST(MultiLaunchTest, all)
// Check ride count again // Check ride count again
ASSERT_EQ(gRideCount, 134); ASSERT_EQ(gRideCount, 134);
delete context;
} }
SUCCEED(); SUCCEED();
} }

View File

@ -91,6 +91,4 @@ TEST_F(RideRatings, all)
expI++; expI++;
} }
} }
delete context;
} }

View File

@ -28,18 +28,11 @@ protected:
SUCCEED(); SUCCEED();
} }
static void TearDownTestCase()
{
delete _context;
_context = nullptr;
SUCCEED();
}
private: private:
static IContext * _context; static std::shared_ptr<IContext> _context;
}; };
IContext * TileElementWantsFootpathConnection::_context = nullptr; std::shared_ptr<IContext> TileElementWantsFootpathConnection::_context;
TEST_F(TileElementWantsFootpathConnection, FlatPath) TEST_F(TileElementWantsFootpathConnection, FlatPath)
{ {