Debian Codegraph @

gstreamer/src/iterator.hg

raw
1 /* gstreamermm - a C++ wrapper for gstreamer
2 *
3 * Copyright 2008 The gstreamermm Development Team
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free
17 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <gst/gst.h>
21 #include <gstreamermm/handle_error.h>
22
23 _DEFS(gstreamermm,gst)
24
25 namespace Gst
26 {
27
28 _WRAP_ENUM(IteratorItem, GstIteratorItem)
29 _WRAP_ENUM(IteratorResult, GstIteratorResult)
30
31 /** The base class for classes that retrieve multiple elements in a thread
32 * safe way.
33 *
34 * Classes derived from Gst::IteratorBase are used to retrieve multiple objects
35 * from another object in a thread safe way. They are implemented as C++ like
36 * input iterators so they do not support multi-passing, but they are usable
37 * enough for iterating through a list of items and referencing them in a
38 * single pass.
39 *
40 * Various GStreamer objects provide access to their internal structures using
41 * an iterator.
42 */
43 template <class CppType>
44 class IteratorBase
45 {
46 public:
47 /** Moves to the next iterator item.
48 *
49 * @return The result of the iteration. MT safe.
50 */
51 virtual IteratorResult next();
52
53 /** Resynchronize the iterator. This function is mostly called after next()
54 * returns Gst::ITERATOR_RESYNC. A result of Gst::ITERATOR_RESYNC from
55 * next() means that a concurrent update was made to the iterator list during
56 * iteration and the iterator needs to be resynchronized before continuing.
57 * Use this function to resynchronize.
58 */
59 void resync();
60
61 /** Tells if the iterator is at the start of the list (not on the first item,
62 * but just before it). Increment the iterator or use
63 * Gst::IteratorBasic::begin() to go to the first item.
64 *
65 * @return true if the iterator is at the start of the list, false otherwise.
66 */
67 bool is_start() const;
68
69 /** Tells if the iterator is at the end of the list (just after the last
70 * element).
71 *
72 * @return true if the iterator is at the end of the list, false otherwise.
73 */
74 bool is_end() const;
75
76 /** Tells whether the iterator is valid and can be dereferenced.
77 */
78 operator bool() const;
79
80 ///Provides access to the underlying C GObject.
81 GstIterator* cobj() { return cobject_; };
82
83 ///Provides access to the underlying C GObject.
84 const GstIterator* cobj() const { return cobject_; };
85
86 /** Frees the underlying C instance if a take_ownership value of true was
87 * used to wrap it.
88 */
89 virtual ~IteratorBase();
90
91 protected:
92 /// Default constructor.
93 IteratorBase();
94
95 /** Copy constructor. Please note that copying and assigning merely shares
96 * the underlying C object. Operations on the copy are also performed in the
97 * underlying C object of the original and if the original is destroyed, the
98 * copy is invalid.
99 */
100 IteratorBase(const IteratorBase<CppType>&);
101
102 /** Constructs an IteratorBase from an underlying C object.
103 * @param castitem The underlying C object.
104 * @param take_ownership Whether to take over the underlying C object. If
105 * true, C object is freed when wrapper is destroyed.
106 */
107 explicit IteratorBase(GstIterator* castitem, bool take_ownership = true);
108
109 /** Assignment operator. It replaces the contents of the iterator with the
110 * contents of the new one freeing the underlying C object if a
111 * take_ownership value of true was used when wrapping it. Please note that
112 * copying and assigning merely shares the underlying C object. Operations
113 * on the copy are also performed in the underlying C object of the original
114 * and if the original is destroyed, the copy is invalid.
115 */
116 IteratorBase<CppType>& operator=(const IteratorBase<CppType>& other);
117
118 #ifndef DOXYGEN_SHOULD_SKIP_THIS
119 GValue current; // The current element the iterator is referencing.
120 IteratorResult current_result; // The current result of a next() call.
121 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
122
123 private:
124 #ifndef DOXYGEN_SHOULD_SKIP_THIS
125 GstIterator* cobject_; // The underlying C object.
126 bool take_ownership; // Whether to destroy C object with the wrapper.
127 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
128 };
129
130 /** A class used to retrieve multiple elements in a thread safe way.
131 * Gst::IteratorBasic iterates specifically through elements that are not
132 * reference counted. Gst::Iterator is used for iterating through reference
133 * counted objects.
134 */
135 template <class CppType>
136 class IteratorBasic : public IteratorBase<CppType>
137 {
138 public:
139 ///Default constructor.
140 IteratorBasic();
141
142 /** Creates a Gst::IteratorBasic wrapper for a GstIterator object. The
143 * underlying @a castitem will be freed with the Gst::IteratorBasic
144 * destruction if a take_ownership value of true is given.
145 *
146 * @param castitem The C instance to wrap.
147 * @param take_ownership Whether to take over the underlying C object. If
148 * true, C object is freed when wrapper is destroyed.
149 */
150 explicit IteratorBasic(GstIterator* castitem, bool take_ownership = true);
151
152 /** Resynchronizes the iterator and moves the iterator to the first item.
153 *
154 * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a
155 * concurrent update to the iterator occurs while it is advanced to the first
156 * element).
157 */
158 void begin();
159
160 /** Dereferences the iterator and obtains the underlying object.
161 */
162 CppType operator*() const;
163
164 /** Accesses underlying object member through the iterator.
165 */
166 CppType* operator->() const;
167
168 /** Prefix auto-increment operator. It advances to the next item in the
169 * iterator. It is faster than the postfix operator.
170 * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a
171 * concurrent update to the iterator occurs while it iterates).
172 */
173 IteratorBasic<CppType>& operator++();
174
175 /** Postfix auto-increment operator. It advances to the next item in the
176 * iterator.
177 * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a
178 * concurrent update to the iterator occurs while it iterates).
179 */
180 IteratorBasic<CppType> operator++(int);
181 };
182
183 /** A class used to retrieve multiple reference counted elements in a thread
184 * safe way.
185 * Gst::Iterator iterates specifically through elements that are reference
186 * counted and therefore dereferencing the elements of the iterator yields a
187 * Glib::RefPtr<> to the C++ element type.
188 */
189 template <class CppType>
190 class Iterator : public IteratorBasic<CppType>
191 {
192 public:
193 ///Default constructor.
194 Iterator();
195
196 /** Creates a Gst::Iterator wrapper for a GstIterator object. The underlying
197 * @a castitem will be freed with the Gst::Iterator destruction if a
198 * take_ownership value of true is given.
199 *
200 * @param castitem The C instance to wrap.
201 * @param take_ownership Whether to take over the underlying C object. If
202 * true, C object is freed when wrapper is destroyed.
203 */
204 explicit Iterator(GstIterator* castitem, bool take_ownership = true);
205
206 /** Moves to the next iterator item.
207 *
208 * @return The result of the iteration. MT safe.
209 */
210 IteratorResult next();
211
212 /** Dereferences the iterator and obtains the underlying Glib::RefPtr<>.
213 */
214 Glib::RefPtr<CppType> operator*() const;
215
216 /** Accesses underlying object member through the RefPtr<>.
217 */
218 CppType* operator->() const;
219
220 /** Prefix auto-increment operator. It advances to the next item in the
221 * iterator. It is faster than the postfix operator.
222 * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a
223 * concurrent update to the iterator occurs while it iterates).
224 */
225 Iterator<CppType>& operator++();
226
227 /** Postfix auto-increment operator. It advances to the next item in the
228 * iterator.
229 * @throw std::runtime_error (if a Gst::ITERATOR_ERROR is encountered or if a
230 * concurrent update to the iterator occurs while it iterates).
231 */
232 Iterator<CppType> operator++(int);
233 };
234
235 #ifndef DOXYGEN_SHOULD_SKIP_THIS
236
237 /***************** Gst::IteratorBase<CppType> ************************/
238
239 template<class CppType>
240 IteratorBase<CppType>::IteratorBase()
241 : current(G_VALUE_INIT),
242 current_result(Gst::ITERATOR_OK),
243 cobject_(nullptr),
244 take_ownership(true)
245 {
246 }
247
248 template<class CppType>
249 IteratorBase<CppType>::IteratorBase(const IteratorBase<CppType>& other)
250 : current(G_VALUE_INIT),
251 current_result(other.current_result),
252 take_ownership(other.take_ownership)
253 {
254 cobject_ = other.take_ownership ? gst_iterator_copy(other.cobject_) : other.cobject_;
255
256 if (G_IS_VALUE (&other.current))
257 {
258 g_value_init(&current, G_VALUE_TYPE(&other.current));
259 g_value_copy(&other.current, &current);
260 }
261 }
262
263 template<class CppType>
264 IteratorBase<CppType>::IteratorBase(GstIterator* castitem, bool take_ownership)
265 : current(G_VALUE_INIT),
266 current_result(Gst::ITERATOR_OK),
267 cobject_(castitem),
268 take_ownership(take_ownership)
269 {}
270
271 template<class CppType>
272 IteratorBase<CppType>& IteratorBase<CppType>::operator=(const IteratorBase<CppType>& other)
273 {
274 if (cobject_ && take_ownership)
275 {
276 gst_iterator_free(cobject_);
277 }
278
279 if (G_IS_VALUE(&current))
280 {
281 g_value_unset(&current);
282 }
283
284 current_result = other.current_result;
285 cobject_ = other.take_ownership ? gst_iterator_copy(other.cobject_) : other.cobject_;
286
287 if (G_IS_VALUE (&other.current))
288 {
289 g_value_init(&current, G_VALUE_TYPE(&other.current));
290 g_value_copy(&other.current, &current);
291 }
292
293 return *this;
294 }
295
296 template<class CppType>
297 IteratorResult IteratorBase<CppType>::next()
298 {
299 // Unset current before calling gst_iterator_next()
300 if(G_IS_VALUE(&current))
301 g_value_unset(&current);
302
303 current_result =
304 static_cast<Gst::IteratorResult>(gst_iterator_next(cobj(), &current));
305
306 // Reset current if iterator is done:
307 if(current_result == Gst::ITERATOR_DONE)
308 g_value_reset (&current);
309
310 return current_result;
311 }
312
313 template<class CppType>
314 void IteratorBase<CppType>::resync()
315 {
316 gst_iterator_resync(cobj());
317 g_value_reset (&current);
318 current_result = Gst::ITERATOR_OK;
319 }
320
321 template<class CppType>
322 bool IteratorBase<CppType>::is_start() const
323 {
324 return (G_VALUE_HOLDS_OBJECT(&current) && current_result == Gst::ITERATOR_OK);
325 }
326
327 template<class CppType>
328 bool IteratorBase<CppType>::is_end() const
329 {
330 return (current_result == Gst::ITERATOR_DONE);
331 }
332
333 template<class CppType>
334 IteratorBase<CppType>::operator bool() const
335 {
336 return (! G_VALUE_HOLDS_OBJECT(&current));
337 }
338
339 //virtual
340 template<class CppType>
341 IteratorBase<CppType>::~IteratorBase()
342 {
343 if(take_ownership && cobject_)
344 {
345 gst_iterator_free(cobject_);
346 cobject_ = nullptr;
347 }
348 if(G_IS_VALUE(&current))
349 g_value_unset(&current);
350 }
351
352 /***************** Gst::IteratorBasic<CppType> ***********************/
353
354 template <class CppType>
355 IteratorBasic<CppType>::IteratorBasic()
356 : IteratorBase<CppType>()
357 {}
358
359 template <class CppType>
360 IteratorBasic<CppType>::IteratorBasic(GstIterator* castitem,
361 bool take_ownership)
362 : IteratorBase<CppType>(castitem, take_ownership)
363 {}
364
365 template<class CppType>
366 void IteratorBasic<CppType>::begin()
367 {
368 this->resync();
369 ++(*this);
370 }
371
372 template <class CppType>
373 CppType IteratorBasic<CppType>::operator*() const
374 {
375 typedef typename CppType::BaseObjectType CType;
376
377 if(G_IS_VALUE(&this->current))
378 return CppType(static_cast<CType*>(g_value_get_object(&this->current)));
379 else
380 return CppType();
381 }
382
383 template <class CppType>
384 CppType* IteratorBasic<CppType>::operator->() const
385 {
386 static typename CppType::CppObjectType result;
387
388 if(G_IS_VALUE(&this->current))
389 {
390 result = this->operator*();
391 return &result;
392 }
393 else
394 return static_cast<CppType*>(0);
395 }
396
397 template<class CppType>
398 IteratorBasic<CppType>& IteratorBasic<CppType>::operator++()
399 {
400 const IteratorResult result = this->next();
401
402 if(result == Gst::ITERATOR_RESYNC)
403 gstreamermm_handle_error("Concurrent update of iterator elements. "
404 "Please resync.");
405 else if(result == Gst::ITERATOR_ERROR)
406 gstreamermm_handle_error("Iterator error while incrementing.");
407
408 return *this;
409 }
410
411 template<class CppType>
412 IteratorBasic<CppType> IteratorBasic<CppType>::operator++(int)
413 {
414 IteratorBasic<CppType> original = *this;
415 ++(*this);
416 return original;
417 }
418
419 /******************* Gst::Iterator<CppType> **************************/
420
421 template <class CppType>
422 Iterator<CppType>::Iterator()
423 : IteratorBasic<CppType>()
424 {}
425
426 template <class CppType>
427 Iterator<CppType>::Iterator(GstIterator* castitem, bool take_ownership)
428 : IteratorBasic<CppType>(castitem, take_ownership)
429 {}
430
431 template <class CppType>
432 IteratorResult Iterator<CppType>::next()
433 {
434 const IteratorResult result = IteratorBasic<CppType>::next();
435
436 return result;
437 }
438
439 template <class CppType>
440 Glib::RefPtr<CppType> Iterator<CppType>::operator*() const
441 {
442 typedef typename CppType::BaseObjectType CType;
443
444 if(G_IS_VALUE(&this->current))
445 {
446 //Take extra reference when dereferencing. The reference will disappear
447 //when Glib::RefPtr<> is destroyed.
448 return Glib::wrap(static_cast<CType*>(g_value_get_object (&this->current)), true);
449 }
450 else
451 return Glib::RefPtr<CppType>(0);
452 }
453
454 template <class CppType>
455 CppType* Iterator<CppType>::operator->() const
456 {
457 typedef typename CppType::BaseObjectType CType;
458
459 if(G_IS_VALUE(&this->current))
460 {
461 //Take extra reference when dereferencing. The reference will disappear
462 //when Glib::RefPtr<> is destroyed.
463 return Glib::wrap(static_cast<CType*>(g_value_get_object (&this->current)), true).operator->();
464 }
465 else
466 return static_cast<CppType*>(0);
467 }
468
469 template<class CppType>
470 Iterator<CppType>& Iterator<CppType>::operator++()
471 {
472 IteratorBasic<CppType>::operator++();
473 return *this;
474 }
475
476 template<class CppType>
477 Iterator<CppType> Iterator<CppType>::operator++(int)
478 {
479 Iterator<CppType> original = *this;
480 ++(*this);
481 return original;
482 }
483
484 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
485
486 } //namespace Gst