Skip to content

Commit

Permalink
Use shared_ptr for long-life objects
Browse files Browse the repository at this point in the history
Use unique_ptr and shared_ptr for IContext, IPlatformEnvironment, IUiContext, and IAudioContext.
  • Loading branch information
IntelOrca committed May 11, 2018
1 parent c7b40ce commit 7dc170e
Show file tree
Hide file tree
Showing 33 changed files with 144 additions and 120 deletions.
3 changes: 2 additions & 1 deletion .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceRoot}"
"${workspaceRoot}",
"${workspaceRoot}/src"
],
"defines": [],
"intelliSenseMode": "clang-x64",
Expand Down
1 change: 0 additions & 1 deletion src/openrct2-cli/Cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
17 changes: 9 additions & 8 deletions src/openrct2-ui/Ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ using namespace OpenRCT2;
using namespace OpenRCT2::Audio;
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.
*/
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/openrct2-ui/UiContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class UiContext final : public IUiContext
public:
InGameConsole& GetInGameConsole() { return _inGameConsole; }

explicit UiContext(IPlatformEnvironment * env)
explicit UiContext(std::shared_ptr<IPlatformEnvironment> env)
: _platformUiContext(CreatePlatformUiContext()),
_windowManager(CreateWindowManager()),
_keyboardShortcuts(env)
Expand Down Expand Up @@ -814,9 +814,9 @@ class UiContext final : public IUiContext
}
};

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()
Expand Down
3 changes: 2 additions & 1 deletion src/openrct2-ui/UiContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <memory>
#include <string>
#include <openrct2/common.h>

Expand Down Expand Up @@ -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<IUiContext> CreateUiContext(std::shared_ptr<IPlatformEnvironment> env);
IPlatformUiContext * CreatePlatformUiContext();

InGameConsole& GetInGameConsole();
Expand Down
4 changes: 2 additions & 2 deletions src/openrct2-ui/audio/AudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ namespace OpenRCT2::Audio
void StopVehicleSounds() override { }
};

IAudioContext * CreateAudioContext()
std::unique_ptr<IAudioContext> CreateAudioContext()
{
return new AudioContext();
return std::make_unique<AudioContext>();
}
} // namespace OpenRCT2::Audio
4 changes: 3 additions & 1 deletion src/openrct2-ui/audio/AudioContext.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

#pragma once

#include <memory>
#include <string>
#include <openrct2/common.h>
#include <openrct2/audio/AudioChannel.h>
Expand Down Expand Up @@ -69,5 +70,6 @@ namespace OpenRCT2::Audio
IAudioMixer * Create();
}

IAudioContext * CreateAudioContext();
std::unique_ptr<IAudioContext> CreateAudioContext();

} // namespace OpenRCT2::Audio
2 changes: 1 addition & 1 deletion src/openrct2-ui/input/KeyboardShortcuts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<IPlatformEnvironment> env)
: _env(env)
{
_instance = this;
Expand Down
5 changes: 3 additions & 2 deletions src/openrct2-ui/input/KeyboardShortcuts.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <memory>
#include <openrct2/common.h>

#define SHIFT 0x100
Expand Down Expand Up @@ -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<IPlatformEnvironment> const _env;
uint16 _keys[SHORTCUT_COUNT];

public:
KeyboardShortcuts(IPlatformEnvironment * env);
KeyboardShortcuts(std::shared_ptr<IPlatformEnvironment> env);

void Reset();
bool Load();
Expand Down
49 changes: 29 additions & 20 deletions src/openrct2/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ namespace OpenRCT2
{
private:
// Dependencies
IPlatformEnvironment * const _env = nullptr;
IAudioContext * const _audioContext = nullptr;
IUiContext * const _uiContext = nullptr;
std::shared_ptr<IPlatformEnvironment> const _env;
std::shared_ptr<IAudioContext> const _audioContext;
std::shared_ptr<IUiContext> const _uiContext;

// Services
IObjectRepository * _objectRepository = nullptr;
Expand Down Expand Up @@ -116,7 +116,10 @@ namespace OpenRCT2
static Context * Instance;

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),
_audioContext(audioContext),
_uiContext(uiContext)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -548,7 +551,7 @@ namespace OpenRCT2

bool LoadBaseGraphics()
{
if (!gfx_load_g1(_env))
if (!gfx_load_g1(*_env))
{
return false;
}
Expand Down Expand Up @@ -902,35 +905,41 @@ namespace OpenRCT2

class PlainContext final : public Context
{
std::unique_ptr<IPlatformEnvironment> _env;
std::unique_ptr<IAudioContext> _audioContext;
std::unique_ptr<IUiContext> _uiContext;
std::shared_ptr<IPlatformEnvironment> _env;
std::shared_ptr<IAudioContext> _audioContext;
std::shared_ptr<IUiContext> _uiContext;

public:
PlainContext()
: PlainContext(CreatePlatformEnvironment(), CreateDummyAudioContext(), CreateDummyUiContext())
{
}

PlainContext(IPlatformEnvironment * env, IAudioContext * audioContext, IUiContext * uiContext)
: Context(env, audioContext, uiContext)
PlainContext(
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;

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()
Expand Down
8 changes: 6 additions & 2 deletions src/openrct2/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "common.h"

#include <memory>
#include <string>

interface IObjectManager;
Expand Down Expand Up @@ -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<IContext> CreateContext();
std::unique_ptr<IContext> CreateContext(
std::shared_ptr<IPlatformEnvironment> env,
std::shared_ptr<Audio::IAudioContext> audioContext,
std::shared_ptr<Ui::IUiContext> uiContext);
IContext * GetContext();
} // namespace OpenRCT2

Expand Down
6 changes: 3 additions & 3 deletions src/openrct2/PlatformEnvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ class PlatformEnvironment final : public IPlatformEnvironment
}
};

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()
Expand All @@ -123,7 +123,7 @@ static std::string GetOpenRCT2DirectoryName()
#endif
}

IPlatformEnvironment * OpenRCT2::CreatePlatformEnvironment()
std::unique_ptr<IPlatformEnvironment> OpenRCT2::CreatePlatformEnvironment()
{
auto subDirectory = GetOpenRCT2DirectoryName();

Expand Down
6 changes: 4 additions & 2 deletions src/openrct2/PlatformEnvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <memory>
#include <string>
#include "common.h"

Expand Down Expand Up @@ -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<IPlatformEnvironment> CreatePlatformEnvironment(DIRBASE_VALUES basePaths);
std::unique_ptr<IPlatformEnvironment> CreatePlatformEnvironment();

} // namespace OpenRCT2
4 changes: 3 additions & 1 deletion src/openrct2/audio/AudioContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <memory>
#include <string>
#include <vector>
#include "../common.h"
Expand Down Expand Up @@ -59,5 +60,6 @@ namespace OpenRCT2::Audio
virtual void StopVehicleSounds() abstract;
};

IAudioContext * CreateDummyAudioContext();
std::unique_ptr<IAudioContext> CreateDummyAudioContext();

} // namespace OpenRCT2::Audio
4 changes: 2 additions & 2 deletions src/openrct2/audio/DummyAudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ namespace OpenRCT2::Audio
void StopVehicleSounds() override { }
};

IAudioContext * CreateDummyAudioContext()
std::unique_ptr<IAudioContext> CreateDummyAudioContext()
{
return new DummyAudioContext();
return std::make_unique<DummyAudioContext>();
}
} // namespace OpenRCT2::Audio
2 changes: 1 addition & 1 deletion src/openrct2/cmdline/RootCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ static exitcode_t HandleCommandScanObjects([[maybe_unused]] CommandLineArgEnumer

auto context = std::unique_ptr<OpenRCT2::IContext>(OpenRCT2::CreateContext());
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
gCurrentLanguage = gConfigGeneral.language;
Expand Down
10 changes: 10 additions & 0 deletions src/openrct2/core/Guard.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#pragma once

#include <memory>
#include <stdarg.h>
#include <stdbool.h>

Expand Down Expand Up @@ -52,6 +53,15 @@ namespace Guard
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>
static void ArgumentInRange(T argument, T min, T max, const char * message = nullptr, ...)
{
Expand Down
Loading

0 comments on commit 7dc170e

Please sign in to comment.