ff173c812af37b2bbb4092afe4a5b840468903a9
[staging/windowmanager.git] / src / util.hpp
1 /*
2  * Copyright (C) 2017 Mentor Graphics Development (Deutschland) GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef WM_UTIL_HPP
18 #define WM_UTIL_HPP
19
20 #include <functional>
21 #include <thread>
22 #include <vector>
23
24 #include <sys/poll.h>
25
26 extern "C" {
27 #include <afb/afb-binding.h>
28 };
29
30 #ifdef __GNUC__
31 #define ATTR_FORMAT(stringindex, firsttocheck) \
32    __attribute__((format(printf, stringindex, firsttocheck)))
33 #define ATTR_NORETURN __attribute__((noreturn))
34 #else
35 #define ATTR_FORMAT(stringindex, firsttocheck)
36 #define ATTR_NORETURN
37 #endif
38
39 #define lognotice(...) AFB_NOTICE(__VA_ARGS__)
40 #define logerror(...) AFB_ERROR(__VA_ARGS__)
41 #define fatal(...)            \
42    do {                       \
43       AFB_ERROR(__VA_ARGS__); \
44       abort();                \
45    } while (0)
46
47 #ifdef DEBUG_OUTPUT
48 #define logdebug(...) AFB_DEBUG(__VA_ARGS__)
49 #else
50 #define logdebug(...)
51 #endif
52
53 #ifdef NDEBUG
54 #define ST()
55 #define STN(N)
56 #else
57 #define CONCAT_(X, Y) X##Y
58 #define CONCAT(X, Y) CONCAT_(X, Y)
59
60 #define ST() \
61     ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
62 #define STN(N) \
63     ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
64
65 struct ScopeTrace {
66    thread_local static int indent;
67    char const *f{};
68    explicit ScopeTrace(char const *func) : f(func) {
69       fprintf(stderr, "%lu %*s%s -->\n", pthread_self(), 2 * indent++, "", this->f);
70    }
71    ~ScopeTrace() { fprintf(stderr, "%lu %*s%s <--\n", pthread_self(), 2 * --indent, "", this->f); }
72 };
73 #endif
74
75 //      _                   _                 _                       __     _
76 //  ___| |_ _ __ _   _  ___| |_   _   _ _ __ (_) __ _ _   _  ___     / _| __| |
77 // / __| __| '__| | | |/ __| __| | | | | '_ \| |/ _` | | | |/ _ \   | |_ / _` |
78 // \__ \ |_| |  | |_| | (__| |_  | |_| | | | | | (_| | |_| |  __/   |  _| (_| |
79 // |___/\__|_|   \__,_|\___|\__|  \__,_|_| |_|_|\__, |\__,_|\___|___|_|  \__,_|
80 //                                                 |_|         |_____|
81 struct unique_fd {
82    int fd{-1};
83    unique_fd() = default;
84    explicit unique_fd(int f) : fd{f} {}
85    operator int() const { return fd; }
86    ~unique_fd();
87    unique_fd(unique_fd const &) = delete;
88    unique_fd &operator=(unique_fd const &) = delete;
89    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
90    unique_fd &operator=(unique_fd &&o) {
91       std::swap(this->fd, o.fd);
92       return *this;
93    }
94 };
95
96 #endif  // !WM_UTIL_HPP