MCRO
C++23 utilities for Unreal Engine.
Loading...
Searching...
No Matches
AssertMacros.h File Reference
#include "CoreMinimal.h"
#include "Mcro/Error.h"
#include "Mcro/Macros.h"
#include "Logging/LogMacros.h"

Go to the source code of this file.

Namespaces

namespace  Mcro
 
namespace  Mcro::AssertMacros
 
namespace  Mcro::AssertMacros::Detail
 

Macros

#define PROPAGATE_FAIL(expression)
 
#define MCRO_ASSERT_SUBMIT_ERROR(condition, severity, async, important, ...)
 
#define MCRO_ASSERT_CRASH_METHOD
 
#define MCRO_CRASH_BODY(condition, ...)
 
#define MCRO_QUIT_BODY(condition, returnOnFailure, ...)
 
#define MCRO_ASSERT_CRASH_COMMON(condition, ...)
 
#define MCRO_ASSERT_QUIT_COMMON(condition, returnOnFailure, ...)
 
#define ASSERT_CRASH(condition, ...)
 
#define FORCE_CRASH(...)
 
#define ASSERT_QUIT(condition, returnOnFailure, ...)
 
#define FORCE_QUIT(returnOnFailure, ...)
 

Functions

MCRO_API void Mcro::AssertMacros::Detail::SubmitError (EErrorSeverity severity, FString const &codeContext, bool async, bool important, TUniqueFunction< void(IErrorRef const &)> &&extraSetup)
 
MCRO_API bool Mcro::AssertMacros::Detail::IsRunningPIE ()
 
MCRO_API void Mcro::AssertMacros::Detail::StopPie ()
 

Detailed Description

Author
David Mórász
Date
2025

By default ASSERT_QUIT|CRASH macros are also active in Shipping builds. This was a personal preference and that's why you can turn it off if you define MCRO_ASSERT_IGNORE_SHIPPING.

Remarks
Yes I know that "shipping builds must have all debug info and checks removed" but I have yet to see one project in my career where Shipping builds didn't have weird issues or crashes not present in other debug builds, and debugging them removed many years from my expected life-span.

Definition in file AssertMacros.h.

Macro Definition Documentation

◆ ASSERT_CRASH

#define ASSERT_CRASH ( condition,
... )
Value:
MCRO_ASSERT_CRASH_COMMON(condition, __VA_ARGS__)
#define MCRO_ASSERT_CRASH_COMMON(condition,...)

Use this instead of check macro if the checked expression shouldn't be ignored in shipping builds. This version will also crash the entire editor even during a PIE session. Use this only if there's absolutely no way to recover from an invalid state, like a situation when early return is not possible / doesn't make sense (e.g.: return a checked pointer by reference).

This macro will attempt to display the error before crashing via the IError type. You can also append extra information to it, or even override preset information like so:

ASSERT_CRASH(arrayNum > 0,
->WithDetails(TEXT("Did you forget to specify data?"))
->WithAppendix(TEXT("Data"), data.ToString())
)
#define ASSERT_CRASH(condition,...)

Definition at line 113 of file AssertMacros.h.

◆ ASSERT_QUIT

#define ASSERT_QUIT ( condition,
returnOnFailure,
... )
Value:
MCRO_ASSERT_QUIT_COMMON(condition, returnOnFailure, __VA_ARGS__)
#define MCRO_ASSERT_QUIT_COMMON(condition, returnOnFailure,...)

Use this instead of check macro if the checked expression shouldn't be ignored in shipping builds. This version will not crash the editor but will display an error, stops the PIE, and should return the calling function. A return value should be provided on failure, or leave it empty to return void. so either

ASSERT_QUIT(SomethingImportantButNullable, ) // important to leave an extra comma for void functions
// or return default
ASSERT_QUIT(SomethingImportantButNullable, {})
// or return whatever
ASSERT_QUIT(SomethingImportantButNullable, myReturnValue)
#define ASSERT_QUIT(condition, returnOnFailure,...)

This is done in the hopes that these checks failing in PIE sessions doesn't crash the entire editor giving the developer a chance for fixing the error before restarting. However early returns might cause undefined/vaguely-defined side effects and may lead to uninitialized resources or in worst case scenario may lead to incorrect states in resource cleanups which then might crash the editor. Always check the validity of your resources in destructors. Failing this check outside of PIE or in a Standalone session will still crash with a check macro.

This macro will attempt to display the error before quitting via the IError type. You can also append extra information to it like so, or even override preset information like so:

ASSERT_QUIT(arrayNum > 0, myReturnValue,
->WithDetails(TEXT("Did you forget to specify data?"))
->WithAppendix(TEXT("Data"), data.ToString())
)

Definition at line 147 of file AssertMacros.h.

◆ FORCE_CRASH

#define FORCE_CRASH ( ...)
Value:
MCRO_CRASH_BODY(Invalid code path, __VA_ARGS__)
#define MCRO_CRASH_BODY(condition,...)

This is equivalent to ASSERT_CRASH but if a code path reaches this macro it will always crash

Definition at line 116 of file AssertMacros.h.

◆ FORCE_QUIT

#define FORCE_QUIT ( returnOnFailure,
... )
Value:
MCRO_QUIT_BODY(Invalid code path, returnOnFailure, __VA_ARGS__)
#define MCRO_QUIT_BODY(condition, returnOnFailure,...)

This is equivalent to ASSERT_QUIT but if a code path reaches this macro it will always quit

Definition at line 150 of file AssertMacros.h.

◆ MCRO_ASSERT_CRASH_COMMON

#define MCRO_ASSERT_CRASH_COMMON ( condition,
... )
Value:
if (UNLIKELY(!(condition))) \
{ \
MCRO_CRASH_BODY(condition, __VA_ARGS__) \
}

Definition at line 83 of file AssertMacros.h.

◆ MCRO_ASSERT_CRASH_METHOD

#define MCRO_ASSERT_CRASH_METHOD
Value:
UE_LOG(LogTemp, Fatal, \
TEXT("Program cannot continue for the reasons above. (at ") \
PREPROCESSOR_TO_TEXT(__FILE__:__LINE__) TEXT(")") \
)
#define PREPROCESSOR_TO_TEXT(x)
Definition Macros.h:17

Definition at line 56 of file AssertMacros.h.

◆ MCRO_ASSERT_QUIT_COMMON

#define MCRO_ASSERT_QUIT_COMMON ( condition,
returnOnFailure,
... )
Value:
if (UNLIKELY(!(condition))) \
{ \
MCRO_QUIT_BODY(condition, returnOnFailure, __VA_ARGS__) \
}

Definition at line 89 of file AssertMacros.h.

◆ MCRO_ASSERT_SUBMIT_ERROR

#define MCRO_ASSERT_SUBMIT_ERROR ( condition,
severity,
async,
important,
... )
Value:
Mcro::Error::EErrorSeverity::severity, \
PREPROCESSOR_TO_TEXT(condition), \
async, important, \
[&](Mcro::Error::IErrorRef const& error) { (error __VA_ARGS__); } \
);
MCRO_API void SubmitError(EErrorSeverity severity, FString const &codeContext, bool async, bool important, TUniqueFunction< void(IErrorRef const &)> &&extraSetup)
TSharedRef< IError > IErrorRef
Definition Error.Fwd.h:25

Definition at line 48 of file AssertMacros.h.

◆ MCRO_CRASH_BODY

#define MCRO_CRASH_BODY ( condition,
... )
Value:
condition, Crashing, false, false, \
__VA_ARGS__ \
) \
#define MCRO_ASSERT_CRASH_METHOD
#define MCRO_ASSERT_SUBMIT_ERROR(condition, severity, async, important,...)

Definition at line 62 of file AssertMacros.h.

◆ MCRO_QUIT_BODY

#define MCRO_QUIT_BODY ( condition,
returnOnFailure,
... )
Value:
condition, Fatal, \
__VA_ARGS__ \
) \
{ \
Mcro::AssertMacros::Detail::StopPie(); \
return returnOnFailure; \
} \
MCRO_API bool IsRunningPIE()

Definition at line 69 of file AssertMacros.h.

◆ PROPAGATE_FAIL

#define PROPAGATE_FAIL ( expression)
Value:
PROPAGATE_FAIL_V(PREPROCESSOR_JOIN(tempResult, __LINE__), expression)
#define PROPAGATE_FAIL_V(var, expression)
Definition Error.h:633