1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/*
* Copyright © 2019 Christian Persch
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cassert>
#include <functional>
#include <memory>
#include <glib.h>
#include "cxx-utils.hh"
#include "std-glue.hh"
namespace vte::glib {
template<typename T>
using FreePtr = vte::FreeablePtr<T, decltype(&g_free), &g_free>;
template<typename T>
FreePtr<T>
take_free_ptr(T* ptr)
{
return FreePtr<T>{ptr};
}
using StringPtr = FreePtr<char>;
using StringGetter = vte::ValueGetter<StringPtr, char*, nullptr>;
inline StringPtr
take_string(char* str)
{
return take_free_ptr(str);
}
inline StringPtr
dup_string(char const* str)
{
return take_string(g_strdup(str));
}
using StrvPtr = vte::FreeablePtr<char*, decltype(&g_strfreev), &g_strfreev>;
using StrvGetter = vte::ValueGetter<StrvPtr, char**, nullptr>;
inline StrvPtr
take_strv(char** strv)
{
return StrvPtr{strv};
}
inline StrvPtr
dup_strv(char const* const* strv)
{
return take_strv(g_strdupv(const_cast<char**>(strv)));
}
class Error {
public:
Error() noexcept = default;
~Error() noexcept { reset(); }
Error(Error const&) = delete;
Error(Error&&) = delete;
Error& operator=(Error const&) = delete;
Error& operator=(Error&&) = delete;
operator GError* () noexcept { return m_error; }
operator GError** () noexcept { return &m_error; }
auto error() const noexcept { return m_error != nullptr; }
auto domain() const noexcept { return error() ? m_error->domain : 0; }
auto code() const noexcept { return error() ? m_error->code : -1; }
auto message() const noexcept { return error() ? m_error->message : nullptr; }
void assert_no_error() const noexcept { g_assert_no_error(m_error); }
G_GNUC_PRINTF(4, 5)
void set(GQuark domain,
int code,
char const* format,
...)
{
va_list args;
va_start(args, format);
g_propagate_error(&m_error, g_error_new_valist(domain, code, format, args));
va_end(args);
}
void set_literal(GQuark domain,
int code,
char const* msg)
{
g_propagate_error(&m_error, g_error_new_literal(domain, code, msg));
}
bool matches(GQuark domain, int code) const noexcept
{
return error() && g_error_matches(m_error, domain, code);
}
void reset() noexcept { g_clear_error(&m_error); }
GError* release() noexcept { auto err = m_error; m_error = nullptr; return err; }
bool propagate(GError** error) noexcept { g_propagate_error(error, release()); return false; }
private:
GError* m_error{nullptr};
};
class Timer {
public:
/* If the callback returns true, the timer is repeated; if the
* callback returns false, the timer is removed.
*/
using callback_type = std::function<bool()>;
Timer(callback_type callback,
char const* name)
: m_callback(callback)
#if VTE_DEBUG
, m_name(name)
#endif
{
}
~Timer() noexcept
{
abort();
}
Timer(Timer const&) = delete;
Timer(Timer&&) = delete;
Timer& operator=(Timer const&) = delete;
Timer& operator=(Timer&&) = delete;
constexpr operator bool() const noexcept { return m_source_id != 0; }
class Priority {
public:
enum {
eHIGH = G_PRIORITY_HIGH,
eDEFAULT = G_PRIORITY_DEFAULT,
eHIGH_IDLE = G_PRIORITY_HIGH_IDLE,
eDEFAULT_IDLE = G_PRIORITY_DEFAULT_IDLE,
eLOW = G_PRIORITY_LOW,
};
};
void schedule(unsigned int timeout,
int priority = Priority::eDEFAULT) noexcept
{
abort();
m_source_id = g_timeout_add_full(priority,
timeout,
s_dispatch_timer_cb,
this,
s_destroy_timer_cb);
set_source_name();
}
void schedule_seconds(unsigned int timeout,
int priority = Priority::eDEFAULT) noexcept
{
abort();
m_source_id = g_timeout_add_seconds_full(priority,
timeout,
s_dispatch_timer_cb,
this,
s_destroy_timer_cb);
set_source_name();
}
void schedule_idle(int priority = Priority::eDEFAULT) noexcept
{
abort();
m_source_id = g_idle_add_full(priority,
s_dispatch_timer_cb,
this,
s_destroy_timer_cb);
set_source_name();
}
void abort() noexcept
{
if (m_source_id != 0) {
g_source_remove(m_source_id);
m_source_id = 0;
}
m_rescheduled = false;
}
private:
callback_type m_callback{};
#if VTE_DEBUG
char const* m_name{nullptr};
#endif
guint m_source_id{0};
bool m_rescheduled{false};
bool dispatch() noexcept
{
auto const id = m_source_id;
auto rv = false;
try {
rv = m_callback();
} catch (...) {
vte::log_exception();
}
/* The Timer may have been re-scheduled or removed from within
* the callback. In this case, the callback must return false!
* m_source_id is now different (since the old source
* ID is still associated with the main context until we return from
* this function), after which invalidate_source() will be called,
* but must not overwrite m_source_id.
* In the non-rescheduled case, invalidate_source() must set
* m_source_id to 0.
*/
m_rescheduled = id != m_source_id;
assert(!m_rescheduled || rv == false);
return rv;
}
inline void set_source_name() const noexcept
{
#if VTE_DEBUG
g_source_set_name_by_id(m_source_id, m_name);
#endif
}
static gboolean s_dispatch_timer_cb(void* data) noexcept
{
auto timer = reinterpret_cast<Timer*>(data);
return timer->dispatch();
}
void invalidate_source() noexcept
{
if (!m_rescheduled)
m_source_id = 0;
m_rescheduled = false;
}
static void s_destroy_timer_cb(void* data) noexcept
{
auto timer = reinterpret_cast<Timer*>(data);
timer->invalidate_source();
}
};
bool set_error_from_exception(GError** error
#if VTE_DEBUG
, char const* func = __builtin_FUNCTION()
, char const* filename = __builtin_FILE()
, int const line = __builtin_LINE()
#endif
) noexcept;
} // namespace vte::glib
namespace vte {
VTE_DECLARE_FREEABLE(GArray, g_array_unref);
VTE_DECLARE_FREEABLE(GBytes, g_bytes_unref);
VTE_DECLARE_FREEABLE(GOptionContext, g_option_context_free);
VTE_DECLARE_FREEABLE(GString, g_autoptr_cleanup_gstring_free);
VTE_DECLARE_FREEABLE(GVariant, g_variant_unref);
} // namespace vte
namespace vte::glib {
inline char*
release_to_string(vte::Freeable<GString> str,
gsize* length = nullptr) noexcept
{
if (length)
*length = str.get()->len;
return g_string_free(str.release(), false);
}
} // namespace vte::glib
|