Gemmi C++ API
Loading...
Searching...
No Matches
fail.hpp
Go to the documentation of this file.
1// Copyright 2017 Global Phasing Ltd.
2//
3// fail(), unreachable() and __declspec/__attribute__ macros
4
5#ifndef GEMMI_FAIL_HPP_
6#define GEMMI_FAIL_HPP_
7
8#include <cerrno> // for errno
9#include <stdexcept> // for runtime_error
10#include <system_error> // for system_error
11#include <string>
12#include <utility> // for forward
13
14#ifdef __INTEL_COMPILER
15// warning #2196: routine is both "inline" and "noinline"
16# pragma warning disable 2196
17#endif
18#if defined(__GNUG__) && !defined(__clang__)
19# pragma GCC diagnostic push
20# pragma GCC diagnostic ignored "-Wattributes"
21#endif
22
23#if defined(__GNUC__) || defined(__clang__)
24# define GEMMI_COLD __attribute__((cold))
25#elif defined(_MSC_VER)
26# define GEMMI_COLD __declspec(noinline)
27#else
28# define GEMMI_COLD __attribute__((noinline))
29#endif
30
31#if __cplusplus >= 202002L || _MSVC_LANG >= 202002L
32# define GEMMI_LIKELY(x) (x) [[likely]]
33# define GEMMI_UNLIKELY(x) (x) [[unlikely]]
34#elif defined(__GNUC__) || defined(__clang__)
35# define GEMMI_LIKELY(x) (__builtin_expect(!!(x), 1))
36# define GEMMI_UNLIKELY(x) (__builtin_expect(!!(x), 0))
37#else
38# define GEMMI_LIKELY(x) (x)
39# define GEMMI_UNLIKELY(x) (x)
40#endif
41
42#if defined(_WIN32)
43# if defined(GEMMI_SHARED)
44# if defined(GEMMI_BUILD)
45# define GEMMI_DLL __declspec(dllexport)
46# else
47# define GEMMI_DLL __declspec(dllimport)
48# endif // GEMMI_BUILD
49# else
50# define GEMMI_DLL
51# endif // GEMMI_SHARED
52#else
53# define GEMMI_DLL __attribute__((visibility("default")))
54#endif
55
56namespace gemmi {
57
58[[noreturn]]
59inline void fail(const std::string& msg) { throw std::runtime_error(msg); }
60
61template<typename T, typename... Args> [[noreturn]]
62void fail(std::string&& str, T&& arg1, Args&&... args) {
63 str += arg1;
64 fail(std::move(str), std::forward<Args>(args)...);
65}
66
67[[noreturn]]
68inline GEMMI_COLD void fail(const char* msg) { throw std::runtime_error(msg); }
69
70[[noreturn]]
71inline GEMMI_COLD void sys_fail(const std::string& msg) {
72 throw std::system_error(errno, std::system_category(), msg);
73}
74[[noreturn]]
75inline GEMMI_COLD void sys_fail(const char* msg) {
76 throw std::system_error(errno, std::system_category(), msg);
77}
78
79// unreachable() is used to silence GCC -Wreturn-type and hint the compiler
80[[noreturn]] inline void unreachable() {
81#if defined(__GNUC__) || defined(__clang__)
83#elif defined(_MSC_VER)
84 __assume(0);
85#endif
86}
87
88#if defined(__GNUG__) && !defined(__clang__)
89# pragma GCC diagnostic pop
90#endif
91
92} // namespace gemmi
93#endif
#define GEMMI_COLD
Definition fail.hpp:28
GEMMI_COLD void sys_fail(const std::string &msg)
Definition fail.hpp:71
void fail(const std::string &msg)
Definition fail.hpp:59
void unreachable()
Definition fail.hpp:80