mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-06 13:33:55 -07:00
move incrementCount() implementation from header to cpp
This commit is contained in:
parent
eca3d75e12
commit
d63501ed84
2 changed files with 13 additions and 11 deletions
|
|
@ -1,12 +1,24 @@
|
|||
#include "server_counter.h"
|
||||
|
||||
#include <libcockatrice/protocol/pb/serverinfo_counter.pb.h>
|
||||
#include <limits>
|
||||
|
||||
Server_Counter::Server_Counter(int _id, const QString &_name, const color &_counterColor, int _radius, int _count)
|
||||
: id(_id), name(_name), counterColor(_counterColor), radius(_radius), count(_count)
|
||||
{
|
||||
}
|
||||
|
||||
//! \todo Extract overflow-safe arithmetic into shared helper.
|
||||
//! Duplicated in Server_Card::incrementCounter() - keep in sync if modified.
|
||||
bool Server_Counter::incrementCount(int delta)
|
||||
{
|
||||
const int oldCount = count;
|
||||
const auto result = static_cast<int64_t>(count) + static_cast<int64_t>(delta);
|
||||
count = static_cast<int>(qBound(static_cast<int64_t>(std::numeric_limits<int>::min()), result,
|
||||
static_cast<int64_t>(std::numeric_limits<int>::max())));
|
||||
return count != oldCount;
|
||||
}
|
||||
|
||||
void Server_Counter::getInfo(ServerInfo_Counter *info)
|
||||
{
|
||||
info->set_id(id);
|
||||
|
|
|
|||
|
|
@ -22,7 +22,6 @@
|
|||
|
||||
#include <QString>
|
||||
#include <libcockatrice/protocol/pb/color.pb.h>
|
||||
#include <limits>
|
||||
|
||||
class ServerInfo_Counter;
|
||||
|
||||
|
|
@ -93,16 +92,7 @@ public:
|
|||
* @return true if the value changed, false otherwise.
|
||||
* @note Clamps result to [INT_MIN, INT_MAX] to prevent overflow.
|
||||
*/
|
||||
[[nodiscard]] bool incrementCount(int delta)
|
||||
{
|
||||
// TODO: Extract overflow-safe arithmetic into shared helper.
|
||||
// Duplicated in Server_Card::incrementCounter() - keep in sync if modified.
|
||||
const int oldCount = count;
|
||||
const auto result = static_cast<int64_t>(count) + static_cast<int64_t>(delta);
|
||||
count = static_cast<int>(qBound(static_cast<int64_t>(std::numeric_limits<int>::min()), result,
|
||||
static_cast<int64_t>(std::numeric_limits<int>::max())));
|
||||
return count != oldCount;
|
||||
}
|
||||
[[nodiscard]] bool incrementCount(int delta);
|
||||
|
||||
/**
|
||||
* @brief Populates info with this counter's current state for network serialization.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue