Codechange: make TimerGameCalendar Date and Year types strongly typed (#10761)

This commit is contained in:
Patric Stout
2023-08-12 20:14:21 +02:00
committed by GitHub
parent 0238a2b567
commit 299570b2c1
55 changed files with 390 additions and 203 deletions

View File

@@ -75,8 +75,8 @@ static const uint16_t _accum_days_for_month[] = {
*/
/* There are 97 leap years in 400 years */
TimerGameCalendar::Year yr = 400 * (date / (DAYS_IN_YEAR * 400 + 97));
int rem = date % (DAYS_IN_YEAR * 400 + 97);
TimerGameCalendar::Year yr = 400 * (static_cast<int32_t>(date) / (DAYS_IN_YEAR * 400 + 97));
int rem = static_cast<int32_t>(date) % (DAYS_IN_YEAR * 400 + 97);
uint16_t x;
if (rem >= DAYS_IN_YEAR * 100 + 25) {
@@ -141,7 +141,7 @@ static const uint16_t _accum_days_for_month[] = {
*/
/* static */ bool TimerGameCalendar::IsLeapYear(TimerGameCalendar::Year yr)
{
return yr % 4 == 0 && (yr % 100 != 0 || yr % 400 == 0);
return static_cast<int32_t>(yr) % 4 == 0 && (static_cast<int32_t>(yr) % 100 != 0 || static_cast<int32_t>(yr) % 400 == 0);
}
/**
@@ -215,7 +215,7 @@ void TimerManager<TimerGameCalendar>::Elapsed(TimerGameCalendar::TElapsed delta)
timer->Elapsed(TimerGameCalendar::DAY);
}
if ((TimerGameCalendar::date % 7) == 3) {
if ((static_cast<int32_t>(TimerGameCalendar::date) % 7) == 3) {
for (auto timer : timers) {
timer->Elapsed(TimerGameCalendar::WEEK);
}

View File

@@ -11,6 +11,7 @@
#define TIMER_GAME_CALENDAR_H
#include "stdafx.h"
#include "../core/strong_typedef_type.hpp"
/**
* Timer that is increased every 27ms, and counts towards ticks / days / months / years.
@@ -76,12 +77,18 @@ public:
struct TStorage {
};
using Date = int32_t; ///< The type to store our dates in
using DateFract = uint16_t; ///< The fraction of a date we're in, i.e. the number of ticks since the last date changeover
/** The type to store our dates in. */
using Date = StrongType::Typedef<int32_t, struct DateTag, StrongType::Explicit, StrongType::Compare, StrongType::Integer>;
using Year = int32_t; ///< Type for the year, note: 0 based, i.e. starts at the year 0.
using Month = uint8_t; ///< Type for the month, note: 0 based, i.e. 0 = January, 11 = December.
using Day = uint8_t; ///< Type for the day of the month, note: 1 based, first day of a month is 1.
/** The fraction of a date we're in, i.e. the number of ticks since the last date changeover. */
using DateFract = uint16_t;
/** Type for the year, note: 0 based, i.e. starts at the year 0. */
using Year = StrongType::Typedef<int32_t, struct YearTag, StrongType::Explicit, StrongType::Compare, StrongType::Integer>;
/** Type for the month, note: 0 based, i.e. 0 = January, 11 = December. */
using Month = uint8_t;
/** Type for the day of the month, note: 1 based, first day of a month is 1. */
using Day = uint8_t;
/**
* Data structure to convert between Date and triplet (year, month, and day).