util: noexcept logging and also noreturn for fatal()
[staging/windowmanager.git] / src / util.hpp
1 #ifndef WM_UTIL_HPP
2 #define WM_UTIL_HPP
3
4 #include <functional>
5 #include <sys/poll.h>
6 #include <vector>
7
8 #ifdef __GNUC__
9 #define ATTR_FORMAT(stringindex, firsttocheck) \
10    __attribute__((format(printf, stringindex, firsttocheck)))
11 #define ATTR_NORETURN __attribute__((noreturn))
12 #else
13 #define ATTR_FORMAT(stringindex, firsttocheck)
14 #define ATTR_NORETURN
15 #endif
16
17 void lognotice(char const *fmt, ...) noexcept ATTR_FORMAT(1, 2);
18 void logerror(char const *fmt, ...) noexcept ATTR_FORMAT(1, 2);
19 void fatal(char const *fmt, ...) noexcept ATTR_FORMAT(1, 2) ATTR_NORETURN;
20
21 #ifdef DEBUG_OUTPUT
22 void logdebug(char const *fmt, ...) noexcept ATTR_FORMAT(1, 2);
23 #else
24 static inline void logdebug(char const *fmt, ...) noexcept ATTR_FORMAT(1, 2);
25 static inline void logdebug(char const *fmt, ...) noexcept {}
26 #endif
27
28 //      _                   _                 _                       __     _
29 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
30 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |
31 // \__ \ |_| |  | |_| | (__| |_  | |_| | | | | | (_| | |_| |  __/   |  _| (_| |
32 // |___/\__|_|   \__,_|\___|\__|  \__,_|_| |_|_|\__, |\__,_|\___|___|_|  \__,_|
33 //                                                 |_|         |_____|
34 struct unique_fd {
35    int fd{-1};
36    unique_fd() = default;
37    explicit unique_fd(int f) : fd{f} {}
38    operator int() const { return fd; }
39    ~unique_fd();
40    unique_fd(unique_fd const &) = delete;
41    unique_fd &operator=(unique_fd const &) = delete;
42    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
43    unique_fd &operator=(unique_fd &&o) {
44       std::swap(this->fd, o.fd);
45       return *this;
46    }
47 };
48
49 //      _                   _     ____       _ _
50 //  ___| |_ _ __ _   _  ___| |_  |  _ \ ___ | | | ___ _ __
51 // / __| __| '__| | | |/ __| __| | |_) / _ \| | |/ _ \ '__|
52 // \__ \ |_| |  | |_| | (__| |_  |  __/ (_) | | |  __/ |
53 // |___/\__|_|   \__,_|\___|\__| |_|   \___/|_|_|\___|_|
54 //
55 struct Poller {
56    std::vector<std::function<int(int)>> handlers;
57    std::vector<struct pollfd> pfds;
58
59    Poller() = default;
60    void add_fd(int fd, std::function<int(int)> handler);
61    int check_events();
62 };
63
64 #endif  // !WM_UTIL_HPP