From e4fb617befbf7fdaa5bd362529debec3082d2f58 Mon Sep 17 00:00:00 2001 From: David Schinazi Date: Sun, 22 Dec 2024 15:21:55 -0800 Subject: [PATCH] Tighten custom int type definitions --- src/jazzlights/types.h | 18 ++++++++++++++---- src/jazzlights/util/time.h | 8 +++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/jazzlights/types.h b/src/jazzlights/types.h index 2e8b7e74..f980c0c1 100644 --- a/src/jazzlights/types.h +++ b/src/jazzlights/types.h @@ -2,16 +2,26 @@ #define JL_TYPES_H #include +#include #include #include "jazzlights/util/geom.h" namespace jazzlights { -// We would normally define PatternBits as int32_t, but then some compilers would require us to use PRIu32 as a printf -// format, so we define it as int so we can use %u instead. -using PatternBits = unsigned int; -static_assert(sizeof(uint32_t) == sizeof(PatternBits), "bad int size"); +// We would normally define out int types using fixed-width types such as as int32_t, but then some compilers would +// require us to use PRId32 as a printf format, so we define them as older types so we can use %u or %d instead. + +#define JL_ASSERT_INT_TYPES_EQUAL(_a, _b) \ + static_assert(sizeof(_a) == sizeof(_b) && std::numeric_limits<_a>::min() == std::numeric_limits<_b>::min() && \ + std::numeric_limits<_a>::max() == std::numeric_limits<_b>::max(), \ + "bad int type") + +#define JL_DEFINE_INT_TYPE(_new_type, _base_type, _expected_type) \ + using _new_type = _base_type; \ + JL_ASSERT_INT_TYPES_EQUAL(_new_type, _expected_type) + +JL_DEFINE_INT_TYPE(PatternBits, unsigned int, uint32_t); using Precedence = uint16_t; using NumHops = uint8_t; diff --git a/src/jazzlights/util/time.h b/src/jazzlights/util/time.h index 32c707d1..52b58701 100644 --- a/src/jazzlights/util/time.h +++ b/src/jazzlights/util/time.h @@ -1,13 +1,11 @@ #ifndef JL_UTIL_TIME_H #define JL_UTIL_TIME_H -#include + +#include "jazzlights/types.h" namespace jazzlights { -// We would normally define Milliseconds as int32_t, but then some compilers would require us to use PRId32 as a printf -// format, so we define it as int so we can use %d instead. -typedef int Milliseconds; -static_assert(sizeof(int32_t) == sizeof(Milliseconds), "bad int size"); +JL_DEFINE_INT_TYPE(Milliseconds, int, int32_t); enum : Milliseconds { ONE_SECOND = 1000,