From 9a0ac7c7fd06a60f0e61b88828eaa59c0ee30fdd Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 2 Dec 2022 03:53:23 +0100 Subject: serialize: work on recovering from corrupted saves --- compat/exception.hpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 compat/exception.hpp (limited to 'compat') diff --git a/compat/exception.hpp b/compat/exception.hpp new file mode 100644 index 00000000..d0d2ca31 --- /dev/null +++ b/compat/exception.hpp @@ -0,0 +1,49 @@ +#pragma once +#include "compat/format.hpp" +#include +#include +#include + +namespace floormat { + +struct base_exception {}; + +struct exception : std::exception, base_exception +{ + template + exception(const Fmt& fmt, Ts&&... args) noexcept; + + const char* what() const noexcept override; + +private: + fmt::memory_buffer buf; +}; + +template +exception::exception(const Fmt& fmt, Ts&&... args) noexcept +{ + fmt::format_to(std::back_inserter(buf), fmt, std::forward(args)...); + buf.push_back('\0'); +} + +} // namespace floormat + +#define fm_soft_assert(...) \ + do { \ + if (!(__VA_ARGS__)) \ + { \ + if (std::is_constant_evaluated()) \ + throw base_exception{}; \ + else \ + throw exception{"assertion failed: {} in {}:{}"_cf, \ + #__VA_ARGS__, __FILE__, __LINE__}; \ + } \ + } while (false) + +#define fm_throw(fmt, ...) \ + do { \ + if (std::is_constant_evaluated()) \ + throw base_exception{}; \ + else \ + throw exception{fmt, __VA_ARGS__}; \ + } while (false) -- cgit v1.2.3