From 8fce97ea1cb3a4ba3c05e2b293a68039ce2a6ba7 Mon Sep 17 00:00:00 2001 From: nmlgc Date: Thu, 9 Jun 2022 17:03:49 +0200 Subject: [PATCH] [Maintenance] Add a static_assert() polyfill to platform.h Part of P0198, funded by Lmocinemod and Ember2528. --- platform.h | 5 +++++ th05/main/bullet/b6ball.cpp | 2 +- th05/main/custom.h | 10 +++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/platform.h b/platform.h index da2024ed..ee4ddcae 100644 --- a/platform.h +++ b/platform.h @@ -31,6 +31,11 @@ typedef void ( far pascal * far farfunc_t_far)(void); #endif /// ---------- +// Message-less static_assert() wasn't available until C++17 +#if (__cplusplus < 201703L) + #define static_assert(condition) ((void)sizeof(char[1 - 2*!(condition)])) +#endif + // Both Turbo C++ and master.lib use uint16_t for segment values throughout // their APIs instead of the more sensible void __seg*. Maybe, integer // arithmetic on segment values was widely considered more important than diff --git a/th05/main/bullet/b6ball.cpp b/th05/main/bullet/b6ball.cpp index 5b2a68a8..1cbae1fd 100644 --- a/th05/main/bullet/b6ball.cpp +++ b/th05/main/bullet/b6ball.cpp @@ -30,7 +30,7 @@ void near b6balls_add(void) void near b6balls_update(void) { - CUSTOM_VERIFY(b6ball_t, B6BALL_COUNT); + custom_assert_count(b6ball_t, B6BALL_COUNT); b6ball_t near *p; int i; diff --git a/th05/main/custom.h b/th05/main/custom.h index a7647c20..8f2fb577 100644 --- a/th05/main/custom.h +++ b/th05/main/custom.h @@ -20,8 +20,8 @@ typedef struct { extern custom_t custom_entities[CUSTOM_COUNT]; -#define CUSTOM_VERIFY(derived_type, derived_count) \ - ((void)sizeof(char[1 - 2*!!( \ - (sizeof(derived_type) * derived_count) \ - > (sizeof(custom_t) * CUSTOM_COUNT) \ - )])) +#define custom_assert_count(derived_type, derived_count) \ + static_assert( \ + (sizeof(derived_type) * derived_count) <= \ + (sizeof(custom_t) * CUSTOM_COUNT) \ + )