TombEngine/TR5Main/Scripting/ScriptAssert.h
hispidence a75b43b7c5 Implement ScriptAssertF and ScriptAssertTerminateF.
These are like ScriptAssert, but make use of the fmt library (atm hijacking the version that comes with spdlog). This allows us to pass in a format string and some args, and then only create the format string if there's an actual error. This means we won't waste time doing a ton of std::string concatenations if the assert succeeds.

As these are variadic templates, we can't use a std::optional as the last argument to check if we're forcing a different error mode (we possibly could by assuming the last arg is a std::optional, but that seems unclean), so instead I've just separated them out into two functions.
2021-08-21 00:23:14 +01:00

51 lines
1 KiB
C++

#pragma once
#include <string>
#include <optional>
#include <spdlog/fmt/fmt.h>
enum class ERROR_MODE
{
SILENT,
WARN,
TERMINATE
};
void SetScriptErrorMode(ERROR_MODE mode);
ERROR_MODE GetScriptErrorMode();
void ScriptWarn(std::string const& msg);
bool ScriptAssert(bool cond, std::string const& msg, std::optional<ERROR_MODE> forceMode = std::nullopt);
template <typename ... Ts> bool ScriptAssertF(bool cond, std::string_view str, Ts...args)
{
if (!cond)
{
auto msg = fmt::format(str, args...);
auto mode = GetScriptErrorMode();
switch (mode)
{
case ERROR_MODE::WARN:
TENLog(msg, LogLevel::Error, LogConfig::All);
break;
case ERROR_MODE::TERMINATE:
TENLog(msg, LogLevel::Error, LogConfig::All);
throw TENScriptException(msg);
break;
}
}
return cond;
}
template <typename ... Ts> bool ScriptAssertTerminateF(bool cond, std::string_view str, Ts...args)
{
if (!cond)
{
auto msg = fmt::format(str, args...);
TENLog(msg, LogLevel::Error, LogConfig::All);
throw TENScriptException(msg);
}
return cond;
}