From a4e7a87307db61369181a9dd7993d0bc50662e99 Mon Sep 17 00:00:00 2001 From: Stephen Anthony Date: Sun, 29 Nov 2020 15:50:31 -0330 Subject: [PATCH] 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. --- src/cheat/CheatManager.cxx | 4 +- src/common/JoyMap.cxx | 32 ++++---- src/common/KeyMap.cxx | 24 +++--- src/common/PJoystickHandler.cxx | 79 +++++++++---------- src/common/main.cxx | 8 +- .../KeyValueRepositoryConfigfile.cxx | 4 +- src/debugger/DebuggerParser.cxx | 12 ++- src/emucore/Settings.cxx | 2 +- src/gui/JoystickDialog.cxx | 6 +- 9 files changed, 83 insertions(+), 88 deletions(-) diff --git a/src/cheat/CheatManager.cxx b/src/cheat/CheatManager.cxx index 3f4db3fb4..62834abb2 100644 --- a/src/cheat/CheatManager.cxx +++ b/src/cheat/CheatManager.cxx @@ -253,8 +253,8 @@ void CheatManager::saveCheatDatabase() return; stringstream out; - for(const auto& iter: myCheatMap) - out << "\"" << iter.first << "\" " << "\"" << iter.second << "\"" << endl; + for(const auto& [md5, cheat]: myCheatMap) + out << "\"" << md5 << "\" " << "\"" << cheat << "\"" << endl; try { myOSystem.cheatFile().write(out); } catch(...) { return; } diff --git a/src/common/JoyMap.cxx b/src/common/JoyMap.cxx index f62385160..6ec142e4c 100644 --- a/src/common/JoyMap.cxx +++ b/src/common/JoyMap.cxx @@ -162,13 +162,13 @@ string JoyMap::getEventMappingDesc(int stick, const Event::Type event, const Eve { 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() != "") buf << ", "; - buf << "J" << stick << getDesc(event, item.first); + buf << "J" << stick << getDesc(event, _mapping); } } return buf.str(); @@ -179,9 +179,9 @@ JoyMap::JoyMappingArray JoyMap::getEventMapping(const Event::Type event, const E { JoyMappingArray map; - for (auto item : myMap) - if (item.second == event && item.first.mode == mode) - map.push_back(item.first); + for (const auto& [_mapping, _event]: myMap) + if (_event == event && _mapping.mode == mode) + map.push_back(_mapping); return map; } @@ -191,23 +191,23 @@ json JoyMap::saveMapping(const EventMode mode) const { json eventMappings = json::array(); - for (auto& item: myMap) { - if (item.first.mode != mode) continue; + for (const auto& [_mapping, _event]: myMap) { + if (_mapping.mode != mode) continue; 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) { - eventMapping["axis"] = item.first.axis; - eventMapping["axisDirection"] = item.first.adir; + if (_mapping.axis != JoyAxis::NONE) { + eventMapping["axis"] = _mapping.axis; + eventMapping["axisDirection"] = _mapping.adir; } - if (item.first.hat != -1) { - eventMapping["hat"] = item.first.hat; - eventMapping["hatDirection"] = item.first.hdir; + if (_mapping.hat != -1) { + eventMapping["hat"] = _mapping.hat; + eventMapping["hatDirection"] = _mapping.hdir; } eventMappings.push_back(eventMapping); diff --git a/src/common/KeyMap.cxx b/src/common/KeyMap.cxx index 730db8057..6d60a0ca3 100644 --- a/src/common/KeyMap.cxx +++ b/src/common/KeyMap.cxx @@ -194,13 +194,13 @@ string KeyMap::getEventMappingDesc(const Event::Type event, const EventMode mode { 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() != "") buf << ", "; - buf << getDesc(item.first); + buf << getDesc(_mapping); } } return buf.str(); @@ -211,9 +211,9 @@ KeyMap::MappingArray KeyMap::getEventMapping(const Event::Type event, const Even { MappingArray map; - for (auto item : myMap) - if (item.second == event && item.first.mode == mode) - map.push_back(item.first); + for (const auto& [_mapping, _event]: myMap) + if (_event == event && _mapping.mode == mode) + map.push_back(_mapping); return map; } @@ -223,16 +223,16 @@ json KeyMap::saveMapping(const EventMode mode) const { json mappings = json::array(); - for (auto item : myMap) { - if (item.first.mode != mode) continue; + for (const auto& [_mapping, _event]: myMap) { + if (_mapping.mode != mode) continue; json mapping = json::object(); - mapping["event"] = item.second; - mapping["key"] = item.first.key; + mapping["event"] = _event; + mapping["key"] = _mapping.key; - if (item.first.mod != StellaMod::KBDM_NONE) - mapping["mod"] = serializeModkeyMask(item.first.mod); + if (_mapping.mod != StellaMod::KBDM_NONE) + mapping["mod"] = serializeModkeyMask(_mapping.mod); mappings.push_back(mapping); } diff --git a/src/common/PJoystickHandler.cxx b/src/common/PJoystickHandler.cxx index 3b41cf854..e0ebb55f1 100644 --- a/src/common/PJoystickHandler.cxx +++ b/src/common/PJoystickHandler.cxx @@ -122,8 +122,8 @@ int PhysicalJoystickHandler::add(const PhysicalJoystickPtr& stick) // For non-unique names that already have a database entry, // we append ' #x', where 'x' increases consecutively int count = 0; - for(const auto& i: myDatabase) - if(BSPF::startsWithIgnoreCase(i.first, stick->name) && i.second.joy) + for(const auto& [_name, _info]: myDatabase) + if(BSPF::startsWithIgnoreCase(_name, stick->name) && _info.joy) ++count; if(count > 0) @@ -223,39 +223,39 @@ void PhysicalJoystickHandler::mapStelladaptors(const string& saport) saOrder[0] = 2; saOrder[1] = 1; } - for(auto& stick: mySticks) + for(auto& [_id, _joyptr]: mySticks) { // 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) - 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) { - stick.second->name += " (emulates left joystick port)"; - stick.second->type = PhysicalJoystick::JT_STELLADAPTOR_LEFT; + _joyptr->name += " (emulates left joystick port)"; + _joyptr->type = PhysicalJoystick::JT_STELLADAPTOR_LEFT; } else if(saOrder[saCount] == 2) { - stick.second->name += " (emulates right joystick port)"; - stick.second->type = PhysicalJoystick::JT_STELLADAPTOR_RIGHT; + _joyptr->name += " (emulates right joystick port)"; + _joyptr->type = PhysicalJoystick::JT_STELLADAPTOR_RIGHT; } saCount++; } - else if(BSPF::startsWithIgnoreCase(stick.second->name, "2600-daptor")) + else if(BSPF::startsWithIgnoreCase(_joyptr->name, "2600-daptor")) { if(saOrder[saCount] == 1) { - stick.second->name += " (emulates left joystick port)"; - stick.second->type = PhysicalJoystick::JT_2600DAPTOR_LEFT; + _joyptr->name += " (emulates left joystick port)"; + _joyptr->type = PhysicalJoystick::JT_2600DAPTOR_LEFT; } else if(saOrder[saCount] == 2) { - stick.second->name += " (emulates right joystick port)"; - stick.second->type = PhysicalJoystick::JT_2600DAPTOR_RIGHT; + _joyptr->name += " (emulates right joystick port)"; + _joyptr->type = PhysicalJoystick::JT_2600DAPTOR_RIGHT; } saCount++; } @@ -354,8 +354,8 @@ void PhysicalJoystickHandler::setStickDefaultMapping(int stick, Event::Type even void PhysicalJoystickHandler::setDefaultMapping(Event::Type event, EventMode mode) { eraseMapping(event, mode); - for (auto& i : mySticks) - setStickDefaultMapping(i.first, event, mode); + for (const auto& [_id, _joyptr]: mySticks) + setStickDefaultMapping(_id, event, mode); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -529,24 +529,24 @@ void PhysicalJoystickHandler::eraseMapping(Event::Type event, EventMode mode) // Otherwise, only reset the given event 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) { - stick.second->eraseMap(EventMode::kCommonMode); - stick.second->eraseMap(EventMode::kJoystickMode); - stick.second->eraseMap(EventMode::kPaddlesMode); - stick.second->eraseMap(EventMode::kKeypadMode); + _joyptr->eraseMap(EventMode::kCommonMode); + _joyptr->eraseMap(EventMode::kJoystickMode); + _joyptr->eraseMap(EventMode::kPaddlesMode); + _joyptr->eraseMap(EventMode::kKeypadMode); } } } else { - for (auto& stick : mySticks) + for (auto& [_id, _joyptr]: mySticks) { - stick.second->eraseEvent(event, mode); // only reset the specific event - stick.second->eraseEvent(event, getEventMode(event, mode)); + _joyptr->eraseEvent(event, mode); // only reset the specific event + _joyptr->eraseEvent(event, getEventMode(event, mode)); } } } @@ -558,9 +558,9 @@ void PhysicalJoystickHandler::saveMapping() // any changes that have been made during the program run 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); } @@ -574,19 +574,16 @@ string PhysicalJoystickHandler::getMappingDesc(Event::Type event, EventMode mode ostringstream buf; EventMode evMode = getEventMode(event, mode); - for(const auto& s: mySticks) + for(const auto& [_id, _joyptr]: mySticks) { - uInt32 stick = s.first; - const PhysicalJoystickPtr j = s.second; - - if(j) + if(_joyptr) { //Joystick mapping / labeling - if(j->joyMap.getEventMapping(event, evMode).size()) + if(_joyptr->joyMap.getEventMapping(event, evMode).size()) { if(buf.str() != "") 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 db; - for(const auto& i: myDatabase) - VarList::push_back(db, i.first, i.second.joy ? i.second.joy->ID : -1); + for(const auto& [_name, _info]: myDatabase) + VarList::push_back(db, _name, _info.joy ? _info.joy->ID : -1); return db; } @@ -830,13 +827,13 @@ ostream& operator<<(ostream& os, const PhysicalJoystickHandler& jh) { os << "---------------------------------------------------------" << endl << "joy database:" << endl; - for(const auto& i: jh.myDatabase) - os << i.first << endl << i.second << endl << endl; + for(const auto& [_name, _info]: jh.myDatabase) + os << _name << endl << _info << endl << endl; os << "---------------------" << endl << "joy active:" << endl; - for(const auto& i: jh.mySticks) - os << i.first << ": " << *i.second << endl; + for(const auto& [_id, _joyptr]: jh.mySticks) + os << _id << ": " << *_joyptr << endl; os << "---------------------------------------------------------" << endl << endl << endl; diff --git a/src/common/main.cxx b/src/common/main.cxx index dddae47bd..9ba6862d2 100644 --- a/src/common/main.cxx +++ b/src/common/main.cxx @@ -123,11 +123,11 @@ void parseCommandLine(int ac, char* av[], #if 0 cout << "Global opts:" << endl; - for(const auto& x: globalOpts) - cout << " -> " << x.first << ": " << x.second << endl; + for(const auto& [key, value]: globalOpts) + cout << " -> " << key << ": " << value << endl; cout << "Local opts:" << endl; - for(const auto& x: localOpts) - cout << " -> " << x.first << ": " << x.second << endl; + for(const auto& [key, value]: localOpts) + cout << " -> " << key << ": " << value << endl; #endif } diff --git a/src/common/repository/KeyValueRepositoryConfigfile.cxx b/src/common/repository/KeyValueRepositoryConfigfile.cxx index 44a840b81..ca9f6b3a4 100644 --- a/src/common/repository/KeyValueRepositoryConfigfile.cxx +++ b/src/common/repository/KeyValueRepositoryConfigfile.cxx @@ -100,8 +100,8 @@ void KeyValueRepositoryConfigfile::save(const std::map& values) << ";" << endl; // Write out each of the key and value pairs - for(const auto& pair: values) - out << pair.first << " = " << pair.second << endl; + for(const auto& [key, value]: values) + out << key << " = " << value << endl; try { diff --git a/src/debugger/DebuggerParser.cxx b/src/debugger/DebuggerParser.cxx index cd631bc7e..d6f87b15d 100644 --- a/src/debugger/DebuggerParser.cxx +++ b/src/debugger/DebuggerParser.cxx @@ -635,9 +635,9 @@ string DebuggerParser::saveScriptFile(string file) stringstream out; Debugger::FunctionDefMap funcs = debugger.getFunctionDefMap(); - for(const auto& f: funcs) - if (!debugger.isBuiltinFunction(f.first)) - out << "function " << f.first << " {" << f.second << "}" << endl; + for(const auto& [name, cmd]: funcs) + if (!debugger.isBuiltinFunction(name)) + out << "function " << name << " {" << cmd << "}" << endl; for(const auto& w: myWatches) out << "watch " << w << endl; @@ -1526,10 +1526,8 @@ void DebuggerParser::executeListfunctions() const Debugger::FunctionDefMap& functions = debugger.getFunctionDefMap(); if(functions.size() > 0) - { - for(const auto& iter: functions) - commandResult << iter.first << " -> " << iter.second << endl; - } + for(const auto& [name, cmd]: functions) + commandResult << name << " -> " << cmd << endl; else commandResult << "no user-defined functions"; } diff --git a/src/emucore/Settings.cxx b/src/emucore/Settings.cxx index a074f5697..f181af5a6 100644 --- a/src/emucore/Settings.cxx +++ b/src/emucore/Settings.cxx @@ -252,7 +252,7 @@ void Settings::setRepository(shared_ptr repository) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - void Settings::load(const Options& options) { - Options fromFile = myRespository->load(); + Options fromFile = myRespository->load(); for (const auto& opt: fromFile) setValue(opt.first, opt.second, false); diff --git a/src/gui/JoystickDialog.cxx b/src/gui/JoystickDialog.cxx index 218179e72..0073fb2ae 100644 --- a/src/gui/JoystickDialog.cxx +++ b/src/gui/JoystickDialog.cxx @@ -80,10 +80,10 @@ void JoystickDialog::loadConfig() myJoyIDs.clear(); StringList sticks; - for(const auto& i: instance().eventHandler().physicalJoystickDatabase()) + for(const auto& [_name, _id]: instance().eventHandler().physicalJoystickDatabase()) { - sticks.push_back(i.first); - myJoyIDs.push_back(i.second.toInt()); + sticks.push_back(_name); + myJoyIDs.push_back(_id.toInt()); } myJoyList->setList(sticks); myJoyList->setSelected(0);