Skip to content

Conversation

@JohnsterID
Copy link

@JohnsterID JohnsterID commented Jan 24, 2026

Fixes: #2177 (closes the ternary operator workaround PR)
Alternative to: Ternary operator approach (less readable)

Adds lowercase min/max template functions to BaseType.h for GameEngine layer, providing readable alternatives to uppercase MIN/MAX macros and ternary operators.

Both BaseType.h and always.h provide min/max independently:
BaseType.h → GameEngine code that doesn't need WW3D
always.h → WWVegas/WW3D code

MinGW-w64 locally tested and CI tested: https://github.com/JohnsterID/GeneralsGameCode/actions/runs/21322546177

TODO

  • Rebase on Main after BaseType.h is in Core

JohnsterID added a commit to JohnsterID/GeneralsGameCode that referenced this pull request Jan 24, 2026
…yer (TheSuperHackers#2183)

Add lowercase min/max template functions to BaseType.h alongside existing
uppercase MIN/MAX macros from BaseTypeCore.h.

Problem: BitFlags.h and other GameEngine code needed readable min() calls,
but VC6's <algorithm> lacks std::min/std::max. GameEngine code cannot rely
on always.h (WWVegas layer).

Solution: Add min/max templates to BaseType.h with header guard to prevent
conflicts when GameEngine code includes both BaseType.h and WWVegas headers
(which also define min/max in always.h).

Implementation:
- Use same header guard as always.h (_MIN_MAX_TEMPLATES_DEFINED_)
- Templates coexist with uppercase MIN/MAX macros
- Works with VC6, MSVC, and MinGW-w64
- Follows existing BaseType.h pattern (sqr, clamp, sign templates)

Architecture note: GameEngine intentionally depends on WWVegas (declared
in CMakeLists.txt). When GameEngine includes WW3D headers, always.h is
transitively included. Header guard prevents duplicate definitions.
JohnsterID added a commit to JohnsterID/GeneralsGameCode that referenced this pull request Jan 24, 2026
…SuperHackers#2183)

Replace uppercase MIN/MAX macros with new lowercase min/max templates in
RealRange::combine() for better type safety and readability.

This demonstrates usage of the templates added in the previous commit and
provides a concrete example of improved code clarity.
@JohnsterID JohnsterID force-pushed the feature/basetype-min-max-templates branch from d1190eb to f9072af Compare January 24, 2026 22:40
@xezon
Copy link

xezon commented Jan 24, 2026

I have a change coming soon for moving BaseType.h to Core, so this will need to wait for a moment.

@JohnsterID
Copy link
Author

Moving it to Core would be good. I'll wait out.

@xezon xezon changed the title Feature/basetype min max templates fix(basetype): Add min max templates to BaseType.h Jan 25, 2026
@xezon xezon added CompileBug Bug at compile time Gen Relates to Generals ZH Relates to Zero Hour Fix Is fixing something, but is not user facing labels Jan 25, 2026
Copy link

@xezon xezon left a comment

Choose a reason for hiding this comment

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

Looks good. Finalize and Merge after moving BaseType.h to Core.

@xezon
Copy link

xezon commented Jan 27, 2026

This can now proceed

…yer (TheSuperHackers#2183)

Add lowercase min/max template functions to BaseType.h alongside existing
uppercase MIN/MAX macros from BaseTypeCore.h.

Problem: BitFlags.h and other GameEngine code needed readable min() calls,
but VC6's <algorithm> lacks std::min/std::max. GameEngine code cannot rely
on always.h (WWVegas layer).

Solution: Add min/max templates to BaseType.h with header guard to prevent
conflicts when GameEngine code includes both BaseType.h and WWVegas headers
(which also define min/max in always.h).

Implementation:
- Use same header guard as always.h (_MIN_MAX_TEMPLATES_DEFINED_)
- Templates coexist with uppercase MIN/MAX macros
- Works with VC6, MSVC, and MinGW-w64
- Follows existing BaseType.h pattern (sqr, clamp, sign templates)

Architecture note: GameEngine intentionally depends on WWVegas (declared
in CMakeLists.txt). When GameEngine includes WW3D headers, always.h is
transitively included. Header guard prevents duplicate definitions.
…SuperHackers#2183)

Replace uppercase MIN/MAX macros with new lowercase min/max templates in
RealRange::combine() for better type safety and readability.

This demonstrates usage of the templates added in the previous commit and
provides a concrete example of improved code clarity.
@JohnsterID JohnsterID force-pushed the feature/basetype-min-max-templates branch from f9072af to b81e9ab Compare January 28, 2026 09:00
@greptile-apps
Copy link

greptile-apps bot commented Jan 28, 2026

Greptile Overview

Greptile Summary

Adds lowercase min/max template functions to BaseType.h for the GameEngine layer, providing type-safe alternatives to the uppercase MIN/MAX macros. The implementation uses the same header guard _MIN_MAX_TEMPLATES_DEFINED_ as always.h to prevent duplicate definitions when both headers are included.

Key changes:

  • Added template functions min<T> and max<T> with #undef guards to handle Windows macro conflicts
  • Updated RealRange::combine to use the new lowercase functions instead of uppercase macros
  • Maintains compatibility with WWVegas/WW3D code that uses always.h

The implementation is sound and follows the same pattern as always.h (lines 224-253). The changes improve type safety and readability compared to both macros and ternary operators.

Confidence Score: 4/5

  • This PR is safe to merge with minimal risk, containing only additive template functions and a single macro-to-function replacement
  • Score reflects that the implementation follows established patterns from always.h, uses proper header guards to prevent conflicts, and makes only minimal changes to existing code (RealRange::combine). The only concern is minor style inconsistency in the comment format.
  • No files require special attention

Important Files Changed

Filename Overview
Core/Libraries/Include/Lib/BaseType.h Added lowercase min/max template functions with header guards to prevent conflicts with WWVegas headers, replacing uppercase MIN/MAX macro usage in RealRange::combine

Sequence Diagram

sequenceDiagram
    participant GE as GameEngine Code
    participant BT as BaseType.h
    participant RR as RealRange::combine
    participant MT as min/max templates

    Note over GE,MT: Compilation Flow

    GE->>BT: #include "Lib/BaseType.h"
    BT->>BT: Check _MIN_MAX_TEMPLATES_DEFINED_
    alt Not defined
        BT->>BT: #undef min/max (clear Windows macros)
        BT->>MT: Define min<T>(a,b) template
        BT->>MT: Define max<T>(a,b) template
        BT->>BT: Set _MIN_MAX_TEMPLATES_DEFINED_
    else Already defined (e.g., from always.h)
        BT->>BT: Skip template definitions
    end

    Note over GE,RR: Runtime Usage

    GE->>RR: Call combine(other)
    RR->>MT: Call min(lo, other.lo)
    MT-->>RR: Return smaller Real value
    RR->>MT: Call max(hi, other.hi)
    MT-->>RR: Return larger Real value
    RR-->>GE: Updated range
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@xezon xezon changed the title fix(basetype): Add min max templates to BaseType.h fix(basetype): Add min/max template functions to BaseType.h Jan 28, 2026
@xezon xezon merged commit 55ee429 into TheSuperHackers:main Jan 28, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CompileBug Bug at compile time Fix Is fixing something, but is not user facing Gen Relates to Generals ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants