Remove and replace the comments
[apps/agl-service-windowmanager-2017.git] / src / util.hpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 #ifndef DO_NOT_USE_AFB
27 extern "C" {
28 #include <afb/afb-binding.h>
29 };
30 #endif
31
32 #define CONCAT_(X, Y) X##Y
33 #define CONCAT(X, Y) CONCAT_(X, Y)
34
35 #ifdef __GNUC__
36 #define ATTR_FORMAT(stringindex, firsttocheck) \
37    __attribute__((format(printf, stringindex, firsttocheck)))
38 #define ATTR_NORETURN __attribute__((noreturn))
39 #else
40 #define ATTR_FORMAT(stringindex, firsttocheck)
41 #define ATTR_NORETURN
42 #endif
43
44 #ifdef AFB_BINDING_VERSION
45 #define lognotice(...) AFB_NOTICE(__VA_ARGS__)
46 #define logerror(...) AFB_ERROR(__VA_ARGS__)
47 #define fatal(...)            \
48    do {                       \
49       AFB_ERROR(__VA_ARGS__); \
50       abort();                \
51    } while (0)
52 #else
53 #define lognotice(...)
54 #define logerror(...)
55 #define fatal(...)            \
56    do {                       \
57       abort();                \
58    } while (0)
59 #endif
60
61 #ifdef DEBUG_OUTPUT
62 #ifdef AFB_BINDING_VERSION
63 #define logdebug(...) AFB_DEBUG(__VA_ARGS__)
64 #else
65 #define logdebug(...)
66 #endif
67 #else
68 #define logdebug(...)
69 #endif
70
71 #ifndef SCOPE_TRACING
72 #define ST()
73 #define STN(N)
74 #else
75 #define ST() \
76     ScopeTrace __attribute__((unused)) CONCAT(trace_scope_, __LINE__)(__func__)
77 #define STN(N) \
78     ScopeTrace __attribute__((unused)) CONCAT(named_trace_scope_, __LINE__)(#N)
79
80 struct ScopeTrace {
81    thread_local static int indent;
82    char const *f{};
83    explicit ScopeTrace(char const *func);
84    ~ScopeTrace();
85 };
86 #endif
87
88 /**
89  * @struct unique_fd
90  */
91 struct unique_fd {
92    int fd{-1};
93    unique_fd() = default;
94    explicit unique_fd(int f) : fd{f} {}
95    operator int() const { return fd; }
96    ~unique_fd();
97    unique_fd(unique_fd const &) = delete;
98    unique_fd &operator=(unique_fd const &) = delete;
99    unique_fd(unique_fd &&o) : fd(o.fd) { o.fd = -1; }
100    unique_fd &operator=(unique_fd &&o) {
101       std::swap(this->fd, o.fd);
102       return *this;
103    }
104 };
105
106 #endif  // !WM_UTIL_HPP