Skip to content

refactor: Replace hand-written erase loops with std::erase and std::erase_if - #3685

Open
CryoTheRenegade wants to merge 4 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/std-erase-if
Open

CryoTheRenegade wants to merge 4 commits into
TheSuperHackers:mainfrom
CryoTheRenegade:refactor/std-erase-if

Conversation

@CryoTheRenegade

@CryoTheRenegade CryoTheRenegade commented Nov 13, 2026 •

Copy link
Copy Markdown

This follows #3532 and #3652, which added C++20 functions to the VC6 adapters so the code can call the standard versions. The code base has many hand-written loops that erase the elements of a container that match a value or a condition. C++20 has std::erase and std::erase_if for this, so this PR adds them for VC6 and uses them where a loop does nothing but remove the matching elements.

+erase_adapter.h                    // new; included from Common/STLTypedefs.h, so all GameEngine code gets it
+  VC6: std::erase_if               // the same `it = c.erase(it)` loop the code already used; returns the count
+  VC6: std::erase                  // erase_if with bind2nd(equal_to<value_type>(), value)
+  modern compilers: #include <deque>, <list>, <vector>   // C++20 declares both functions there

The adapter is named after the functions, like interlocked_adapter.h, because C++20 has no single header for them. The VC6 versions do not work on std::map or std::set, because STLport's erase(iterator) returns void for those. The header comment lists this and the other differences from the standard functions.

Converted loops (both games unless marked Core). VC6 has no lambdas, so each condition is a small functor struct with its own name, like GameSortStruct in LobbyUtils.cpp:

Function Now
ResourceGatheringManager::removeSupplyCenter, removeSupplyWarehouse std::erase by object ID
WeaponTemplate::trimTriggeredHistoricDamage std::erase_if(..., HistoricDamageHasTriggerId)
Eva::processPlayingMessages std::erase_if(..., EvaCheckIsFinished), with the two original conditions and comments
Eva::reset m_checks.clear() (the loop erased every element)
Squad::getLiveObjects std::erase_if(..., ObjectIsNotSelectable)
JetAIUpdate::pruneDeadTargeters std::erase_if(..., ObjectIDIsGone)
CommandXlat select-team (Core) std::erase_if(..., DrawableIsNotLocallyControlled)

Loops left as they are:

  • They do more than remove the element: they delete or release it, call deleteOverrides(), resetWakeFrame(), createSpawn() or leaveGroup(), deal damage, update a counter or flag, or move the element to another list. This covers Credits, ControlBarResizer, GameWindowTransitions, HeaderTemplate, ShellMenuScheme, Player, CampaignManager, InGameUI, MilesAudioManager, CrateSystem, RankInfo, Science, SpecialPower, ParkingPlace, FlightDeck, Countermeasures, SpawnBehavior, OpenContain, AIGroup, Squad::getAllObjects and WeaponStore::update.
  • ResourceGatheringManager::findBestSupplyWarehouse / findBestSupplyCenter: the same loop also picks the best dock, and splitting it would look up each object twice.
  • WeaponTemplate::trimOldHistoricDamage: it stops at the first entry it keeps.
  • ThingTemplate: clearAiModuleInfo's element type ModuleInfo::Nugget is private; the other three loops also record what they cleared.
  • DiscreteCircle::removeDuplicates: it compares neighbours, so it is a unique, not an erase_if.
  • GameLogic m_normalUpdates and W3DTerrainVisual::updateSeismicSimulations: only compiled with ALLOW_NONSLEEPY_UPDATES and DO_SEISMIC_SIMULATIONS, which nothing defines.

Evidence

  • Disassembly: ClangCL and VC6 Release objects for both games were compared function by function against main. In every changed function the float arithmetic matches main. Call targets match apart from the new remove_if / erase_if / erase helpers and templates that were inlined before and are now called out of line.

    Function ClangCL VC6
    removeSupplyCenter, removeSupplyWarehouse C++20 list::remove_if (37 → 55 instructions) same code; one compare has its operands swapped
    trimTriggeredHistoricDamage list::remove_if 45 → 35 instructions; the trigger ID is loaded into a register once instead of each iteration
    WeaponTemplate::privateFireWeapon only an assert __LINE__ constant changes same
    Eva::reset clear(); the per-element memmove is gone clear() (65 → 47 instructions)
    Eva::processPlayingMessages remove_if on the vector erase_if inlined; calls the predicate and vector::erase where it called __copy
    Squad::getLiveObjects, aiGroupFromSquad (inlines it) remove_if on the vector; it calls the predicate from two places (find the first match, then compact), and still tests each element once vector::erase, __copy_ptrs and the hash-map find are now called out of line instead of inlined
    JetAIUpdate::pruneDeadTargeters, update (inlines it) both call list::remove_if out of line both call erase_if out of line; it calls the hash-map find, where the old loops called _M_find (pruneDeadTargeters) or findObjectByID (update)
    CommandXlat::translateGameMessage the loop moved into list::remove_if the loop has the same instructions, with a 4-byte smaller frame; block layout changed and one inlined TheTacticalView->isUserControlLocked() check is duplicated into a second path

    The remaining elements keep their order in every function, so the game state and CRC should not change.

Council review

Rounds: 2

Delegate Round 1 Round 2
Claude Opus 5.5 9/10 9.5/10
GPT-6.1 Sol 9/10 10/10
Gemini 4 Carbon 8.5/10 no score

Gemini 4 Carbon could not run in round 2 because its usage limit was reached.

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

The pull request adds an erase compatibility adapter and replaces manual container-removal loops with std::erase or std::erase_if across client, resource-gathering, and gameplay code in Generals and GeneralsMD.

Changes

Container erase adoption

Layer / File(s) Summary
Erase adapter and integration
Dependencies/Utility/Utility/erase_adapter.h, Dependencies/Utility/CMakeLists.txt, Core/GameEngine/Include/Common/STLTypedefs.h
The adapter defines std::erase_if and std::erase for _MSC_VER values below 1300. For other compiler versions, it includes deque, list, and vector headers. The utility build list and common typedef header reference the adapter.
Client-side list pruning
Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp, Generals/Code/GameEngine/Source/GameClient/Eva.cpp, GeneralsMD/Code/GameEngine/Source/GameClient/Eva.cpp
Selected drawables and pending Eva checks are filtered with std::erase_if. Eva::reset clears pending checks, and the priority-selection loops declare their iterators in the loop initializer.
Resource list removal
Generals/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp, GeneralsMD/Code/GameEngine/Source/Common/RTS/ResourceGatheringManager.cpp
Supply center and warehouse removal use std::erase to remove matching IDs.
Gameplay list pruning
Generals/Code/GameEngine/Source/GameLogic/AI/Squad.cpp, GeneralsMD/Code/GameEngine/Source/GameLogic/AI/Squad.cpp, Generals/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/JetAIUpdate.cpp, GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Update/AIUpdate/JetAIUpdate.cpp, Generals/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp, GeneralsMD/Code/GameEngine/Source/GameLogic/Object/Weapon.cpp
Squad::getLiveObjects, JetAIUpdate::pruneDeadTargeters, and WeaponTemplate::trimTriggeredHistoricDamage use std::erase_if with predicates for their existing removal conditions.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Suggested labels: Refactor

Suggested reviewers: xezon

Merge Risk: Minimal · up to d3b25

This PR replaces hand-written container-removal loops with standard erase helpers and adds a compatibility header for VC6. Behavior and element order are meant to be unchanged, and no concrete defect was found in the supplied changes. The change is low risk to merge.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: hand-written erase loops are replaced with std::erase and std::erase_if. The changeset adds these functions as VC6 adapters and converts the listed loops to use them. …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is specific and on-topic. It explains that the PR adds erase_adapter.h with VC6 std::erase and std::erase_if adapters, and it lists the converted loops (ResourceGatheringManager, Weapo…
Full details: Title check

Explanation

The title names the main change: hand-written erase loops are replaced with std::erase and std::erase_if. The changeset adds these functions as VC6 adapters and converts the listed loops to use them. The 'refactor:' prefix is a conventional commit type and fits the change.

Full details: Description check

Explanation

The description is specific and on-topic. It explains that the PR adds erase_adapter.h with VC6 std::erase and std::erase_if adapters, and it lists the converted loops (ResourceGatheringManager, WeaponTemplate, Eva, Squad, JetAIUpdate, CommandXlat) and the loops left unchanged with reasons. It also covers the VC6 limitation for std::map and std::set and the disassembly evidence. This matches the raw summary's changes to the same files.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 601af723-a2ca-4ddd-a3cd-2385a867d564
Commits

Reviewing files that changed from the base of the PR and between 0b9f43c and d3b253b.

Files selected for processing: 4

  • Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
  • Dependencies/Utility/Utility/erase_adapter.h
  • Generals/Code/GameEngine/Source/GameLogic/AI/Squad.cpp
  • GeneralsMD/Code/GameEngine/Source/GameLogic/AI/Squad.cpp
Files skipped from review as they are similar to previous changes: 4

  • Core/GameEngine/Source/GameClient/MessageStream/CommandXlat.cpp
  • Dependencies/Utility/Utility/erase_adapter.h
  • Generals/Code/GameEngine/Source/GameLogic/AI/Squad.cpp
  • GeneralsMD/Code/GameEngine/Source/GameLogic/AI/Squad.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

@CryoTheRenegade
CryoTheRenegade marked this pull request as ready for review November 16, 2026 19:28
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T08:31:13.946125Z d3b253b New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge.

Summary

Replaces removal-only loops with std::erase and std::erase_if in both games.

  • Game code can use the same erase calls with VC6 and modern compilers.
  • Selected-unit voice responses use only locally controlled units.
  • EVA clears pending checks at reset and removes checks when they finish or expire.
  • Squad live-object lists leave out objects that are not selectable.
  • Jet targeter lists drop IDs that no longer resolve to objects.
  • Weapon history drops entries that match the current trigger ID.
  • Supply center and warehouse lists remove IDs for departing objects.

Reviews (3) · Last reviewed commit: "style: Add a header line above ObjectIsN..." · Reviewed by Greptile

…ace of hand-written erase loops

Utility/erase_adapter.h provides C++20 std::erase and std::erase_if. VC6 gets
function templates that use the erase-iterator loop the code already used.
Common/STLTypedefs.h includes it, so all GameEngine code gets it.

The converted loops only removed elements that match a predicate without side
effects. The predicates are file-scope functors because VC6 has no lambdas.
Eva::reset erased every element and now calls clear().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
CryoTheRenegade and others added 3 commits November 17, 2026 00:53
…IONS is never defined

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… above translateGameMessage

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…unctions in Squad.cpp

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant