From 1be629b345ecf0cfae860467bf02bef750f3d3ed Mon Sep 17 00:00:00 2001 From: Daniel Stuart Baxter Date: Sun, 31 May 2015 12:58:47 -0500 Subject: [PATCH] Review Changes 12 - unique_ptr for InputBase --- src/input_common/input_common.cpp | 15 ++++++--------- src/input_common/input_common.h | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/src/input_common/input_common.cpp b/src/input_common/input_common.cpp index 263c6919e..d5096d8b2 100644 --- a/src/input_common/input_common.cpp +++ b/src/input_common/input_common.cpp @@ -13,8 +13,8 @@ namespace InputCommon { -// Set to nullptr until initialized by a backend -InputBase* g_user_input = nullptr; +// User input system +std::unique_ptr g_user_input; // Event id for CoreTiming static int input_event; @@ -26,7 +26,7 @@ static u64 frame_ticks; static void InputCallback(u64 userdata, int cycles_late) { // Call user input plugin Poll() - if (g_user_input != nullptr) g_user_input->Poll(); + if (g_user_input) g_user_input->Poll(); // Reschedule recurrent event CoreTiming::ScheduleEvent(frame_ticks - cycles_late, input_event); @@ -45,7 +45,7 @@ void Init(InputBackends backend) { #ifdef HAS_SDL // SDL2 backend selected case InputBackends::SDL2: - g_user_input = new SDLController(); + g_user_input.reset(new SDLController); break; #endif @@ -60,7 +60,7 @@ void Init(InputBackends backend) { } // Shutdown immediately if backend failed to initialize - if (g_user_input != nullptr && !g_user_input->activated) { + if (!g_user_input && !g_user_input->activated) { Shutdown(); return; } @@ -73,10 +73,7 @@ void Init(InputBackends backend) { } void Shutdown() { - if (g_user_input != nullptr) { - delete g_user_input; - g_user_input = nullptr; - } + if (g_user_input) g_user_input.release(); } } // namespace diff --git a/src/input_common/input_common.h b/src/input_common/input_common.h index 4952ea9b5..230b3c8ec 100644 --- a/src/input_common/input_common.h +++ b/src/input_common/input_common.h @@ -55,6 +55,6 @@ void Init(InputBackends backend); void Shutdown(); /// InputCommon 'plugin' -extern InputBase* g_user_input; +extern std::unique_ptr g_user_input; } // namespace