File: gst_extra_def.hpp

package info (click to toggle)
cppgir 2.0%2Bgit20240928.c8bb1c6%2Breally2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,228 kB
  • sloc: cpp: 14,307; ansic: 339; makefile: 11; sh: 9
file content (398 lines) | stat: -rw-r--r-- 9,837 bytes parent folder | download | duplicates (2)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#ifndef _GI_GST_GST_EXTRA_DEF_HPP_
#define _GI_GST_GST_EXTRA_DEF_HPP_

#include <ios>
#include <iostream>
#include <iterator>
#include <limits>

namespace gi
{
namespace repository
{
namespace Gst
{
// connection helpers
namespace internal
{
class PadProbeConnection : public detail::connection_impl
{
public:
  // using connection_impl::connection_impl;
  PadProbeConnection(gulong id, detail::connection_status s, Pad pad)
      : connection_impl(id, s), pad_(pad)
  {}

  void disconnect() { pad_.remove_probe(id_); }

private:
  Pad pad_;
};

template<typename T, typename LimitT = T>
struct Range
{
  T min;
  T max;

  explicit constexpr Range(T _min = std::numeric_limits<LimitT>::min(),
      T _max = std::numeric_limits<LimitT>::max())
      : min(_min), max(_max)
  {}
  constexpr bool operator==(const Range &other)
  {
    return min == other.min && max == other.max;
  }
  constexpr bool operator!=(const Range &other) { return !(*this == other); }
};

template<typename T>
struct StepRange : public Range<T>
{
  T step;

  explicit constexpr StepRange(T _min, T _max, T _step = 1)
      : Range<T>(_min, _max), step(_step)
  {}
  constexpr bool operator==(const StepRange &other)
  {
    return Range<T>::operator==(*this, other) && step == other.step;
  }
  constexpr bool operator!=(const StepRange &other)
  {
    return !(*this == other);
  }
};

class ios_flags_saver
{
  std::ostream &out;
  std::ios old;

public:
  ios_flags_saver(std::ostream &_out) : out(_out), old(nullptr)
  {
    old.copyfmt(out);
  }
  ~ios_flags_saver() { out.copyfmt(old); }
};

template<typename T, typename U>
inline std::ostream &
operator<<(std::ostream &out, const Range<T, U> &v)
{
  return out << "Range(" << v.min << ", " << v.max << ')';
}

template<typename T>
inline std::ostream &
operator<<(std::ostream &out, const StepRange<T> &v)
{
  return out << "Range(" << v.min << ", " << v.max << ", " << v.step << ')';
}

} // namespace internal

using PadProbeConnection = detail::connection<internal::PadProbeConnection>;
using PadProbeScopedConnection = detail::scoped_connection<PadProbeConnection>;

// make Rank a more convenient numeric
GI_ENUM_NUMERIC(Rank)

// iterator helper
template<typename T>
class IteratorAdapter
{
  Iterator it_;

public:
  IteratorAdapter(Iterator _it) : it_(std::move(_it)) {}

  class iterator
  {
  private:
    Iterator_Ref it_;
    GObject::Value val_;
    T value_{};
    IteratorResult result_{IteratorResult::DONE_};

  public:
    // traits
    typedef T difference_type;
    typedef T value_type;
    typedef T *pointer;
    typedef T &reference;
    typedef std::input_iterator_tag category;

    iterator() {}
    iterator(Iterator_Ref _it) : it_(_it) { ++(*this); }
    // only prefix required
    iterator &operator++()
    {
      if (it_) {
        val_.unset();
        result_ = it_.next(val_);
        value_ = val_.get_value<T>();
        // clear iterator when done to ensure end() comparison
        if (result_ == IteratorResult::DONE_)
          it_ = nullptr;
      }
      return *this;
    }
    T &operator*()
    {
      if (result_ != IteratorResult::OK_ && result_ != IteratorResult::DONE_)
        throw std::runtime_error(
            "GstIterator error result " + std::to_string((int)result_));
      return value_;
    }
    void operator->() { return &value_; }
    bool operator==(const iterator &other) const
    {
      return other.it_ == it_ && other.result_ == result_;
    }
    bool operator!=(const iterator &other) const { return !(*this == other); }
  };

  iterator begin() const { return iterator(it_); }
  iterator end() const { return iterator(); }
};

// GstValue helpers

struct Bitmask
{
  guint64 mask;
  constexpr Bitmask(guint64 _mask = 0) : mask(_mask) {}
  constexpr operator guint64() const { return mask; }
};

inline std::ostream &
operator<<(std::ostream &out, const Bitmask &v)
{
  internal::ios_flags_saver s(out);
  return out << std::showbase << std::hex << "Bitmask(" << (guint64)v << ')';
}

struct FlagSet
{
  guint flags;
  guint mask;
  explicit constexpr FlagSet(guint _flags, guint _mask)
      : flags(_flags), mask(_mask)
  {}
  constexpr bool operator==(const FlagSet &other)
  {
    return flags == other.flags && mask == other.mask;
  }
  constexpr bool operator!=(const FlagSet &other) { return !(*this == other); }
};

inline std::ostream &
operator<<(std::ostream &out, const FlagSet &v)
{
  internal::ios_flags_saver s(out);
  return out << std::showbase << std::hex << "FlagSet(" << v.flags << " , "
             << v.mask << ')';
}

struct Fraction
{
  gint num;
  gint den;

  constexpr Fraction(gint _num = 0, gint _den = 1) : num(_num), den(_den) {}

  constexpr bool operator==(const Fraction &other)
  {
    return num == other.num && den == other.den;
  }
  constexpr bool operator!=(const Fraction &other) { return !(*this == other); }

  Fraction operator*(const Fraction &other)
  {
    Fraction result;
    if (gst_util_fraction_multiply(
            num, den, other.num, other.den, &result.num, &result.den)) {
      return result;
    } else {
      throw std::overflow_error("multiplying Fraction");
    }
  }
  Fraction operator/(const Fraction &other)
  {
    return *this * Fraction(other.den, other.num);
  }
  Fraction &operator*=(const Fraction &other)
  {
    *this = *this * other;
    return *this;
  }
  Fraction &operator/=(const Fraction &other)
  {
    *this = *this / other;
    return *this;
  }

  Fraction operator+(const Fraction &other)
  {
    Fraction result;
    if (gst_util_fraction_add(
            num, den, other.num, other.den, &result.num, &result.den)) {
      return result;
    } else {
      throw std::overflow_error("adding Fraction");
    }
  }
  Fraction operator-(const Fraction &other)
  {
    return *this * Fraction(-other.num, other.den);
  }
  Fraction &operator+=(const Fraction &other)
  {
    *this = *this + other;
    return *this;
  }
  Fraction &operator-=(const Fraction &other)
  {
    *this = *this - other;
    return *this;
  }
};

inline std::ostream &
operator<<(std::ostream &out, const Fraction &v)
{
  return out << "Fraction(" << v.num << ", " << v.den << ')';
}

using DoubleRange = internal::Range<double>;
using IntRange = internal::StepRange<int>;
using Int64Range = internal::StepRange<gint64>;
using FractionRange = internal::Range<Fraction, int>;

} // namespace Gst

template<>
struct declare_gtype_of<Gst::Bitmask>
{
  static GType get_type() { return GST_TYPE_BITMASK; }
  static Gst::Bitmask get_value(const GValue *val)
  {
    return Gst::Bitmask(gst_value_get_bitmask(val));
  }
  static void set_value(GValue *val, const Gst::Bitmask &r)
  {
    gst_value_set_bitmask(val, r);
  }
};

template<>
struct declare_gtype_of<Gst::FlagSet>
{
  static GType get_type() { return GST_TYPE_FLAG_SET; }
  static Gst::FlagSet get_value(const GValue *val)
  {
    return Gst::FlagSet(
        gst_value_get_flagset_flags(val), gst_value_get_flagset_mask(val));
  }
  static void set_value(GValue *val, const Gst::FlagSet &r)
  {
    gst_value_set_flagset(val, r.flags, r.mask);
  }
};

template<>
struct declare_gtype_of<Gst::Fraction>
{
  static GType get_type() { return GST_TYPE_FRACTION; }
  static Gst::Fraction get_value(const GValue *val)
  {
    return Gst::Fraction(gst_value_get_fraction_numerator(val),
        gst_value_get_fraction_denominator(val));
  }
  static void set_value(GValue *val, const Gst::Fraction &f)
  {
    gst_value_set_fraction(val, f.num, f.den);
  }
};

template<>
struct declare_gtype_of<Gst::FractionRange>
{
  static GType get_type() { return GST_TYPE_FRACTION_RANGE; }
  static Gst::FractionRange get_value(const GValue *val)
  {
    const GValue *vmin = gst_value_get_fraction_range_min(val);
    const GValue *vmax = gst_value_get_fraction_range_max(val);
    return Gst::FractionRange(
        Gst::Fraction(gst_value_get_fraction_numerator(vmin),
            gst_value_get_fraction_denominator(vmin)),
        Gst::Fraction(gst_value_get_fraction_numerator(vmax),
            gst_value_get_fraction_denominator(vmax)));
  }
  static void set_value(GValue *val, const Gst::FractionRange &f)
  {
    gst_value_set_fraction_range_full(
        val, f.min.num, f.min.den, f.max.num, f.max.den);
  }
};

template<>
struct declare_gtype_of<Gst::DoubleRange>
{
  static GType get_type() { return GST_TYPE_DOUBLE_RANGE; }
  static Gst::DoubleRange get_value(const GValue *val)
  {
    return Gst::DoubleRange(gst_value_get_double_range_min(val),
        gst_value_get_double_range_max(val));
  }
  static void set_value(GValue *val, const Gst::DoubleRange &r)
  {
    gst_value_set_double_range(val, r.min, r.max);
  }
};

template<>
struct declare_gtype_of<Gst::IntRange>
{
  static GType get_type() { return GST_TYPE_INT_RANGE; }
  static Gst::IntRange get_value(const GValue *val)
  {
    return Gst::IntRange(gst_value_get_int_range_min(val),
        gst_value_get_int_range_max(val), gst_value_get_int_range_step(val));
  }
  static void set_value(GValue *val, const Gst::IntRange &r)
  {
    gst_value_set_int_range_step(val, r.min, r.max, r.step);
  }
};

template<>
struct declare_gtype_of<Gst::Int64Range>
{
  static GType get_type() { return GST_TYPE_INT64_RANGE; }
  static Gst::Int64Range get_value(const GValue *val)
  {
    return Gst::Int64Range(gst_value_get_int64_range_min(val),
        gst_value_get_int64_range_max(val),
        gst_value_get_int64_range_step(val));
  }
  static void set_value(GValue *val, const Gst::Int64Range &r)
  {
    gst_value_set_int64_range_step(val, r.min, r.max, r.step);
  }
};

} // namespace repository

inline repository::Gst::PadProbeConnection
make_connection(gulong id, const repository::GLib::SourceFunc &func,
    repository::Gst::Pad pad)
{
  return repository::Gst::PadProbeConnection(id, func.connection(), pad);
}

} // namespace gi

#endif