Debian Codegraph @

terps/bocfel/util.h

raw symbols SCIP
1 // vim: set ft=cpp:
2
3 #ifndef ZTERP_UTIL_H
4 #define ZTERP_UTIL_H
5
6 #include <cstdarg>
7 #include <functional>
8 #include <memory>
9 #include <string>
10 #include <type_traits>
11
12 #include "types.h"
13
14 #if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))
15 #if defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
16 // Gcc appears to default to ms_printf on MinGW, even when it is
17 // providing standards-conforming printf() functionality (i.e. if
18 // __USE_MINGW_ANSI_STDIO is defined), so force gnu_printf there.
19 #define zprintflike(f, a) __attribute__((__format__(__gnu_printf__, f, a)))
20 #else
21 #define zprintflike(f, a) __attribute__((__format__(__printf__, f, a)))
22 #endif
23 #else
24 #define zprintflike(f, a)
25 #endif
26
27 // Allow scoped enums to be used as hash keys.
28 // NOTE: Remove when switching to C++17, as C++17 provides this.
29 struct EnumClassHash {
30 template <typename T>
31 std::enable_if_t<std::is_enum<T>::value, std::size_t>
32 operator()(T enumValue) const {
33 using UnderlyingType = std::underlying_type_t<T>;
34 return std::hash<UnderlyingType>{}(static_cast<UnderlyingType>(enumValue));
35 }
36 };
37
38 int16_t as_signed(uint16_t n);
39
40 #ifndef ZTERP_NO_SAFETY_CHECKS
41 [[noreturn]]
42 zprintflike(1, 2)
43 void assert_fail(const char *fmt, ...);
44 #define ZASSERT(expr, ...) do { if (!(expr)) assert_fail(__VA_ARGS__); } while (false)
45 #else
46 #define ZASSERT(expr, ...) ((void)0)
47 #endif
48
49 zprintflike(1, 2)
50 void warning(const char *fmt, ...);
51
52 [[noreturn]]
53 zprintflike(1, 2)
54 void die(const char *fmt, ...);
55 void help();
56
57 long parseint(const std::string &s, int base, bool &valid);
58 std::string vstring(const char *fmt, std::va_list ap);
59 zprintflike(1, 2)
60 std::string fstring(const char *fmt, ...);
61 std::string ltrim(const std::string &s);
62 std::string rtrim(const std::string &s);
63 void parse_grouped_file(std::ifstream &f, const std::function<void(const std::string &line, int lineno)> &callback);
64 std::unique_ptr<std::string> zterp_getenv(const std::string &name);
65
66 #endif