2013-10-11 12:53:39 +00:00
|
|
|
// Iterator.cs - Custom iterator wrapper for IEnumerable
|
|
|
|
//
|
2013-10-11 15:48:15 +00:00
|
|
|
// Authors:
|
2014-05-28 11:20:18 +00:00
|
|
|
// Maarten Bosmans <mkbosmans@gmail.com>
|
2013-10-11 15:48:15 +00:00
|
|
|
// Sebastian Dröge <slomo@circular-chaos.org>
|
|
|
|
// Stephan Sundermann <stephansundermann@gmail.com>
|
2013-10-11 12:53:39 +00:00
|
|
|
//
|
2014-05-28 11:20:18 +00:00
|
|
|
// Copyright (c) 2009 Maarten Bosmans
|
2013-10-11 15:48:15 +00:00
|
|
|
// Copyright (c) 2009 Sebastian Dröge
|
2014-05-28 11:20:18 +00:00
|
|
|
// Copyright (c) 2013 Stephan Sundermann
|
2013-10-11 12:53:39 +00:00
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of version 2 of the Lesser GNU General
|
|
|
|
// Public License as published by the Free Software Foundation.
|
|
|
|
//
|
|
|
|
// This program 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 program; if not, write to the
|
|
|
|
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
// Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
namespace Gst {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using GLib;
|
|
|
|
|
|
|
|
public partial class Iterator : IEnumerable {
|
|
|
|
|
2019-10-24 14:00:17 +00:00
|
|
|
[DllImport ("gobject-2.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2013-10-12 14:47:10 +00:00
|
|
|
static extern IntPtr g_value_reset (ref GLib.Value val);
|
|
|
|
|
2019-10-24 14:00:17 +00:00
|
|
|
[DllImport("gstreamer-1.0-0.dll", CallingConvention = CallingConvention.Cdecl)]
|
2013-10-12 15:54:30 +00:00
|
|
|
static extern int gst_iterator_next(IntPtr raw, ref GLib.Value elem);
|
|
|
|
|
|
|
|
public Gst.IteratorResult Next(ref GLib.Value elem) {
|
|
|
|
int raw_ret = gst_iterator_next(Handle, ref elem);
|
|
|
|
Gst.IteratorResult ret = (Gst.IteratorResult) raw_ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-11 12:53:39 +00:00
|
|
|
private class Enumerator : IEnumerator {
|
|
|
|
Iterator iterator;
|
|
|
|
Hashtable seen = new Hashtable ();
|
|
|
|
|
|
|
|
private object current = null;
|
|
|
|
public object Current {
|
|
|
|
get {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool MoveNext () {
|
|
|
|
IntPtr raw_ret;
|
|
|
|
bool retry = false;
|
|
|
|
|
|
|
|
if (iterator.Handle == IntPtr.Zero)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
do {
|
2013-10-12 15:54:30 +00:00
|
|
|
GLib.Value value = new GLib.Value (GLib.GType.Boolean);
|
|
|
|
value.Dispose ();
|
2013-10-12 14:47:10 +00:00
|
|
|
|
2013-10-12 15:54:30 +00:00
|
|
|
IteratorResult ret = iterator.Next (ref value);
|
2013-10-11 12:53:39 +00:00
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case IteratorResult.Done:
|
|
|
|
return false;
|
|
|
|
case IteratorResult.Ok:
|
|
|
|
if (seen.Contains (value)) {
|
|
|
|
retry = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
seen.Add (value , null);
|
|
|
|
current = value.Val;
|
|
|
|
return true;
|
|
|
|
case IteratorResult.Resync:
|
|
|
|
iterator.Resync ();
|
|
|
|
retry = true;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case IteratorResult.Error:
|
2013-10-12 14:47:10 +00:00
|
|
|
throw new Exception ("Error while iterating");
|
2013-10-11 12:53:39 +00:00
|
|
|
}
|
|
|
|
} while (retry);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset () {
|
|
|
|
seen.Clear ();
|
|
|
|
if (iterator.Handle != IntPtr.Zero)
|
|
|
|
iterator.Resync ();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Enumerator (Iterator iterator) {
|
|
|
|
this.iterator = iterator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Enumerator enumerator = null;
|
|
|
|
|
|
|
|
public IEnumerator GetEnumerator () {
|
|
|
|
if (this.enumerator == null)
|
|
|
|
this.enumerator = new Enumerator (this);
|
|
|
|
return this.enumerator;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|