Switch to C++17 structured bindings for maps in a few places, adding const as well.

This makes the code a little easier to follow.
This commit is contained in:
Stephen Anthony 2020-11-29 15:50:31 -03:30
parent 08c65fea88
commit a4e7a87307
9 changed files with 83 additions and 88 deletions

View File

@ -253,8 +253,8 @@ void CheatManager::saveCheatDatabase()
return; return;
stringstream out; stringstream out;
for(const auto& iter: myCheatMap) for(const auto& [md5, cheat]: myCheatMap)
out << "\"" << iter.first << "\" " << "\"" << iter.second << "\"" << endl; out << "\"" << md5 << "\" " << "\"" << cheat << "\"" << endl;
try { myOSystem.cheatFile().write(out); } try { myOSystem.cheatFile().write(out); }
catch(...) { return; } catch(...) { return; }

View File

@ -162,13 +162,13 @@ string JoyMap::getEventMappingDesc(int stick, const Event::Type event, const Eve
{ {
ostringstream buf; ostringstream buf;
for (auto item : myMap) for (const auto& [_mapping, _event]: myMap)
{ {
if (item.second == event && item.first.mode == mode) if (_event == event && _mapping.mode == mode)
{ {
if (buf.str() != "") if (buf.str() != "")
buf << ", "; buf << ", ";
buf << "J" << stick << getDesc(event, item.first); buf << "J" << stick << getDesc(event, _mapping);
} }
} }
return buf.str(); return buf.str();
@ -179,9 +179,9 @@ JoyMap::JoyMappingArray JoyMap::getEventMapping(const Event::Type event, const E
{ {
JoyMappingArray map; JoyMappingArray map;
for (auto item : myMap) for (const auto& [_mapping, _event]: myMap)
if (item.second == event && item.first.mode == mode) if (_event == event && _mapping.mode == mode)
map.push_back(item.first); map.push_back(_mapping);
return map; return map;
} }
@ -191,23 +191,23 @@ json JoyMap::saveMapping(const EventMode mode) const
{ {
json eventMappings = json::array(); json eventMappings = json::array();
for (auto& item: myMap) { for (const auto& [_mapping, _event]: myMap) {
if (item.first.mode != mode) continue; if (_mapping.mode != mode) continue;
json eventMapping = json::object(); json eventMapping = json::object();
eventMapping["event"] = item.second; eventMapping["event"] = _event;
if (item.first.button != JOY_CTRL_NONE) eventMapping["button"] = item.first.button; if (_mapping.button != JOY_CTRL_NONE) eventMapping["button"] = _mapping.button;
if (item.first.axis != JoyAxis::NONE) { if (_mapping.axis != JoyAxis::NONE) {
eventMapping["axis"] = item.first.axis; eventMapping["axis"] = _mapping.axis;
eventMapping["axisDirection"] = item.first.adir; eventMapping["axisDirection"] = _mapping.adir;
} }
if (item.first.hat != -1) { if (_mapping.hat != -1) {
eventMapping["hat"] = item.first.hat; eventMapping["hat"] = _mapping.hat;
eventMapping["hatDirection"] = item.first.hdir; eventMapping["hatDirection"] = _mapping.hdir;
} }
eventMappings.push_back(eventMapping); eventMappings.push_back(eventMapping);

View File

@ -194,13 +194,13 @@ string KeyMap::getEventMappingDesc(const Event::Type event, const EventMode mode
{ {
ostringstream buf; ostringstream buf;
for (auto item : myMap) for (const auto& [_mapping, _event]: myMap)
{ {
if (item.second == event && item.first.mode == mode) if (_event == event && _mapping.mode == mode)
{ {
if (buf.str() != "") if (buf.str() != "")
buf << ", "; buf << ", ";
buf << getDesc(item.first); buf << getDesc(_mapping);
} }
} }
return buf.str(); return buf.str();
@ -211,9 +211,9 @@ KeyMap::MappingArray KeyMap::getEventMapping(const Event::Type event, const Even
{ {
MappingArray map; MappingArray map;
for (auto item : myMap) for (const auto& [_mapping, _event]: myMap)
if (item.second == event && item.first.mode == mode) if (_event == event && _mapping.mode == mode)
map.push_back(item.first); map.push_back(_mapping);
return map; return map;
} }
@ -223,16 +223,16 @@ json KeyMap::saveMapping(const EventMode mode) const
{ {
json mappings = json::array(); json mappings = json::array();
for (auto item : myMap) { for (const auto& [_mapping, _event]: myMap) {
if (item.first.mode != mode) continue; if (_mapping.mode != mode) continue;
json mapping = json::object(); json mapping = json::object();
mapping["event"] = item.second; mapping["event"] = _event;
mapping["key"] = item.first.key; mapping["key"] = _mapping.key;
if (item.first.mod != StellaMod::KBDM_NONE) if (_mapping.mod != StellaMod::KBDM_NONE)
mapping["mod"] = serializeModkeyMask(item.first.mod); mapping["mod"] = serializeModkeyMask(_mapping.mod);
mappings.push_back(mapping); mappings.push_back(mapping);
} }

View File

@ -122,8 +122,8 @@ int PhysicalJoystickHandler::add(const PhysicalJoystickPtr& stick)
// For non-unique names that already have a database entry, // For non-unique names that already have a database entry,
// we append ' #x', where 'x' increases consecutively // we append ' #x', where 'x' increases consecutively
int count = 0; int count = 0;
for(const auto& i: myDatabase) for(const auto& [_name, _info]: myDatabase)
if(BSPF::startsWithIgnoreCase(i.first, stick->name) && i.second.joy) if(BSPF::startsWithIgnoreCase(_name, stick->name) && _info.joy)
++count; ++count;
if(count > 0) if(count > 0)
@ -223,39 +223,39 @@ void PhysicalJoystickHandler::mapStelladaptors(const string& saport)
saOrder[0] = 2; saOrder[1] = 1; saOrder[0] = 2; saOrder[1] = 1;
} }
for(auto& stick: mySticks) for(auto& [_id, _joyptr]: mySticks)
{ {
// remove previously added emulated ports // remove previously added emulated ports
size_t pos = stick.second->name.find(" (emulates "); size_t pos = _joyptr->name.find(" (emulates ");
if(pos != std::string::npos) if(pos != std::string::npos)
stick.second->name.erase(pos); _joyptr->name.erase(pos);
if(BSPF::startsWithIgnoreCase(stick.second->name, "Stelladaptor")) if(BSPF::startsWithIgnoreCase(_joyptr->name, "Stelladaptor"))
{ {
if(saOrder[saCount] == 1) if(saOrder[saCount] == 1)
{ {
stick.second->name += " (emulates left joystick port)"; _joyptr->name += " (emulates left joystick port)";
stick.second->type = PhysicalJoystick::JT_STELLADAPTOR_LEFT; _joyptr->type = PhysicalJoystick::JT_STELLADAPTOR_LEFT;
} }
else if(saOrder[saCount] == 2) else if(saOrder[saCount] == 2)
{ {
stick.second->name += " (emulates right joystick port)"; _joyptr->name += " (emulates right joystick port)";
stick.second->type = PhysicalJoystick::JT_STELLADAPTOR_RIGHT; _joyptr->type = PhysicalJoystick::JT_STELLADAPTOR_RIGHT;
} }
saCount++; saCount++;
} }
else if(BSPF::startsWithIgnoreCase(stick.second->name, "2600-daptor")) else if(BSPF::startsWithIgnoreCase(_joyptr->name, "2600-daptor"))
{ {
if(saOrder[saCount] == 1) if(saOrder[saCount] == 1)
{ {
stick.second->name += " (emulates left joystick port)"; _joyptr->name += " (emulates left joystick port)";
stick.second->type = PhysicalJoystick::JT_2600DAPTOR_LEFT; _joyptr->type = PhysicalJoystick::JT_2600DAPTOR_LEFT;
} }
else if(saOrder[saCount] == 2) else if(saOrder[saCount] == 2)
{ {
stick.second->name += " (emulates right joystick port)"; _joyptr->name += " (emulates right joystick port)";
stick.second->type = PhysicalJoystick::JT_2600DAPTOR_RIGHT; _joyptr->type = PhysicalJoystick::JT_2600DAPTOR_RIGHT;
} }
saCount++; saCount++;
} }
@ -354,8 +354,8 @@ void PhysicalJoystickHandler::setStickDefaultMapping(int stick, Event::Type even
void PhysicalJoystickHandler::setDefaultMapping(Event::Type event, EventMode mode) void PhysicalJoystickHandler::setDefaultMapping(Event::Type event, EventMode mode)
{ {
eraseMapping(event, mode); eraseMapping(event, mode);
for (auto& i : mySticks) for (const auto& [_id, _joyptr]: mySticks)
setStickDefaultMapping(i.first, event, mode); setStickDefaultMapping(_id, event, mode);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -529,24 +529,24 @@ void PhysicalJoystickHandler::eraseMapping(Event::Type event, EventMode mode)
// Otherwise, only reset the given event // Otherwise, only reset the given event
if(event == Event::NoType) if(event == Event::NoType)
{ {
for (auto& stick : mySticks) for (auto& [_id, _joyptr]: mySticks)
{ {
stick.second->eraseMap(mode); // erase all events _joyptr->eraseMap(mode); // erase all events
if(mode == EventMode::kEmulationMode) if(mode == EventMode::kEmulationMode)
{ {
stick.second->eraseMap(EventMode::kCommonMode); _joyptr->eraseMap(EventMode::kCommonMode);
stick.second->eraseMap(EventMode::kJoystickMode); _joyptr->eraseMap(EventMode::kJoystickMode);
stick.second->eraseMap(EventMode::kPaddlesMode); _joyptr->eraseMap(EventMode::kPaddlesMode);
stick.second->eraseMap(EventMode::kKeypadMode); _joyptr->eraseMap(EventMode::kKeypadMode);
} }
} }
} }
else else
{ {
for (auto& stick : mySticks) for (auto& [_id, _joyptr]: mySticks)
{ {
stick.second->eraseEvent(event, mode); // only reset the specific event _joyptr->eraseEvent(event, mode); // only reset the specific event
stick.second->eraseEvent(event, getEventMode(event, mode)); _joyptr->eraseEvent(event, getEventMode(event, mode));
} }
} }
} }
@ -558,9 +558,9 @@ void PhysicalJoystickHandler::saveMapping()
// any changes that have been made during the program run // any changes that have been made during the program run
json mapping = json::array(); json mapping = json::array();
for(const auto& i: myDatabase) for(const auto& [_name, _info]: myDatabase)
{ {
json map = i.second.joy ? i.second.joy->getMap() : i.second.mapping; json map = _info.joy ? _info.joy->getMap() : _info.mapping;
if (!map.is_null()) mapping.emplace_back(map); if (!map.is_null()) mapping.emplace_back(map);
} }
@ -574,19 +574,16 @@ string PhysicalJoystickHandler::getMappingDesc(Event::Type event, EventMode mode
ostringstream buf; ostringstream buf;
EventMode evMode = getEventMode(event, mode); EventMode evMode = getEventMode(event, mode);
for(const auto& s: mySticks) for(const auto& [_id, _joyptr]: mySticks)
{ {
uInt32 stick = s.first; if(_joyptr)
const PhysicalJoystickPtr j = s.second;
if(j)
{ {
//Joystick mapping / labeling //Joystick mapping / labeling
if(j->joyMap.getEventMapping(event, evMode).size()) if(_joyptr->joyMap.getEventMapping(event, evMode).size())
{ {
if(buf.str() != "") if(buf.str() != "")
buf << ", "; buf << ", ";
buf << j->joyMap.getEventMappingDesc(stick, event, evMode); buf << _joyptr->joyMap.getEventMappingDesc(_id, event, evMode);
} }
} }
} }
@ -819,8 +816,8 @@ void PhysicalJoystickHandler::handleHatEvent(int stick, int hat, int value)
VariantList PhysicalJoystickHandler::database() const VariantList PhysicalJoystickHandler::database() const
{ {
VariantList db; VariantList db;
for(const auto& i: myDatabase) for(const auto& [_name, _info]: myDatabase)
VarList::push_back(db, i.first, i.second.joy ? i.second.joy->ID : -1); VarList::push_back(db, _name, _info.joy ? _info.joy->ID : -1);
return db; return db;
} }
@ -830,13 +827,13 @@ ostream& operator<<(ostream& os, const PhysicalJoystickHandler& jh)
{ {
os << "---------------------------------------------------------" << endl os << "---------------------------------------------------------" << endl
<< "joy database:" << endl; << "joy database:" << endl;
for(const auto& i: jh.myDatabase) for(const auto& [_name, _info]: jh.myDatabase)
os << i.first << endl << i.second << endl << endl; os << _name << endl << _info << endl << endl;
os << "---------------------" << endl os << "---------------------" << endl
<< "joy active:" << endl; << "joy active:" << endl;
for(const auto& i: jh.mySticks) for(const auto& [_id, _joyptr]: jh.mySticks)
os << i.first << ": " << *i.second << endl; os << _id << ": " << *_joyptr << endl;
os << "---------------------------------------------------------" os << "---------------------------------------------------------"
<< endl << endl << endl; << endl << endl << endl;

View File

@ -123,11 +123,11 @@ void parseCommandLine(int ac, char* av[],
#if 0 #if 0
cout << "Global opts:" << endl; cout << "Global opts:" << endl;
for(const auto& x: globalOpts) for(const auto& [key, value]: globalOpts)
cout << " -> " << x.first << ": " << x.second << endl; cout << " -> " << key << ": " << value << endl;
cout << "Local opts:" << endl; cout << "Local opts:" << endl;
for(const auto& x: localOpts) for(const auto& [key, value]: localOpts)
cout << " -> " << x.first << ": " << x.second << endl; cout << " -> " << key << ": " << value << endl;
#endif #endif
} }

View File

@ -100,8 +100,8 @@ void KeyValueRepositoryConfigfile::save(const std::map<string, Variant>& values)
<< ";" << endl; << ";" << endl;
// Write out each of the key and value pairs // Write out each of the key and value pairs
for(const auto& pair: values) for(const auto& [key, value]: values)
out << pair.first << " = " << pair.second << endl; out << key << " = " << value << endl;
try try
{ {

View File

@ -635,9 +635,9 @@ string DebuggerParser::saveScriptFile(string file)
stringstream out; stringstream out;
Debugger::FunctionDefMap funcs = debugger.getFunctionDefMap(); Debugger::FunctionDefMap funcs = debugger.getFunctionDefMap();
for(const auto& f: funcs) for(const auto& [name, cmd]: funcs)
if (!debugger.isBuiltinFunction(f.first)) if (!debugger.isBuiltinFunction(name))
out << "function " << f.first << " {" << f.second << "}" << endl; out << "function " << name << " {" << cmd << "}" << endl;
for(const auto& w: myWatches) for(const auto& w: myWatches)
out << "watch " << w << endl; out << "watch " << w << endl;
@ -1526,10 +1526,8 @@ void DebuggerParser::executeListfunctions()
const Debugger::FunctionDefMap& functions = debugger.getFunctionDefMap(); const Debugger::FunctionDefMap& functions = debugger.getFunctionDefMap();
if(functions.size() > 0) if(functions.size() > 0)
{ for(const auto& [name, cmd]: functions)
for(const auto& iter: functions) commandResult << name << " -> " << cmd << endl;
commandResult << iter.first << " -> " << iter.second << endl;
}
else else
commandResult << "no user-defined functions"; commandResult << "no user-defined functions";
} }

View File

@ -80,10 +80,10 @@ void JoystickDialog::loadConfig()
myJoyIDs.clear(); myJoyIDs.clear();
StringList sticks; StringList sticks;
for(const auto& i: instance().eventHandler().physicalJoystickDatabase()) for(const auto& [_name, _id]: instance().eventHandler().physicalJoystickDatabase())
{ {
sticks.push_back(i.first); sticks.push_back(_name);
myJoyIDs.push_back(i.second.toInt()); myJoyIDs.push_back(_id.toInt());
} }
myJoyList->setList(sticks); myJoyList->setList(sticks);
myJoyList->setSelected(0); myJoyList->setSelected(0);