Debian Codegraph @

override/gst/gst_extra_def.hpp

raw symbols SCIP
1 #ifndef _GI_GST_GST_EXTRA_DEF_HPP_
2 #define _GI_GST_GST_EXTRA_DEF_HPP_
3
4 #include <ios>
5 #include <iostream>
6 #include <iterator>
7 #include <limits>
8
9 namespace gi
10 {
11 namespace repository
12 {
13 namespace Gst
14 {
15 // connection helpers
16 namespace internal
17 {
18 class PadProbeConnection : public detail::connection_impl
19 {
20 public:
21 // using connection_impl::connection_impl;
22 PadProbeConnection(gulong id, detail::connection_status s, Pad pad)
23 : connection_impl(id, s), pad_(pad)
24 {}
25
26 void disconnect() { pad_.remove_probe(id_); }
27
28 private:
29 Pad pad_;
30 };
31
32 template<typename T, typename LimitT = T>
33 struct Range
34 {
35 T min;
36 T max;
37
38 explicit constexpr Range(T _min = std::numeric_limits<LimitT>::min(),
39 T _max = std::numeric_limits<LimitT>::max())
40 : min(_min), max(_max)
41 {}
42 constexpr bool operator==(const Range &other)
43 {
44 return min == other.min && max == other.max;
45 }
46 constexpr bool operator!=(const Range &other) { return !(*this == other); }
47 };
48
49 template<typename T>
50 struct StepRange : public Range<T>
51 {
52 T step;
53
54 explicit constexpr StepRange(T _min, T _max, T _step = 1)
55 : Range<T>(_min, _max), step(_step)
56 {}
57 constexpr bool operator==(const StepRange &other)
58 {
59 return Range<T>::operator==(*this, other) && step == other.step;
60 }
61 constexpr bool operator!=(const StepRange &other)
62 {
63 return !(*this == other);
64 }
65 };
66
67 class ios_flags_saver
68 {
69 std::ostream &out;
70 std::ios old;
71
72 public:
73 ios_flags_saver(std::ostream &_out) : out(_out), old(nullptr)
74 {
75 old.copyfmt(out);
76 }
77 ~ios_flags_saver() { out.copyfmt(old); }
78 };
79
80 template<typename T, typename U>
81 inline std::ostream &
82 operator<<(std::ostream &out, const Range<T, U> &v)
83 {
84 return out << "Range(" << v.min << ", " << v.max << ')';
85 }
86
87 template<typename T>
88 inline std::ostream &
89 operator<<(std::ostream &out, const StepRange<T> &v)
90 {
91 return out << "Range(" << v.min << ", " << v.max << ", " << v.step << ')';
92 }
93
94 } // namespace internal
95
96 using PadProbeConnection = detail::connection<internal::PadProbeConnection>;
97 using PadProbeScopedConnection = detail::scoped_connection<PadProbeConnection>;
98
99 // make Rank a more convenient numeric
100 GI_ENUM_NUMERIC(Rank)
101
102 // iterator helper
103 template<typename T>
104 class IteratorAdapter
105 {
106 Iterator it_;
107
108 public:
109 IteratorAdapter(Iterator _it) : it_(std::move(_it)) {}
110
111 class iterator
112 {
113 private:
114 Iterator_Ref it_;
115 GObject::Value val_;
116 T value_{};
117 IteratorResult result_{IteratorResult::DONE_};
118
119 public:
120 // traits
121 typedef T difference_type;
122 typedef T value_type;
123 typedef T *pointer;
124 typedef T &reference;
125 typedef std::input_iterator_tag category;
126
127 iterator() {}
128 iterator(Iterator_Ref _it) : it_(_it) { ++(*this); }
129 // only prefix required
130 iterator &operator++()
131 {
132 if (it_) {
133 val_.unset();
134 result_ = it_.next(val_);
135 value_ = val_.get_value<T>();
136 // clear iterator when done to ensure end() comparison
137 if (result_ == IteratorResult::DONE_)
138 it_ = nullptr;
139 }
140 return *this;
141 }
142 T &operator*()
143 {
144 if (result_ != IteratorResult::OK_ && result_ != IteratorResult::DONE_)
145 detail::try_throw(std::runtime_error(
146 "GstIterator error result " + std::to_string((int)result_)));
147 return value_;
148 }
149 void operator->() { return &value_; }
150 bool operator==(const iterator &other) const
151 {
152 return other.it_ == it_ && other.result_ == result_;
153 }
154 bool operator!=(const iterator &other) const { return !(*this == other); }
155 };
156
157 iterator begin() const { return iterator(it_); }
158 iterator end() const { return iterator(); }
159 };
160
161 // GstValue helpers
162
163 struct Bitmask
164 {
165 guint64 mask;
166 constexpr Bitmask(guint64 _mask = 0) : mask(_mask) {}
167 constexpr operator guint64() const { return mask; }
168 };
169
170 inline std::ostream &
171 operator<<(std::ostream &out, const Bitmask &v)
172 {
173 internal::ios_flags_saver s(out);
174 return out << std::showbase << std::hex << "Bitmask(" << (guint64)v << ')';
175 }
176
177 struct FlagSet
178 {
179 guint flags;
180 guint mask;
181 explicit constexpr FlagSet(guint _flags, guint _mask)
182 : flags(_flags), mask(_mask)
183 {}
184 constexpr bool operator==(const FlagSet &other)
185 {
186 return flags == other.flags && mask == other.mask;
187 }
188 constexpr bool operator!=(const FlagSet &other) { return !(*this == other); }
189 };
190
191 inline std::ostream &
192 operator<<(std::ostream &out, const FlagSet &v)
193 {
194 internal::ios_flags_saver s(out);
195 return out << std::showbase << std::hex << "FlagSet(" << v.flags << " , "
196 << v.mask << ')';
197 }
198
199 struct Fraction
200 {
201 gint num;
202 gint den;
203
204 constexpr Fraction(gint _num = 0, gint _den = 1) : num(_num), den(_den) {}
205
206 constexpr bool operator==(const Fraction &other)
207 {
208 return num == other.num && den == other.den;
209 }
210 constexpr bool operator!=(const Fraction &other) { return !(*this == other); }
211
212 Fraction operator*(const Fraction &other)
213 {
214 Fraction result;
215 if (gst_util_fraction_multiply(
216 num, den, other.num, other.den, &result.num, &result.den)) {
217 return result;
218 } else {
219 detail::try_throw(std::overflow_error("multiplying Fraction"));
220 }
221 }
222 Fraction operator/(const Fraction &other)
223 {
224 return *this * Fraction(other.den, other.num);
225 }
226 Fraction &operator*=(const Fraction &other)
227 {
228 *this = *this * other;
229 return *this;
230 }
231 Fraction &operator/=(const Fraction &other)
232 {
233 *this = *this / other;
234 return *this;
235 }
236
237 Fraction operator+(const Fraction &other)
238 {
239 Fraction result;
240 if (gst_util_fraction_add(
241 num, den, other.num, other.den, &result.num, &result.den)) {
242 return result;
243 } else {
244 detail::try_throw(std::overflow_error("adding Fraction"));
245 }
246 }
247 Fraction operator-(const Fraction &other)
248 {
249 return *this * Fraction(-other.num, other.den);
250 }
251 Fraction &operator+=(const Fraction &other)
252 {
253 *this = *this + other;
254 return *this;
255 }
256 Fraction &operator-=(const Fraction &other)
257 {
258 *this = *this - other;
259 return *this;
260 }
261 };
262
263 inline std::ostream &
264 operator<<(std::ostream &out, const Fraction &v)
265 {
266 return out << "Fraction(" << v.num << ", " << v.den << ')';
267 }
268
269 using DoubleRange = internal::Range<double>;
270 using IntRange = internal::StepRange<int>;
271 using Int64Range = internal::StepRange<gint64>;
272 using FractionRange = internal::Range<Fraction, int>;
273
274 } // namespace Gst
275
276 template<>
277 struct declare_gtype_of<Gst::Bitmask>
278 {
279 static GType get_type() { return GST_TYPE_BITMASK; }
280 static Gst::Bitmask get_value(const GValue *val)
281 {
282 return Gst::Bitmask(gst_value_get_bitmask(val));
283 }
284 static void set_value(GValue *val, const Gst::Bitmask &r)
285 {
286 gst_value_set_bitmask(val, r);
287 }
288 };
289
290 template<>
291 struct declare_gtype_of<Gst::FlagSet>
292 {
293 static GType get_type() { return GST_TYPE_FLAG_SET; }
294 static Gst::FlagSet get_value(const GValue *val)
295 {
296 return Gst::FlagSet(
297 gst_value_get_flagset_flags(val), gst_value_get_flagset_mask(val));
298 }
299 static void set_value(GValue *val, const Gst::FlagSet &r)
300 {
301 gst_value_set_flagset(val, r.flags, r.mask);
302 }
303 };
304
305 template<>
306 struct declare_gtype_of<Gst::Fraction>
307 {
308 static GType get_type() { return GST_TYPE_FRACTION; }
309 static Gst::Fraction get_value(const GValue *val)
310 {
311 return Gst::Fraction(gst_value_get_fraction_numerator(val),
312 gst_value_get_fraction_denominator(val));
313 }
314 static void set_value(GValue *val, const Gst::Fraction &f)
315 {
316 gst_value_set_fraction(val, f.num, f.den);
317 }
318 };
319
320 template<>
321 struct declare_gtype_of<Gst::FractionRange>
322 {
323 static GType get_type() { return GST_TYPE_FRACTION_RANGE; }
324 static Gst::FractionRange get_value(const GValue *val)
325 {
326 const GValue *vmin = gst_value_get_fraction_range_min(val);
327 const GValue *vmax = gst_value_get_fraction_range_max(val);
328 return Gst::FractionRange(
329 Gst::Fraction(gst_value_get_fraction_numerator(vmin),
330 gst_value_get_fraction_denominator(vmin)),
331 Gst::Fraction(gst_value_get_fraction_numerator(vmax),
332 gst_value_get_fraction_denominator(vmax)));
333 }
334 static void set_value(GValue *val, const Gst::FractionRange &f)
335 {
336 gst_value_set_fraction_range_full(
337 val, f.min.num, f.min.den, f.max.num, f.max.den);
338 }
339 };
340
341 template<>
342 struct declare_gtype_of<Gst::DoubleRange>
343 {
344 static GType get_type() { return GST_TYPE_DOUBLE_RANGE; }
345 static Gst::DoubleRange get_value(const GValue *val)
346 {
347 return Gst::DoubleRange(gst_value_get_double_range_min(val),
348 gst_value_get_double_range_max(val));
349 }
350 static void set_value(GValue *val, const Gst::DoubleRange &r)
351 {
352 gst_value_set_double_range(val, r.min, r.max);
353 }
354 };
355
356 template<>
357 struct declare_gtype_of<Gst::IntRange>
358 {
359 static GType get_type() { return GST_TYPE_INT_RANGE; }
360 static Gst::IntRange get_value(const GValue *val)
361 {
362 return Gst::IntRange(gst_value_get_int_range_min(val),
363 gst_value_get_int_range_max(val), gst_value_get_int_range_step(val));
364 }
365 static void set_value(GValue *val, const Gst::IntRange &r)
366 {
367 gst_value_set_int_range_step(val, r.min, r.max, r.step);
368 }
369 };
370
371 template<>
372 struct declare_gtype_of<Gst::Int64Range>
373 {
374 static GType get_type() { return GST_TYPE_INT64_RANGE; }
375 static Gst::Int64Range get_value(const GValue *val)
376 {
377 return Gst::Int64Range(gst_value_get_int64_range_min(val),
378 gst_value_get_int64_range_max(val),
379 gst_value_get_int64_range_step(val));
380 }
381 static void set_value(GValue *val, const Gst::Int64Range &r)
382 {
383 gst_value_set_int64_range_step(val, r.min, r.max, r.step);
384 }
385 };
386
387 } // namespace repository
388
389 inline repository::Gst::PadProbeConnection
390 make_connection(gulong id, const repository::GLib::SourceFunc &func,
391 repository::Gst::Pad pad)
392 {
393 return repository::Gst::PadProbeConnection(id, func.connection(), pad);
394 }
395
396 } // namespace gi
397
398 #endif