ControllerInterface: Switch to std::shared_ptr

Small cleanup by using std::shared_ptr and getting rid of
ciface.Devices() which just returned the m_devices (which defeats the
point of making m_devices protected).

Incidentally, this should make the code safer when we have
different threads accessing devices in the future (for hotplug?).

A lot of code use Device references directly so there is
no easy way to remove FindDevice() and make those unique_ptrs.
This commit is contained in:
Léo Lam 2016-06-25 21:46:39 +02:00
parent afa202738e
commit 8678133e87
18 changed files with 80 additions and 60 deletions

View file

@ -2,8 +2,10 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include <mutex>
#include "Common/Thread.h"
#include "InputCommon/ControllerInterface/ControllerInterface.h"
#ifdef CIFACE_USE_XINPUT
#include "InputCommon/ControllerInterface/XInput/XInput.h"
@ -106,14 +108,11 @@ void ControllerInterface::Shutdown()
std::lock_guard<std::mutex> lk(m_devices_mutex);
for (ciface::Core::Device* d : m_devices)
for (const auto& d : m_devices)
{
// Set outputs to ZERO before destroying device
for (ciface::Core::Device::Output* o : d->Outputs())
o->SetState(0);
// Delete device
delete d;
}
m_devices.clear();
@ -141,10 +140,10 @@ void ControllerInterface::Shutdown()
m_is_init = false;
}
void ControllerInterface::AddDevice(ciface::Core::Device* device)
void ControllerInterface::AddDevice(std::shared_ptr<ciface::Core::Device> device)
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
m_devices.push_back(device);
m_devices.emplace_back(std::move(device));
}
//
@ -155,7 +154,7 @@ void ControllerInterface::AddDevice(ciface::Core::Device* device)
void ControllerInterface::UpdateInput()
{
std::lock_guard<std::mutex> lk(m_devices_mutex);
for (ciface::Core::Device* d : m_devices)
for (const auto& d : m_devices)
d->UpdateInput();
}