Skip to content

Commit

Permalink
Use C++17 structure binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarod42 committed Apr 24, 2023
1 parent 5ad6622 commit f3248df
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 70 deletions.
5 changes: 1 addition & 4 deletions src/lib/game/logic/action/actionbuyupgrades.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,8 @@ void cActionBuyUpgrades::execute (cModel& model) const

const cUnitsData& unitsdata = *model.getUnitsData();

for (size_t i = 0; i < unitUpgrades.size(); ++i)
for (const auto& [unitType, upgradesForUnit] : unitUpgrades)
{
const auto& unitType = unitUpgrades[i].first;
const auto& upgradesForUnit = unitUpgrades[i].second;

if (!unitsdata.isValidId (unitType)) return;

// check costs for upgrading this unit
Expand Down
5 changes: 1 addition & 4 deletions src/lib/game/logic/action/actioninitnewgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,8 @@ void cActionInitNewGame::execute (cModel& model) const

// apply upgrades
int credits = model.getGameSettings()->startCredits;
for (const auto& upgrade : initPlayerData.unitUpgrades)
for (const auto& [unitId, upgradeValues] : initPlayerData.unitUpgrades)
{
const sID& unitId = upgrade.first;
const cUnitUpgrade& upgradeValues = upgrade.second;

if (!unitsdata.isValidId (unitId))
{
NetLog.error (" Apply upgrades failed. Unknown sID: " + unitId.getText());
Expand Down
4 changes: 2 additions & 2 deletions src/lib/game/logic/action/actionupgradevehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ void cActionUpgradeVehicle::execute (cModel& model) const
}
}

for (const auto& x : result)
for (const auto& [id, upgrade] : result)
{
containingBuilding->getOwner()->unitsUpgraded (x.first, x.second.nr, x.second.costs);
containingBuilding->getOwner()->unitsUpgraded (id, upgrade.nr, upgrade.costs);
}
}
8 changes: 4 additions & 4 deletions src/lib/game/logic/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,11 @@ void cClient::handleNetMessages()
freezeModes = msg->freezeModes;
if (waitForServer) freezeModes.enable (eFreezeMode::WaitForServer);

for (const auto& state : msg->playerStates)
for (const auto& [playerId, conn] : msg->playerStates)
{
if (model.getPlayer (state.first) == nullptr)
if (model.getPlayer (playerId) == nullptr)
{
NetLog.error (" Client: Invalid player id: " + std::to_string (state.first));
NetLog.error (" Client: Invalid player id: " + std::to_string (playerId));
break;
}
}
Expand Down Expand Up @@ -612,7 +612,7 @@ void cClient::report (std::unique_ptr<cSavedReport> report)
}

//------------------------------------------------------------------------------
void cClient::sendGUISaveInfo(int slot, int savingId, const sPlayerGuiInfo& guiInfo, std::optional<cGameGuiState> gameGuiState)
void cClient::sendGUISaveInfo (int slot, int savingId, const sPlayerGuiInfo& guiInfo, std::optional<cGameGuiState> gameGuiState)
{
cNetMessageGUISaveInfo message (slot, savingId);
message.guiInfo = guiInfo;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/output/video/video.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ void cVideo::detectResolutions()

for (size_t i = 0; i < resolutions.size(); ++i)
{
const auto& resolution = resolutions[i];
Log.info ("cVideo: Display" + std::to_string (displayIndex) + " is offering detected video mode " + std::to_string (i) + " (" + std::to_string (resolution.first) + "x" + std::to_string (resolution.second) + ")");
const auto& [w, h] = resolutions[i];
Log.info ("cVideo: Display" + std::to_string (displayIndex) + " is offering detected video mode " + std::to_string (i) + " (" + std::to_string (w) + "x" + std::to_string (h) + ")");
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/resources/loaddata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ void debugTranslationSize (const cLanguage& language, const cUnicodeFont& font)
{
#if 1
std::regex reg{".*_([0-9]+)"};
for (const auto& p : language.getAllTranslations())
for (const auto& [key, translatedText] : language.getAllTranslations())
{
std::smatch res;

if (std::regex_match (p.first, res, reg))
if (std::regex_match (key, res, reg))
{
std::size_t maxSize = std::stoi (res[1]);
const char referenceLetter = 'a';

if (font.getTextWide (std::string (maxSize, referenceLetter)) < font.getTextWide (p.second))
if (font.getTextWide (std::string (maxSize, referenceLetter)) < font.getTextWide (translatedText))
{
Log.warn ("Maybe too long string for " + p.first + ": " + p.second);
Log.warn ("Maybe too long string for " + key + ": " + translatedText);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/lib/utility/crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ template <typename T>
template <typename K, typename T>
[[nodiscard]] uint32_t calcCheckSum (const std::map<K, T>& data, uint32_t checksum)
{
for (const auto& x : data)
for (const auto& [key, value] : data)
{
checksum = calcCheckSum (x.first, checksum);
checksum = calcCheckSum (x.second, checksum);
checksum = calcCheckSum (key, checksum);
checksum = calcCheckSum (value, checksum);
}
return checksum;
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib/utility/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,11 @@ std::vector<std::pair<std::string, std::string>> cLanguage::getAllTranslations()
{
std::vector<std::pair<std::string, std::string>> res;
const auto& catalog = pimpl->maxrCatalog;
for (const auto& p : catalog.GetIndex())
for (const auto& [key, index] : catalog.GetIndex())
{
const spiritless_po::Catalog::IndexDataT& index = p.second;
for (std::size_t i = 0; i != index.totalPlurals; ++i)
{
res.emplace_back (p.first, catalog.GetStringTable()[index.stringTableIndex + i]);
res.emplace_back (key, catalog.GetStringTable()[index.stringTableIndex + i]);
}
}
return res;
Expand Down
10 changes: 5 additions & 5 deletions src/ui/graphical/game/control/gameguicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void cGameGuiController::setClients (std::vector<std::shared_ptr<cClient>> clien
if (guiInfo.playerNr != client->getActivePlayer().getId()) return;

const cMap& map = *client->getModel().getMap();
if (ranges::any_of (guiInfo.guiInfo.savedPositions, [&](const auto& savedPosition) { return savedPosition && !map.isValidPosition (*savedPosition);}))
if (ranges::any_of (guiInfo.guiInfo.savedPositions, [&] (const auto& savedPosition) { return savedPosition && !map.isValidPosition (*savedPosition); }))
{
return;
}
Expand Down Expand Up @@ -1878,18 +1878,18 @@ void cGameGuiController::updateGuiInfoTexts()
{
std::string disconncetedPlayers;
std::string notRespondingPlayers;
for (const auto& playerState : playerConnectionStates)
for (const auto& [playerId, state] : playerConnectionStates)
{
const cPlayer& player = *activeClient->getModel().getPlayer (playerState.first);
if (playerState.second == ePlayerConnectionState::Disconnected)
const cPlayer& player = *activeClient->getModel().getPlayer (playerId);
if (state == ePlayerConnectionState::Disconnected)
{
if (!disconncetedPlayers.empty())
{
disconncetedPlayers += ", ";
}
disconncetedPlayers += player.getName();
}
if (playerState.second == ePlayerConnectionState::NotResponding)
if (state == ePlayerConnectionState::NotResponding)
{
if (!notRespondingPlayers.empty())
{
Expand Down
60 changes: 30 additions & 30 deletions src/ui/graphical/game/widgets/gamemapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ cGameMapWidget::cGameMapWidget (const cBox<cPosition>& area, std::shared_ptr<con
});

zoomFactorChanged.connect ([this]() {
const auto tileDrawingRange = computeTileDrawingRange();
const cPosition difference = tileDrawingRange.first - tileDrawingRange.second;
const auto& [topLeft, bottomRight] = computeTileDrawingRange();
const cPosition difference = bottomRight - topLeft;
const auto diameter = difference.l2Norm();
soundManager->setMaxListeningDistance ((int) (diameter * 2));
});
Expand Down Expand Up @@ -452,8 +452,8 @@ void cGameMapWidget::setMapView (std::shared_ptr<const cMapView> mapView_)
mapViewSignalConnectionManager.connect (mapView->unitAppeared, [this] (const cUnit& unit) {
if (!cSettings::getInstance().isAnimations()) return;

const auto tileDrawingRange = computeTileDrawingRange();
const auto tileDrawingArea = cBox<cPosition> (tileDrawingRange.first, tileDrawingRange.second - cPosition (1, 1));
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto tileDrawingArea = cBox<cPosition> (topLeft, bottomRight - cPosition (1, 1));

if (tileDrawingArea.intersects (unit.getArea()))
{
Expand All @@ -464,8 +464,8 @@ void cGameMapWidget::setMapView (std::shared_ptr<const cMapView> mapView_)
mapViewSignalConnectionManager.connect (mapView->unitMoved, [this] (const cUnit& unit, const cPosition& oldPosition) {
if (!cSettings::getInstance().isAnimations()) return;

const auto tileDrawingRange = computeTileDrawingRange();
const auto tileDrawingArea = cBox<cPosition> (tileDrawingRange.first, tileDrawingRange.second - cPosition (1, 1));
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto tileDrawingArea = cBox<cPosition> (topLeft, bottomRight - cPosition (1, 1));

if (tileDrawingArea.intersects (unit.getArea()) && !tileDrawingArea.intersects (cBox<cPosition> (oldPosition, oldPosition + unit.getArea().getSize() - cPosition (1, 1))))
{
Expand Down Expand Up @@ -658,9 +658,9 @@ void cGameMapWidget::toggleHelpMode()
//------------------------------------------------------------------------------
cBox<cPosition> cGameMapWidget::getDisplayedMapArea() const
{
auto tileDrawingRange = computeTileDrawingRange();
auto [topLeft, bottomRight] = computeTileDrawingRange();

return cBox<cPosition> (tileDrawingRange.first, tileDrawingRange.second - 1);
return cBox<cPosition> (topLeft, bottomRight - 1);
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -979,14 +979,14 @@ std::pair<cPosition, cPosition> cGameMapWidget::computeTileDrawingRange() const
void cGameMapWidget::drawTerrain()
{
const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
const auto& terrain = staticMap->getGraphic().getTile (staticMap->getTileIndex (*i));

auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, *i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, *i);
if (shouldDrawFog && (!player || !player->canSeeAt (*i)))
{
if (!cSettings::getInstance().shouldDoPrescale() && (terrain.shw->w != zoomedTileSize.x() || terrain.shw->h != zoomedTileSize.y()))
Expand Down Expand Up @@ -1111,17 +1111,17 @@ void cGameMapWidget::drawShips()
if (!mapView) return;

const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
auto& mapField = mapView->getField (*i);
auto vehicle = mapField.getVehicle();
if (vehicle == nullptr) continue;
if (vehicle->getStaticUnitData().factorSea > 0 && vehicle->getStaticUnitData().factorGround == 0)
{
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, *i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, *i);
unitDrawingEngine.drawUnit (*vehicle, drawDestination, getZoomFactor(), *mapView, &unitSelection, player.get());
}
}
Expand Down Expand Up @@ -1199,17 +1199,17 @@ void cGameMapWidget::drawVehicles()
if (!mapView) return;

const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
auto& mapField = mapView->getField (*i);
auto vehicle = mapField.getVehicle();
if (vehicle == nullptr) continue;
if (vehicle->getStaticUnitData().factorGround != 0 && !vehicle->isUnitBuildingABuilding() && !vehicle->isUnitClearing())
{
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, *i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, *i);
unitDrawingEngine.drawUnit (*vehicle, drawDestination, getZoomFactor(), *mapView, &unitSelection, player.get());
}
}
Expand All @@ -1221,17 +1221,17 @@ void cGameMapWidget::drawConnectors()
if (!mapView) return;

const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
auto& mapField = mapView->getField (*i);
auto building = mapField.getTopBuilding();
if (building == nullptr) continue;
if (building->getStaticUnitData().surfacePosition == eSurfacePosition::Above)
{
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, *i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, *i);
unitDrawingEngine.drawUnit (*building, drawDestination, getZoomFactor(), &unitSelection, player.get(), currentTurnResearchAreasFinished);
}
}
Expand All @@ -1243,15 +1243,15 @@ void cGameMapWidget::drawPlanes()
if (!mapView) return;

const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
auto& mapField = mapView->getField (*i);
const auto& planes = mapField.getPlanes();

auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, *i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, *i);
for (auto it = planes.rbegin(); it != planes.rend(); ++it)
{
auto& plane = **it;
Expand All @@ -1266,17 +1266,17 @@ void cGameMapWidget::drawResources()
if (!mapView) return;

const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

SDL_Rect tmp, src = {0, 0, Uint16 (zoomedTileSize.x()), Uint16 (zoomedTileSize.y())};
for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
if (player && !player->hasResourceExplored (*i)) continue;
if (mapView->isBlocked (*i)) continue;

const auto& resource = mapView->getResource (*i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, *i);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, *i);

if (resource.typ == eResourceType::None)
{
Expand Down Expand Up @@ -1480,10 +1480,10 @@ void cGameMapWidget::drawExitPointsIf (const cUnit& unit, const std::function<bo
void cGameMapWidget::drawExitPoint (const cPosition& position)
{
const auto zoomedTileSize = getZoomedTileSize();
const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();
const auto zoomedStartTilePixelOffset = getZoomedStartTilePixelOffset();

auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, tileDrawingRange.first, position);
auto drawDestination = computeTileDrawingArea (zoomedTileSize, zoomedStartTilePixelOffset, topLeft, position);

const int nr = animationTimer->getAnimationTime() % 5;
SDL_Rect src;
Expand Down Expand Up @@ -2256,9 +2256,9 @@ void cGameMapWidget::renewDamageEffects()
if (!cSettings::getInstance().isDamageEffects()) return;
if (!mapView) return;

const auto tileDrawingRange = computeTileDrawingRange();
const auto [topLeft, bottomRight] = computeTileDrawingRange();

for (auto i = makeIndexIterator (tileDrawingRange.first, tileDrawingRange.second); i.hasMore(); i.next())
for (auto i = makeIndexIterator (topLeft, bottomRight); i.hasMore(); i.next())
{
auto& mapField = mapView->getField (*i);

Expand Down
4 changes: 2 additions & 2 deletions src/ui/graphical/menu/dialogs/dialogpreferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ void cDialogPreferences::loadValues()

resolutionsComboBox->clearItems();
const auto& resolutions = Video.getDetectedResolutions();
for (const auto& resolution : resolutions)
for (const auto& [w, h] : resolutions)
{
resolutionsComboBox->addItem (std::to_string (resolution.first) + "x" + std::to_string (resolution.second));
resolutionsComboBox->addItem (std::to_string (w) + "x" + std::to_string (h));
}
const auto currentResolutionIndex = Video.validateResolution (Video.getResolutionX(), Video.getResolutionY());
if (currentResolutionIndex != -1) resolutionsComboBox->setSelectedIndex (currentResolutionIndex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,7 @@ cGameSettings cWindowGameSettings::getGameSettings() const

gameSettings.startCredits = *creditsGroup->getSelectedValue();

const auto victoryCondition = *victoryGroup->getSelectedValue();
const auto victoryType = victoryCondition.first;
const auto victoryCount = victoryCondition.second;
const auto [victoryType, victoryCount] = *victoryGroup->getSelectedValue();
gameSettings.victoryConditionType = victoryType;
switch (victoryType)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ cWindowLandingUnitSelection::cWindowLandingUnitSelection (cRgbColor playerColor,
//
// Initialization
//
for (const auto& p : initialUnits)
for (const auto& [unitId, cargo] : initialUnits)
{
auto& addedItem = addSelectedUnit (p.first);
addedItem.setCargo (p.second);
fixedSelectedUnits.emplace (&addedItem, p.second);
auto& addedItem = addSelectedUnit (unitId);
addedItem.setCargo (cargo);
fixedSelectedUnits.emplace (&addedItem, cargo);
}

generateSelectionList (true);
Expand Down

0 comments on commit f3248df

Please sign in to comment.