Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
exception.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#pragma once
9
10#include <fmt/format.h>
11
12#include <cstdio>
13#include <cstdlib>
14#include <stdexcept>
15#include <type_traits>
16
24#define MT_THROW_T(Exception, ...) ::momentum::detail::throwImpl<Exception>(__VA_ARGS__)
25
31#define MT_THROW(...) MT_THROW_T(::momentum::detail::DefaultException, ##__VA_ARGS__)
32
42#define MT_THROW_IF_T(Condition, Exception, ...) \
43 do { \
44 if (Condition) { \
45 MT_THROW_T(Exception, ##__VA_ARGS__); \
46 } \
47 } while (0)
48
55#define MT_THROW_IF(Condition, ...) \
56 MT_THROW_IF_T(Condition, ::momentum::detail::DefaultException, ##__VA_ARGS__)
57
59
60using DefaultException = std::runtime_error;
61
62#if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
63
64template <typename Exception = DefaultException, typename... Args>
65[[noreturn]] void throwImpl(fmt::format_string<Args...> format, Args&&... args) {
66 throw Exception{fmt::format(format, std::forward<Args>(args)...)};
67}
68
73template <typename Exception>
74[[noreturn]] void throwImpl() {
75 if constexpr (std::is_constructible_v<Exception>) {
76 throw Exception{};
77 } else {
78 throw Exception{""};
79 }
80}
81
82#else
83
85template <typename Exception = DefaultException, typename... Args>
86[[noreturn]] void throwImpl(fmt::format_string<Args...> format, Args&&... args) {
87 std::fprintf(
88 stderr, "FATAL ERROR: %s\n", fmt::format(format, std::forward<Args>(args)...).c_str());
89 std::abort();
90}
91
92template <typename Exception>
93[[noreturn]] void throwImpl() {
94 std::fputs("FATAL ERROR (no message)\n", stderr);
95 std::abort();
96}
97
98#endif
99
100} // namespace momentum::detail
std::runtime_error DefaultException
Definition exception.h:60