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  * Conversion parameters:
26  * inFile  = glib-Error-Reporting.html
27  * outPack = glib
28  * outFile = ErrorG
29  * strct   = GError
30  * realStrct=
31  * ctorStrct=
32  * clss    = ErrorG
33  * interf  = 
34  * class Code: Yes
35  * interface Code: No
36  * template for:
37  * extend  = 
38  * implements:
39  * prefixes:
40  * 	- g_error_
41  * omit structs:
42  * omit prefixes:
43  * omit code:
44  * omit signals:
45  * imports:
46  * 	- glib.Str
47  * 	- gtkc.paths
48  * 	- gtkc.Loader
49  * structWrap:
50  * 	- GError* -> ErrorG
51  * module aliases:
52  * local aliases:
53  * overrides:
54  */
55 
56 module glib.ErrorG;
57 
58 public  import gtkc.glibtypes;
59 
60 private import gtkc.glib;
61 private import glib.ConstructionException;
62 
63 private import glib.Str;
64 private import gtkc.paths;
65 private import gtkc.Loader;
66 
67 
68 
69 /**
70  * GLib provides a standard method of reporting errors from a called
71  * function to the calling code. (This is the same problem solved by
72  * exceptions in other languages.) It's important to understand that
73  * this method is both a data type (the GError
74  * object) and a set of rules. If you use GError
75  * incorrectly, then your code will not properly interoperate with other
76  * code that uses GError, and users of your API will probably get confused.
77  *
78  * First and foremost: GError should only be used to report
79  * recoverable runtime errors, never to report programming
80  * errors. If the programmer has screwed up, then you should
81  * use g_warning(), g_return_if_fail(), g_assert(), g_error(), or some
82  * similar facility. (Incidentally, remember that the g_error() function
83  * should only be used for programming errors, it
84  * should not be used to print any error reportable via GError.)
85  *
86  * Examples of recoverable runtime errors are "file not found" or
87  * "failed to parse input." Examples of programming errors are "NULL
88  * passed to strcmp()" or "attempted to free the same pointer twice."
89  * These two kinds of errors are fundamentally different: runtime errors
90  * should be handled or reported to the user, programming errors should
91  * be eliminated by fixing the bug in the program. This is why most
92  * functions in GLib and GTK+ do not use the GError facility.
93  *
94  * Functions that can fail take a return location for a GError as their
95  * last argument. For example:
96  *
97  * $(DDOC_COMMENT example)
98  *
99  * If you pass a non-NULL value for the error
100  * argument, it should point to a location where an error can be placed.
101  * For example:
102  *
103  * $(DDOC_COMMENT example)
104  *
105  * Note that err != NULL in this example is a
106  * reliable indicator of whether
107  * g_file_get_contents() failed. Additionally, g_file_get_contents()
108  * returns a boolean which indicates whether it was successful.
109  *
110  * Because g_file_get_contents() returns FALSE on failure, if you
111  * are only interested in whether it failed and don't need to display
112  * an error message, you can pass NULL for the error
113  * argument:
114  *
115  * $(DDOC_COMMENT example)
116  *
117  * The GError object contains three fields: domain
118  * indicates the module the error-reporting function is located in,
119  * code indicates the specific error that occurred,
120  * and message is a user-readable error message with
121  * as many details as possible. Several functions are provided to deal
122  * with an error received from a called function: g_error_matches()
123  * returns TRUE if the error matches a given domain and code,
124  * g_propagate_error() copies an error into an error location (so the
125  * calling function will receive it), and g_clear_error() clears an
126  * error location by freeing the error and resetting the location to
127  * NULL. To display an error to the user, simply display
128  * error->message, perhaps along with additional
129  * context known only to the calling function (the file being opened,
130  * or whatever -- though in the g_file_get_contents() case,
131  * error->message already contains a filename).
132  *
133  * When implementing a function that can report errors, the basic
134  * tool is g_set_error(). Typically, if a fatal error occurs you
135  * want to g_set_error(), then return immediately. g_set_error()
136  * does nothing if the error location passed to it is NULL.
137  * Here's an example:
138  *
139  * $(DDOC_COMMENT example)
140  *
141  * Things are somewhat more complicated if you yourself call another
142  * function that can report a GError. If the sub-function indicates
143  * fatal errors in some way other than reporting a GError, such as
144  * by returning TRUE on success, you can simply do the following:
145  *
146  * $(DDOC_COMMENT example)
147  *
148  * If the sub-function does not indicate errors other than by
149  * reporting a GError, you need to create a temporary GError
150  * since the passed-in one may be NULL. g_propagate_error() is
151  * intended for use in this case.
152  *
153  * $(DDOC_COMMENT example)
154  *
155  * Error pileups are always a bug. For example, this code is incorrect:
156  *
157  * $(DDOC_COMMENT example)
158  *
159  * tmp_error should be checked immediately after
160  * sub_function_that_can_fail(), and either cleared or propagated
161  * upward. The rule is: after each error, you must either
162  * handle the error, or return it to the calling function.
163  * Note that passing NULL for the error location is the equivalent
164  * of handling an error by always doing nothing about it. So the
165  * following code is fine, assuming errors in sub_function_that_can_fail()
166  * are not fatal to my_function_that_can_fail():
167  *
168  * $(DDOC_COMMENT example)
169  *
170  * Note that passing NULL for the error location
171  * ignores errors; it's equivalent to
172  * try { sub_function_that_can_fail(); } catch (...) {}
173  * in C++. It does not mean to leave errors
174  * unhandled; it means to handle them by doing nothing.
175  *
176  * Error domains and codes are conventionally named as follows:
177  *
178  *  The error domain is called
179  *  <NAMESPACE>_<MODULE>_ERROR,
180  *  for example G_SPAWN_ERROR or G_THREAD_ERROR:
181  *
182  * $(DDOC_COMMENT example)
183  *
184  *  The quark function for the error domain is called
185  *  <namespace>_<module>_error_quark,
186  *  for example g_spawn_error_quark() or g_thread_error_quark().
187  *
188  *  The error codes are in an enumeration called
189  *  <Namespace><Module>Error;
190  *  for example,GThreadError or GSpawnError.
191  *
192  *  Members of the error code enumeration are called
193  *  <NAMESPACE>_<MODULE>_ERROR_<CODE>,
194  *  for example G_SPAWN_ERROR_FORK or G_THREAD_ERROR_AGAIN.
195  *
196  *  If there's a "generic" or "unknown" error code for unrecoverable
197  *  errors it doesn't make sense to distinguish with specific codes,
198  *  it should be called <NAMESPACE>_<MODULE>_ERROR_FAILED,
199  *  for example G_SPAWN_ERROR_FAILED.
200  *
201  * Summary of rules for use of GError:
202  *
203  *  Do not report programming errors via GError.
204  *
205  *  The last argument of a function that returns an error should
206  *  be a location where a GError can be placed (i.e. "GError** error").
207  *  If GError is used with varargs, the GError** should be the last
208  *  argument before the "...".
209  *
210  *  The caller may pass NULL for the GError** if they are not interested
211  *  in details of the exact error that occurred.
212  *
213  *  If NULL is passed for the GError** argument, then errors should
214  *  not be returned to the caller, but your function should still
215  *  abort and return if an error occurs. That is, control flow should
216  *  not be affected by whether the caller wants to get a GError.
217  *
218  *  If a GError is reported, then your function by definition
219  *  had a fatal failure and did not complete whatever
220  *  it was supposed to do. If the failure was not fatal,
221  *  then you handled it and you should not report it. If it was fatal,
222  *  then you must report it and discontinue whatever you were doing
223  *  immediately.
224  *
225  *  If a GError is reported, out parameters are not guaranteed to
226  *  be set to any defined value.
227  *
228  *  A GError* must be initialized to NULL before passing its address
229  *  to a function that can report errors.
230  *
231  *  "Piling up" errors is always a bug. That is, if you assign a
232  *  new GError to a GError* that is non-NULL, thus overwriting
233  *  the previous error, it indicates that you should have aborted
234  *  the operation instead of continuing. If you were able to continue,
235  *  you should have cleared the previous error with g_clear_error().
236  *  g_set_error() will complain if you pile up errors.
237  *
238  *  By convention, if you return a boolean value indicating success
239  *  then TRUE means success and FALSE means failure. If FALSE is
240  *  returned, the error must be set to a non-NULL
241  *  value.
242  *
243  *  A NULL return value is also frequently used to mean that an error
244  *  occurred. You should make clear in your documentation whether NULL
245  *  is a valid return value in non-error cases; if NULL is a valid value,
246  *  then users must check whether an error was returned to see if the
247  *  function succeeded.
248  *
249  *  When implementing a function that can report errors, you may want
250  *  to add a check at the top of your function that the error return
251  *  location is either NULL or contains a NULL error (e.g.
252  *  g_return_if_fail (error == NULL || *error == NULL);).
253  */
254 public class ErrorG
255 {
256 	
257 	/** the main Gtk struct */
258 	protected GError* gError;
259 	
260 	
261 	/** Get the main Gtk struct */
262 	public GError* getErrorGStruct()
263 	{
264 		return gError;
265 	}
266 	
267 	
268 	/** the main Gtk struct as a void* */
269 	protected void* getStruct()
270 	{
271 		return cast(void*)gError;
272 	}
273 	
274 	/**
275 	 * Sets our main struct and passes it to the parent class
276 	 */
277 	public this (GError* gError)
278 	{
279 		this.gError = gError;
280 	}
281 	
282 	~this()
283 	{
284 		if ( Linker.isLoaded(LIBRARY.GLIB) && gError !is null )
285 		{
286 			g_error_free(gError);
287 		}
288 	}
289 	
290 	/**
291 	 */
292 	
293 	/**
294 	 * Creates a new GError; unlike g_error_new(), message is
295 	 * not a printf()-style format string. Use this function if
296 	 * message contains text you don't have control over,
297 	 * that could include printf() escape sequences.
298 	 * Params:
299 	 * domain = error domain
300 	 * code = error code
301 	 * message = error message
302 	 * Throws: ConstructionException GTK+ fails to create the object.
303 	 */
304 	public this (GQuark domain, int code, string message)
305 	{
306 		// GError * g_error_new_literal (GQuark domain,  gint code,  const gchar *message);
307 		auto p = g_error_new_literal(domain, code, Str.toStringz(message));
308 		if(p is null)
309 		{
310 			throw new ConstructionException("null returned by g_error_new_literal(domain, code, Str.toStringz(message))");
311 		}
312 		this(cast(GError*) p);
313 	}
314 	
315 	/**
316 	 * Creates a new GError with the given domain and code,
317 	 * and a message formatted with format.
318 	 * Since 2.22
319 	 * Params:
320 	 * domain = error domain
321 	 * code = error code
322 	 * format = printf()-style format for error message
323 	 * args = va_list of parameters for the message format
324 	 * Throws: ConstructionException GTK+ fails to create the object.
325 	 */
326 	public this (GQuark domain, int code, string format, void* args)
327 	{
328 		// GError * g_error_new_valist (GQuark domain,  gint code,  const gchar *format,  va_list args);
329 		auto p = g_error_new_valist(domain, code, Str.toStringz(format), args);
330 		if(p is null)
331 		{
332 			throw new ConstructionException("null returned by g_error_new_valist(domain, code, Str.toStringz(format), args)");
333 		}
334 		this(cast(GError*) p);
335 	}
336 	
337 	/**
338 	 * Frees a GError and associated resources.
339 	 */
340 	public void free()
341 	{
342 		// void g_error_free (GError *error);
343 		g_error_free(gError);
344 	}
345 	
346 	/**
347 	 * Makes a copy of error.
348 	 * Returns: a new GError
349 	 */
350 	public ErrorG copy()
351 	{
352 		// GError * g_error_copy (const GError *error);
353 		auto p = g_error_copy(gError);
354 		
355 		if(p is null)
356 		{
357 			return null;
358 		}
359 		
360 		return new ErrorG(cast(GError*) p);
361 	}
362 	
363 	/**
364 	 * Returns TRUE if error matches domain and code, FALSE
365 	 * otherwise. In particular, when error is NULL, FALSE will
366 	 * be returned.
367 	 * Params:
368 	 * domain = an error domain
369 	 * code = an error code
370 	 * Returns: whether error has domain and code
371 	 */
372 	public int matches(GQuark domain, int code)
373 	{
374 		// gboolean g_error_matches (const GError *error,  GQuark domain,  gint code);
375 		return g_error_matches(gError, domain, code);
376 	}
377 	
378 	/**
379 	 * Does nothing if err is NULL; if err is non-NULL, then *err
380 	 * must be NULL. A new GError is created and assigned to *err.
381 	 * Unlike g_set_error(), message is not a printf()-style format string.
382 	 * Use this function if message contains text you don't have control over,
383 	 * that could include printf() escape sequences.
384 	 * Since 2.18
385 	 * Params:
386 	 * err = a return location for a GError, or NULL. [allow-none]
387 	 * domain = error domain
388 	 * code = error code
389 	 * message = error message
390 	 */
391 	public static void gSetErrorLiteral(out ErrorG err, GQuark domain, int code, string message)
392 	{
393 		// void g_set_error_literal (GError **err,  GQuark domain,  gint code,  const gchar *message);
394 		GError* outerr = null;
395 		
396 		g_set_error_literal(&outerr, domain, code, Str.toStringz(message));
397 		
398 		err = new ErrorG(outerr);
399 	}
400 	
401 	/**
402 	 * If dest is NULL, free src; otherwise, moves src into *dest.
403 	 * The error variable dest points to must be NULL.
404 	 * Params:
405 	 * dest = error return location
406 	 * src = error to move into the return location
407 	 */
408 	public static void gPropagateError(out ErrorG dest, ErrorG src)
409 	{
410 		// void g_propagate_error (GError **dest,  GError *src);
411 		GError* outdest = null;
412 		
413 		g_propagate_error(&outdest, (src is null) ? null : src.getErrorGStruct());
414 		
415 		dest = new ErrorG(outdest);
416 	}
417 	
418 	/**
419 	 * If err is NULL, does nothing. If err is non-NULL,
420 	 * calls g_error_free() on *err and sets *err to NULL.
421 	 * Params:
422 	 * err = a GError return location
423 	 */
424 	public static void gClearError(ref ErrorG err)
425 	{
426 		// void g_clear_error (GError **err);
427 		GError* outerr = (err is null) ? null : err.getErrorGStruct();
428 		
429 		g_clear_error(&outerr);
430 		
431 		err = new ErrorG(outerr);
432 	}
433 }