1 /*
2  * This file is part of gtkD.
3  *
4  * gtkD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 3
7  * of the License, or (at your option) any later version, with
8  * some exceptions, please read the COPYING file.
9  *
10  * gtkD 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
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with gtkD; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
18  */
19 
20 // generated automatically - do not change
21 // find conversion definition on APILookup.txt
22 // implement new conversion functionalities on the wrap.utils pakage
23 
24 
25 module gstreamer.Promise;
26 
27 private import glib.ConstructionException;
28 private import gobject.ObjectG;
29 private import gstreamer.Structure;
30 private import gstreamer.c.functions;
31 public  import gstreamer.c.types;
32 public  import gstreamerc.gstreamertypes;
33 
34 
35 /**
36  * The #GstPromise object implements the container for values that may
37  * be available later. i.e. a Future or a Promise in
38  * <ulink url="https://en.wikipedia.org/wiki/Futures_and_promises">https://en.wikipedia.org/wiki/Futures_and_promises</ulink>
39  * As with all Future/Promise-like functionality, there is the concept of the
40  * producer of the value and the consumer of the value.
41  * 
42  * A #GstPromise is created with gst_promise_new() by the consumer and passed
43  * to the producer to avoid thread safety issues with the change callback.
44  * A #GstPromise can be replied to with a value (or an error) by the producer
45  * with gst_promise_reply(). gst_promise_interrupt() is for the consumer to
46  * indicate to the producer that the value is not needed anymore and producing
47  * that value can stop.  The @GST_PROMISE_RESULT_EXPIRED state set by a call
48  * to gst_promise_expire() indicates to the consumer that a value will never
49  * be produced and is intended to be called by a third party that implements
50  * some notion of message handling such as #GstBus.
51  * A callback can also be installed at #GstPromise creation for
52  * result changes with gst_promise_new_with_change_func().
53  * The change callback can be used to chain #GstPromises's together as in the
54  * following example.
55  * |[<!-- language="C" -->
56  * const GstStructure *reply;
57  * GstPromise *p;
58  * if (gst_promise_wait (promise) != GST_PROMISE_RESULT_REPLIED)
59  * return; // interrupted or expired value
60  * reply = gst_promise_get_reply (promise);
61  * if (error in reply)
62  * return; // propagate error
63  * p = gst_promise_new_with_change_func (another_promise_change_func, user_data, notify);
64  * pass p to promise-using API
65  * ]|
66  * 
67  * Each #GstPromise starts out with a #GstPromiseResult of
68  * %GST_PROMISE_RESULT_PENDING and only ever transitions once
69  * into one of the other #GstPromiseResult's.
70  * 
71  * In order to support multi-threaded code, gst_promise_reply(),
72  * gst_promise_interrupt() and gst_promise_expire() may all be from
73  * different threads with some restrictions and the final result of the promise
74  * is whichever call is made first.  There are two restrictions on ordering:
75  * 
76  * 1. That gst_promise_reply() and gst_promise_interrupt() cannot be called
77  * after gst_promise_expire()
78  * 2. That gst_promise_reply() and gst_promise_interrupt()
79  * cannot be called twice.
80  * 
81  * The change function set with gst_promise_new_with_change_func() is
82  * called directly from either the gst_promise_reply(),
83  * gst_promise_interrupt() or gst_promise_expire() and can be called
84  * from an arbitrary thread.  #GstPromise using APIs can restrict this to
85  * a single thread or a subset of threads but that is entirely up to the API
86  * that uses #GstPromise.
87  *
88  * Since: 1.14
89  */
90 public class Promise
91 {
92 	/** the main Gtk struct */
93 	protected GstPromise* gstPromise;
94 	protected bool ownedRef;
95 
96 	/** Get the main Gtk struct */
97 	public GstPromise* getPromiseStruct(bool transferOwnership = false)
98 	{
99 		if (transferOwnership)
100 			ownedRef = false;
101 		return gstPromise;
102 	}
103 
104 	/** the main Gtk struct as a void* */
105 	protected void* getStruct()
106 	{
107 		return cast(void*)gstPromise;
108 	}
109 
110 	/**
111 	 * Sets our main struct and passes it to the parent class.
112 	 */
113 	public this (GstPromise* gstPromise, bool ownedRef = false)
114 	{
115 		this.gstPromise = gstPromise;
116 		this.ownedRef = ownedRef;
117 	}
118 
119 
120 	/** */
121 	public static GType getType()
122 	{
123 		return gst_promise_get_type();
124 	}
125 
126 	/**
127 	 * Returns: a new #GstPromise
128 	 *
129 	 * Since: 1.14
130 	 *
131 	 * Throws: ConstructionException GTK+ fails to create the object.
132 	 */
133 	public this()
134 	{
135 		auto p = gst_promise_new();
136 
137 		if(p is null)
138 		{
139 			throw new ConstructionException("null returned by new");
140 		}
141 
142 		this(cast(GstPromise*) p);
143 	}
144 
145 	/**
146 	 * @func will be called exactly once when transitioning out of
147 	 * %GST_PROMISE_RESULT_PENDING into any of the other #GstPromiseResult
148 	 * states.
149 	 *
150 	 * Params:
151 	 *     func = a #GstPromiseChangeFunc to call
152 	 *     userData = argument to call @func with
153 	 *     notify = notification function that @user_data is no longer needed
154 	 *
155 	 * Returns: a new #GstPromise
156 	 *
157 	 * Since: 1.14
158 	 *
159 	 * Throws: ConstructionException GTK+ fails to create the object.
160 	 */
161 	public this(GstPromiseChangeFunc func, void* userData, GDestroyNotify notify)
162 	{
163 		auto p = gst_promise_new_with_change_func(func, userData, notify);
164 
165 		if(p is null)
166 		{
167 			throw new ConstructionException("null returned by new_with_change_func");
168 		}
169 
170 		this(cast(GstPromise*) p);
171 	}
172 
173 	/**
174 	 * Expire a @promise.  This will wake up any waiters with
175 	 * %GST_PROMISE_RESULT_EXPIRED.  Called by a message loop when the parent
176 	 * message is handled and/or destroyed (possibly unanswered).
177 	 *
178 	 * Since: 1.14
179 	 */
180 	public void expire()
181 	{
182 		gst_promise_expire(gstPromise);
183 	}
184 
185 	/**
186 	 * Retrieve the reply set on @promise.  @promise must be in
187 	 * %GST_PROMISE_RESULT_REPLIED and the returned structure is owned by @promise
188 	 *
189 	 * Returns: The reply set on @promise
190 	 *
191 	 * Since: 1.14
192 	 */
193 	public Structure getReply()
194 	{
195 		auto p = gst_promise_get_reply(gstPromise);
196 
197 		if(p is null)
198 		{
199 			return null;
200 		}
201 
202 		return ObjectG.getDObject!(Structure)(cast(GstStructure*) p);
203 	}
204 
205 	/**
206 	 * Interrupt waiting for a @promise.  This will wake up any waiters with
207 	 * %GST_PROMISE_RESULT_INTERRUPTED.  Called when the consumer does not want
208 	 * the value produced anymore.
209 	 *
210 	 * Since: 1.14
211 	 */
212 	public void interrupt()
213 	{
214 		gst_promise_interrupt(gstPromise);
215 	}
216 
217 	/**
218 	 * Set a reply on @promise.  This will wake up any waiters with
219 	 * %GST_PROMISE_RESULT_REPLIED.  Called by the producer of the value to
220 	 * indicate success (or failure).
221 	 *
222 	 * If @promise has already been interrupted by the consumer, then this reply
223 	 * is not visible to the consumer.
224 	 *
225 	 * Params:
226 	 *     s = a #GstStructure with the the reply contents
227 	 *
228 	 * Since: 1.14
229 	 */
230 	public void reply(Structure s)
231 	{
232 		gst_promise_reply(gstPromise, (s is null) ? null : s.getStructureStruct(true));
233 	}
234 
235 	/**
236 	 * Wait for @promise to move out of the %GST_PROMISE_RESULT_PENDING state.
237 	 * If @promise is not in %GST_PROMISE_RESULT_PENDING then it will return
238 	 * immediately with the current result.
239 	 *
240 	 * Returns: the result of the promise
241 	 *
242 	 * Since: 1.14
243 	 */
244 	public GstPromiseResult wait()
245 	{
246 		return gst_promise_wait(gstPromise);
247 	}
248 }