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