Use std::optional instead of sentinel values for absent data

This commit is contained in:
2026-07-20 20:27:20 +02:00
parent 1cdafb7bcd
commit 9622fa4345
32 changed files with 228 additions and 209 deletions

View File

@@ -83,7 +83,7 @@ public:
setFixedSize(cols * kCellSize + 1, rows * kCellSize + 1);
}
void setGhostData(int moduleIndex, Rotation rotation)
void setGhostData(std::optional<int> moduleIndex, Rotation rotation)
{
m_ghostModuleIdx = moduleIndex;
m_ghostRotation = rotation;
@@ -111,9 +111,9 @@ protected:
{
painter.fillRect(cellRect, QColor(30, 30, 30));
}
else if (cell.moduleIndex >= 0)
else if (cell.moduleIndex.has_value())
{
const PlacedModule& pm = (*m_placed)[cell.moduleIndex];
const PlacedModule& pm = (*m_placed)[*cell.moduleIndex];
const ModuleDef* def = findModule(pm.moduleId);
QColor color(Qt::gray);
QString glyph;
@@ -140,9 +140,9 @@ protected:
}
// Draw ghost
if (m_ghostModuleIdx >= 0 && m_hoverCell.x() >= 0 && m_config)
if (m_ghostModuleIdx.has_value() && m_hoverCell.x() >= 0 && m_config)
{
const ModuleDef& def = m_config->modules.modules[m_ghostModuleIdx];
const ModuleDef& def = m_config->modules.modules[*m_ghostModuleIdx];
const std::vector<std::string> mask = rotateMask(def.surfaceMask, m_ghostRotation);
QColor ghostColor(QString::fromStdString(def.fillColor));
ghostColor.setAlpha(100);
@@ -238,7 +238,7 @@ private:
int m_cols = 0;
const std::vector<PlacedModule>* m_placed = nullptr;
const GameConfig* m_config = nullptr;
int m_ghostModuleIdx = -2;
std::optional<int> m_ghostModuleIdx;
Rotation m_ghostRotation = Rotation::East;
QPoint m_hoverCell = QPoint(-1, -1);
};
@@ -374,7 +374,6 @@ ShipLayoutDialog::ShipLayoutDialog(const GameConfig* config,
, m_rows(0)
, m_cols(0)
, m_placedModules(currentLayout.placedModules)
, m_activeModuleIndex(-2)
, m_currentRotation(Rotation::East)
, m_removeButton(nullptr)
, m_gridWidget(nullptr)
@@ -406,7 +405,7 @@ ShipLayoutDialog::ShipLayoutDialog(const GameConfig* config,
}
// Initialize grid.
m_grid.assign(m_rows, std::vector<CellInfo>(m_cols, {false, -1}));
m_grid.assign(m_rows, std::vector<CellInfo>(m_cols, {false, std::nullopt}));
for (int r = 0; r < m_rows; ++r)
{
for (int c = 0; c < static_cast<int>(m_shipLayout[r].size()); ++c)
@@ -491,9 +490,9 @@ ShipLayoutDialog::ShipLayoutDialog(const GameConfig* config,
}
buttonGrid->addWidget(m_removeButton, row, 0, 1, kCols);
connect(m_removeButton, &QPushButton::clicked, this, [this]() {
if (m_activeModuleIndex == -1)
if (m_removeMode)
{
m_activeModuleIndex = -2;
m_removeMode = false;
m_removeButton->setChecked(false);
}
else
@@ -502,7 +501,8 @@ ShipLayoutDialog::ShipLayoutDialog(const GameConfig* config,
{
if (btn) { btn->setChecked(false); }
}
m_activeModuleIndex = -1;
m_activeModuleIndex = std::nullopt;
m_removeMode = true;
m_removeButton->setChecked(true);
}
updateGridWidget();
@@ -540,20 +540,20 @@ ShipLayoutDialog::ShipLayoutDialog(const GameConfig* config,
// Grid click handler.
connect(this, &ShipLayoutDialog::gridCellClicked, this, [this](QPoint cell) {
if (m_activeModuleIndex == -2)
if (!m_removeMode && !m_activeModuleIndex.has_value())
{
return;
}
if (m_activeModuleIndex == -1)
if (m_removeMode)
{
// Remove mode: find and remove module at cell.
if (cell.y() >= 0 && cell.y() < m_rows && cell.x() >= 0 && cell.x() < m_cols)
{
const int idx = m_grid[cell.y()][cell.x()].moduleIndex;
if (idx >= 0)
const std::optional<int> idx = m_grid[cell.y()][cell.x()].moduleIndex;
if (idx.has_value())
{
m_placedModules.erase(m_placedModules.begin() + idx);
m_placedModules.erase(m_placedModules.begin() + *idx);
rebuildOccupancy();
updateGridWidget();
updateStats();
@@ -563,7 +563,7 @@ ShipLayoutDialog::ShipLayoutDialog(const GameConfig* config,
}
// Place module.
const ModuleDef& def = m_config->modules.modules[m_activeModuleIndex];
const ModuleDef& def = m_config->modules.modules[*m_activeModuleIndex];
if (canPlaceModule(def, cell, m_currentRotation))
{
PlacedModule pm;
@@ -622,7 +622,7 @@ void ShipLayoutDialog::onModuleButtonClicked(int index)
if (m_activeModuleIndex == index)
{
if (m_moduleButtons[index]) { m_moduleButtons[index]->setChecked(false); }
m_activeModuleIndex = -2;
m_activeModuleIndex = std::nullopt;
}
else
{
@@ -631,6 +631,7 @@ void ShipLayoutDialog::onModuleButtonClicked(int index)
if (m_moduleButtons[i]) { m_moduleButtons[i]->setChecked(i == index); }
}
m_removeButton->setChecked(false);
m_removeMode = false;
m_activeModuleIndex = index;
}
updateGridWidget();
@@ -656,7 +657,7 @@ void ShipLayoutDialog::rebuildOccupancy()
{
for (int c = 0; c < m_cols; ++c)
{
m_grid[r][c].moduleIndex = -1;
m_grid[r][c].moduleIndex = std::nullopt;
}
}
@@ -726,7 +727,7 @@ bool ShipLayoutDialog::canPlaceModule(const ModuleDef& def, QPoint position,
{
return false;
}
if (m_grid[gr][gc].moduleIndex >= 0)
if (m_grid[gr][gc].moduleIndex.has_value())
{
return false;
}