Review Changes 12 - unique_ptr for InputBase

This commit is contained in:
Daniel Stuart Baxter 2015-05-31 12:58:47 -05:00
parent 5dbd3c53f7
commit 1be629b345
2 changed files with 7 additions and 10 deletions

View file

@ -13,8 +13,8 @@
namespace InputCommon { namespace InputCommon {
// Set to nullptr until initialized by a backend // User input system
InputBase* g_user_input = nullptr; std::unique_ptr<InputBase> g_user_input;
// Event id for CoreTiming // Event id for CoreTiming
static int input_event; static int input_event;
@ -26,7 +26,7 @@ static u64 frame_ticks;
static void InputCallback(u64 userdata, int cycles_late) { static void InputCallback(u64 userdata, int cycles_late) {
// Call user input plugin Poll() // 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 // Reschedule recurrent event
CoreTiming::ScheduleEvent(frame_ticks - cycles_late, input_event); CoreTiming::ScheduleEvent(frame_ticks - cycles_late, input_event);
@ -45,7 +45,7 @@ void Init(InputBackends backend) {
#ifdef HAS_SDL #ifdef HAS_SDL
// SDL2 backend selected // SDL2 backend selected
case InputBackends::SDL2: case InputBackends::SDL2:
g_user_input = new SDLController(); g_user_input.reset(new SDLController);
break; break;
#endif #endif
@ -60,7 +60,7 @@ void Init(InputBackends backend) {
} }
// Shutdown immediately if backend failed to initialize // 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(); Shutdown();
return; return;
} }
@ -73,10 +73,7 @@ void Init(InputBackends backend) {
} }
void Shutdown() { void Shutdown() {
if (g_user_input != nullptr) { if (g_user_input) g_user_input.release();
delete g_user_input;
g_user_input = nullptr;
}
} }
} // namespace } // namespace

View file

@ -55,6 +55,6 @@ void Init(InputBackends backend);
void Shutdown(); void Shutdown();
/// InputCommon 'plugin' /// InputCommon 'plugin'
extern InputBase* g_user_input; extern std::unique_ptr<InputBase> g_user_input;
} // namespace } // namespace