Skip to content

Commit

Permalink
Replace unweildy COMPILE_ASSERT macro with static_assert
Browse files Browse the repository at this point in the history
When this codebase was originally written, there wasn't a built-in
 cross-compiler way to add compile-time asserts, so the original
 authors did something clever by forcing the compile-time
 instantiation of a undefined template class if a precondition was
 not met.

However, this produces a wall of insane template errors all wrapped
 within macro expansions that is completely unintelligible if you
 haven't seen it before.

Since C++11 we have static_assert for the same purpose, and can
 produce nice diagnostic strings as an added bonus :)
  • Loading branch information
bluebandit21 committed Jan 7, 2025
1 parent b4cd354 commit 2d57161
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 31 deletions.
26 changes: 0 additions & 26 deletions src/Etterna/Globals/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,6 @@ ShowWarningOrTrace(const char* file,
#define SM_UNIQUE_NAME2(x, line) SM_UNIQUE_NAME3(x, line)
#define SM_UNIQUE_NAME(x) SM_UNIQUE_NAME2(x, __LINE__)

template<bool>
struct CompileAssert;
template<>
struct CompileAssert<true>
{
};
template<int>
struct CompileAssertDecl
{
};

// Ignore "unused-local-typedef" warnings for COMPILE_ASSERT
#if defined(__clang__)
#define COMPILE_ASSERT_PRE \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wunused-local-typedef\"")
#define COMPILE_ASSERT_POST _Pragma("clang diagnostic pop")
#else
#define COMPILE_ASSERT_PRE
#define COMPILE_ASSERT_POST
#endif
#define COMPILE_ASSERT(COND) \
COMPILE_ASSERT_PRE \
typedef CompileAssertDecl<sizeof(CompileAssert<!!(COND)>)> \
CompileAssertInst COMPILE_ASSERT_POST

#include "RageUtil/Misc/RageException.h"
/* Don't include our own headers here, since they tend to change often. */

Expand Down
2 changes: 1 addition & 1 deletion src/Etterna/Models/Misc/EnumHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ EnumToString(int iVal,
\
const std::string& X##ToString(X x); \
\
COMPILE_ASSERT(NUM_##X == ARRAYLEN(X##Names)); \
static_assert(NUM_##X == ARRAYLEN(X##Names), "Size mismatch between "#X" enum and "#X"Names (Did you forget to add a string for a new enum entry?)"); \
\
const std::string& X##ToString(X x) \
\
Expand Down
4 changes: 2 additions & 2 deletions src/Etterna/Models/Misc/Game.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "Etterna/Globals/global.h"
#include "Etterna/Globals/global.h"
#include "Game.h"

TapNoteScore
Expand Down Expand Up @@ -38,7 +38,7 @@ static const Game::PerButtonInfo g_CommonButtonInfo[] = {
const Game::PerButtonInfo*
Game::GetPerButtonInfo(GameButton gb) const
{
COMPILE_ASSERT(GAME_BUTTON_NEXT == ARRAYLEN(g_CommonButtonInfo));
static_assert(GAME_BUTTON_NEXT == ARRAYLEN(g_CommonButtonInfo));
if (gb < GAME_BUTTON_NEXT)
return &g_CommonButtonInfo[gb];

Expand Down
2 changes: 1 addition & 1 deletion src/Etterna/Singletons/InputMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ static const InputScheme::GameButtonInfo g_CommonGameButtonInfo[] = {
const InputScheme::GameButtonInfo*
InputScheme::GetGameButtonInfo(GameButton gb) const
{
COMPILE_ASSERT(GAME_BUTTON_NEXT == ARRAYLEN(g_CommonGameButtonInfo));
static_assert(GAME_BUTTON_NEXT == ARRAYLEN(g_CommonGameButtonInfo));
if (gb < GAME_BUTTON_NEXT)
return &g_CommonGameButtonInfo[gb];

Expand Down
2 changes: 1 addition & 1 deletion src/arch/MovieTexture/MovieTexture_Generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ MovieTexture_Generic::UpdateFrame()
static EffectMode EffectModes[] = {
EffectMode_YUYV422,
};
COMPILE_ASSERT(ARRAYLEN(EffectModes) == NUM_PixelFormatYCbCr);
static_assert(ARRAYLEN(EffectModes) == NUM_PixelFormatYCbCr);

EffectMode
MovieTexture_Generic::GetEffectMode(MovieDecoderPixelFormatYCbCr fmt)
Expand Down

0 comments on commit 2d57161

Please sign in to comment.