Skip to content

Commit

Permalink
Tighten custom int type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidSchinazi committed Dec 22, 2024
1 parent 1c7bcd4 commit e4fb617
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
18 changes: 14 additions & 4 deletions src/jazzlights/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
#define JL_TYPES_H

#include <cstdint>
#include <limits>
#include <vector>

#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;

Expand Down
8 changes: 3 additions & 5 deletions src/jazzlights/util/time.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#ifndef JL_UTIL_TIME_H
#define JL_UTIL_TIME_H
#include <stdint.h>

#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,
Expand Down

0 comments on commit e4fb617

Please sign in to comment.