Use smart pointer for joystick in PhysicalJoystickHandler.

This commit is contained in:
Stephen Anthony 2018-03-17 19:44:47 -02:30
parent be23ad7ca7
commit 7eab28b2ef
5 changed files with 25 additions and 31 deletions

View File

@ -127,7 +127,7 @@ void EventHandlerSDL2::pollEvent()
case SDL_JOYDEVICEADDED:
{
addPhysicalJoystick(new JoystickSDL2(myEvent.jdevice.which));
addPhysicalJoystick(make_shared<JoystickSDL2>(myEvent.jdevice.which));
break; // SDL_JOYDEVICEADDED
}
case SDL_JOYDEVICEREMOVED:

View File

@ -49,13 +49,6 @@ PhysicalJoystickHandler::PhysicalJoystickHandler(
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PhysicalJoystickHandler::~PhysicalJoystickHandler()
{
for(const auto& i: myDatabase)
delete i.second.joy;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::printDatabase() const
{
@ -72,7 +65,7 @@ void PhysicalJoystickHandler::printDatabase() const
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int PhysicalJoystickHandler::add(PhysicalJoystick* stick)
int PhysicalJoystickHandler::add(PhysicalJoystickPtr stick)
{
// Skip if we couldn't open it for any reason
if(stick->ID < 0)
@ -167,7 +160,7 @@ bool PhysicalJoystickHandler::remove(int id)
// So we use the 'active' joystick list to access them
try
{
PhysicalJoystick* stick = mySticks.at(id);
PhysicalJoystickPtr stick = mySticks.at(id);
auto it = myDatabase.find(stick->name);
if(it != myDatabase.end() && it->second.joy == stick)
@ -179,7 +172,7 @@ bool PhysicalJoystickHandler::remove(int id)
// Remove joystick, but remember mapping
it->second.mapping = stick->getMap();
delete it->second.joy; it->second.joy = nullptr;
it->second.joy = nullptr;
mySticks.erase(id);
return true;
@ -389,7 +382,7 @@ string PhysicalJoystickHandler::getMappingDesc(Event::Type event, EventMode mode
for(const auto& s: mySticks)
{
uInt32 stick = s.first;
const PhysicalJoystick* j = s.second;
const PhysicalJoystickPtr j = s.second;
if(!j) continue;
// Joystick button mapping/labeling
@ -455,7 +448,7 @@ string PhysicalJoystickHandler::getMappingDesc(Event::Type event, EventMode mode
bool PhysicalJoystickHandler::addAxisMapping(Event::Type event, EventMode mode,
int stick, int axis, int value)
{
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
if(j)
{
if(axis >= 0 && axis < j->numAxes && event < Event::LastType)
@ -484,7 +477,7 @@ bool PhysicalJoystickHandler::addAxisMapping(Event::Type event, EventMode mode,
bool PhysicalJoystickHandler::addBtnMapping(Event::Type event, EventMode mode,
int stick, int button)
{
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
if(j)
{
if(button >= 0 && button < j->numButtons && event < Event::LastType)
@ -500,7 +493,7 @@ bool PhysicalJoystickHandler::addBtnMapping(Event::Type event, EventMode mode,
bool PhysicalJoystickHandler::addHatMapping(Event::Type event, EventMode mode,
int stick, int hat, JoyHat value)
{
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
if(j)
{
if(hat >= 0 && hat < j->numHats && event < Event::LastType &&
@ -516,7 +509,7 @@ bool PhysicalJoystickHandler::addHatMapping(Event::Type event, EventMode mode,
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::handleAxisEvent(int stick, int axis, int value)
{
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
if(!j) return;
// Stelladaptors handle axis differently than regular joysticks
@ -623,7 +616,7 @@ void PhysicalJoystickHandler::handleAxisEvent(int stick, int axis, int value)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PhysicalJoystickHandler::handleBtnEvent(int stick, int button, uInt8 state)
{
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
if(!j) return;
// Stelladaptors handle buttons differently than regular joysticks
@ -688,7 +681,7 @@ void PhysicalJoystickHandler::handleHatEvent(int stick, int hat, int value)
// when we get a diagonal hat event
if(myHandler.state() == EventHandlerState::EMULATION)
{
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
if(!j) return;
myHandler.handleEvent(j->hatTable[hat][int(JoyHat::UP)][kEmulationMode],

View File

@ -29,6 +29,8 @@ class Event;
#include "PhysicalJoystick.hxx"
#include "Variant.hxx"
using PhysicalJoystickPtr = shared_ptr<PhysicalJoystick>;
/**
This class handles all physical joystick-related operations in Stella.
@ -46,11 +48,11 @@ class PhysicalJoystickHandler
private:
struct StickInfo
{
StickInfo(const string& map = EmptyString, PhysicalJoystick* stick = nullptr)
StickInfo(const string& map = EmptyString, PhysicalJoystickPtr stick = nullptr)
: mapping(map), joy(stick) {}
string mapping;
PhysicalJoystick* joy;
PhysicalJoystickPtr joy;
friend ostream& operator<<(ostream& os, const StickInfo& si) {
os << " joy: " << si.joy << endl << " map: " << si.mapping;
@ -59,14 +61,10 @@ class PhysicalJoystickHandler
};
public:
using StickDatabase = std::map<string,StickInfo>;
using StickList = std::map<int, PhysicalJoystick*>;
PhysicalJoystickHandler(OSystem& system, EventHandler& handler, Event& event);
~PhysicalJoystickHandler();
/** Return stick ID on success, -1 on failure. */
int add(PhysicalJoystick* stick);
int add(PhysicalJoystickPtr stick);
bool remove(int id);
bool remove(const string& name);
void mapStelladaptors(const string& saport);
@ -86,21 +84,24 @@ class PhysicalJoystickHandler
void handleHatEvent(int stick, int hat, int value);
Event::Type eventForAxis(int stick, int axis, int value, EventMode mode) const {
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
return j ? j->axisTable[axis][(value > 0)][mode] : Event::NoType;
}
Event::Type eventForButton(int stick, int button, EventMode mode) const {
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
return j ? j->btnTable[button][mode] : Event::NoType;
}
Event::Type eventForHat(int stick, int hat, JoyHat value, EventMode mode) const {
const PhysicalJoystick* j = joy(stick);
const PhysicalJoystickPtr j = joy(stick);
return j ? j->hatTable[hat][int(value)][mode] : Event::NoType;
}
VariantList database() const;
private:
using StickDatabase = std::map<string,StickInfo>;
using StickList = std::map<int, PhysicalJoystickPtr>;
OSystem& myOSystem;
EventHandler& myHandler;
Event& myEvent;
@ -113,7 +114,7 @@ class PhysicalJoystickHandler
// Get joystick corresponding to given id (or nullptr if it doesn't exist)
// Make this inline so it's as fast as possible
const PhysicalJoystick* joy(int id) const {
const PhysicalJoystickPtr joy(int id) const {
const auto& i = mySticks.find(id);
return i != mySticks.cend() ? i->second : nullptr;
}

View File

@ -135,7 +135,7 @@ void EventHandler::reset(EventHandlerState state)
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void EventHandler::addPhysicalJoystick(PhysicalJoystick* joy)
void EventHandler::addPhysicalJoystick(PhysicalJoystickPtr joy)
{
#ifdef JOYSTICK_SUPPORT
int ID = myPJoyHandler->add(joy);

View File

@ -366,7 +366,7 @@ class EventHandler
/**
Add the given joystick to the list of physical joysticks available to the handler.
*/
void addPhysicalJoystick(PhysicalJoystick* stick);
void addPhysicalJoystick(PhysicalJoystickPtr stick);
/**
Remove physical joystick at the current index.