Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a new strong type for game ticks #21395

Open
ZehMatt opened this issue Feb 13, 2024 · 2 comments
Open

Create a new strong type for game ticks #21395

ZehMatt opened this issue Feb 13, 2024 · 2 comments
Assignees
Labels
refactor A task that will improve code readability, without changing outcome.

Comments

@ZehMatt
Copy link
Member

ZehMatt commented Feb 13, 2024

At the moment the game simply uses uint32_t for whenever it stores game ticks which is prone for errors when mixing it with other types, one of the examples can be seen in #21394.

It would be best to create a strong type so that we can no longer mix this up, I'm thinking about using a struct which offers a few helper functions while adhering std::is_trivially_constructible and std::is_pod so we can treat it more or less as a primitive.

struct GameTicks
{
    using ValueType = std::uint32_t;
    
    ValueType Value;
    
    static constexpr GameTicks FromSeconds(uint32_t seconds) { /* ... */ }
    static constexpr GameTicks FromMilliseconds(uint32_t seconds) { /* ... */ }
    
    constexpr GameTicks& operator++() { /* ... */ }
    constexpr GameTicks& operator--() { /* ... */ }
    
    constexpr GameTicks operator+(GameTicks rhs) const { /* ... */ }
    constexpr GameTicks operator-(GameTicks rhs) const { /* ... */ }
    
    /* ... */
};
@ZehMatt ZehMatt added the refactor A task that will improve code readability, without changing outcome. label Feb 13, 2024
@CookiePLMonster
Copy link
Contributor

while adhering std::is_trivially_constructible and std::is_pod

Nitpick: for the sake of future migration to C++20, best not to use std::is_pod (if you want to make it a static assert or something). I think what you want here is std::is_standard_layout.

@ZehMatt
Copy link
Member Author

ZehMatt commented Feb 16, 2024

while adhering std::is_trivially_constructible and std::is_pod

Nitpick: for the sake of future migration to C++20, best not to use std::is_pod (if you want to make it a static assert or something). I think what you want here is std::is_standard_layout.

Yeah, just saying it should act the same as any other primitive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor A task that will improve code readability, without changing outcome.
Projects
None yet
Development

No branches or pull requests

2 participants