mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-04 12:33:55 -07:00
- Add addClamped() in new header clamped_arithmetic.h; uses a 64-bit intermediate so the addition cannot overflow int. - Use it in Server_Card::incrementCounter() (clamps [0, MAX_COUNTERS_ON_CARD]) and Server_Counter::incrementCount() (clamps [INT_MIN, INT_MAX]), removing the duplicated overflow-safe logic and its keep-in-sync TODO. - Inline incrementCount() into server_counter.h; server_counter.cpp now holds only the constructor and getInfo(). - Clarify the card-counter bounds comment in trice_limits.h.
22 lines
825 B
C++
22 lines
825 B
C++
#ifndef CLAMPED_ARITHMETIC_H
|
|
#define CLAMPED_ARITHMETIC_H
|
|
|
|
#include <QtGlobal>
|
|
#include <cstdint>
|
|
|
|
/**
|
|
* @brief Overflow-safe clamped addition: returns value + delta bounded to [minValue, maxValue].
|
|
*
|
|
* Uses a 64-bit intermediate so the addition itself cannot overflow int. Shared by the
|
|
* counter arithmetic in Server_Card and Server_Counter so both stay in sync.
|
|
*
|
|
* @note Requires minValue <= maxValue. Bounds come from trusted compile-time call sites;
|
|
* qBound() asserts this internally in debug builds.
|
|
*/
|
|
inline int addClamped(int value, int delta, int minValue, int maxValue)
|
|
{
|
|
const auto result = static_cast<int64_t>(value) + static_cast<int64_t>(delta);
|
|
return static_cast<int>(qBound(static_cast<int64_t>(minValue), result, static_cast<int64_t>(maxValue)));
|
|
}
|
|
|
|
#endif // CLAMPED_ARITHMETIC_H
|