Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions plugins/cursecheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ curses determineCurse(df::unit * unit)

command_result cursecheck (color_ostream &out, vector <string> & parameters)
{
df::unit* selected_unit = Gui::getSelectedUnit(out, true);
vector<df::unit*> selected_unit = {Gui::getSelectedUnit(out, true)};

bool giveDetails = false;
bool giveUnitID = false;
Expand Down Expand Up @@ -157,7 +157,7 @@ command_result cursecheck (color_ostream &out, vector <string> & parameters)
}

// check whole map if no unit is selected
for(df::unit *unit : selected_unit ? vector{ selected_unit } : world->units.all)
for(df::unit *unit : selected_unit[0] ? selected_unit : world->units.all)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have just made a temporary vector for the loop here. Small thing, though.

Copy link
Member Author

@myk002 myk002 Dec 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my first version did just that, but it looked messy (because of the required null check, but it's not like what I ended up with was entirely "clean" either), but maybe I can clean it up more

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#5150 is arguably cleaner

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while i'm not going to change the code we have now, this would arguably have been a good use for a std::span

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for my own education, how would that have worked? a ternary expression must have the same type in both of its branches. How would we wrap units.all to have the same type as the inline span?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was thinking that we knew where in the all vector the selected unit is, but we don't, so there's no value in having either the entire units vector or just part of it, which is what std::span would have done for us here

we want here an object that holds a reference to a vector which is either owning or nonowning, and the object knows which. this is because in the single-unit case, we need a temporary vector which will be destructed when we're done with it, but in the all-units case we do not want to destruct the vector to be iterated. there are ways to do this with shared_ptr but they're not less oblique than the solution used

{
// filter out all "living" units that are currently removed from play
// don't spam all completely dead creatures if not explicitly wanted
Expand Down Expand Up @@ -208,9 +208,9 @@ command_result cursecheck (color_ostream &out, vector <string> & parameters)
}
}

if (selected_unit && !giveDetails)
if (selected_unit[0] && !giveDetails)
out.print("Selected unit is %scursed\n", cursecount == 0 ? "not " : "");
else if (!selected_unit)
else if (!selected_unit[0])
out.print("%zd cursed creatures on map\n", cursecount);

return CR_OK;
Expand Down
Loading