Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #256 from Flamefire/master
[simgrid.git] / include / xbt / ex.h
1 /* Copyright (c) 2005-2018. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef XBT_EX_H
8 #define XBT_EX_H
9
10 #include <stdlib.h>
11
12 #include "xbt/base.h"
13 #include "xbt/sysdep.h"
14 #include "xbt/misc.h"
15 #include "xbt/virtu.h"
16
17 /** @addtogroup XBT_ex_c
18  *  @brief Exceptions support (C)
19  *
20  *  Those fonctions are used to throw C++ exceptions from C code. This feature
21  *  should probably be removed in the future because C and exception do not
22  *  exactly play nicely together.
23  */
24
25 /** Categories of errors
26  *
27  *  This very similar to std::error_catgory and should probably be replaced
28  *  by this in the future.
29  *
30  *  @ingroup XBT_ex_c
31  */
32 typedef enum {
33   unknown_error = 0,            /**< unknown error */
34   arg_error,                    /**< Invalid argument */
35   bound_error,                  /**< Out of bounds argument */
36   mismatch_error,               /**< The provided ID does not match */
37   not_found_error,              /**< The searched element was not found */
38   system_error,                 /**< a syscall did fail */
39   network_error,                /**< error while sending/receiving data */
40   timeout_error,                /**< not quick enough, dude */
41   cancel_error,                 /**< an action was canceled */
42   thread_error,                 /**< error while [un]locking */
43   host_error,                   /**< host failed */
44   tracing_error,                /**< error during the simulation tracing */
45   io_error,                     /**< disk or file error */
46   vm_error                      /**< vm  error */
47 } xbt_errcat_t;
48
49 SG_BEGIN_DECL()
50
51 /** Get the name of a category
52  *  @ingroup XBT_ex_c
53  */
54 XBT_PUBLIC const char* xbt_ex_catname(xbt_errcat_t cat);
55
56 typedef struct xbt_ex xbt_ex_t;
57
58 /** Helper function used to throw exceptions in C */
59 XBT_ATTRIB_NORETURN XBT_PUBLIC void _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file,
60                                                int line, const char* func);
61
62 /** Builds and throws an exception
63  *  @ingroup XBT_ex_c
64  *  @hideinitializer
65  */
66 #define THROW(c, v)             { _xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); }
67
68 /** Builds and throws an exception with a printf-like formatted message
69  *  @ingroup XBT_ex_c
70  *  @hideinitializer
71  */
72 #define THROWF(c, v, ...)       _xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__)
73
74 /** Throw an exception because someting impossible happened
75  *  @ingroup XBT_ex_c
76  */
77 #define THROW_IMPOSSIBLE \
78   THROWF(unknown_error, 0, "The Impossible Did Happen (yet again)")
79
80 /** Throw an exception because someting unimplemented stuff has been attempted
81  *  @ingroup XBT_ex_c
82  */
83 #define THROW_UNIMPLEMENTED \
84   THROWF(unknown_error, 0, "Function %s unimplemented",__func__)
85
86 /** Throw an exception because some dead code was reached
87  *  @ingroup XBT_ex_c
88  */
89 #define THROW_DEADCODE \
90   THROWF(unknown_error, 0, "Function %s was supposed to be DEADCODE, but it's not",__func__)
91
92 /** Die because something impossible happened
93  *  @ingroup XBT_ex_c
94  */
95 #define DIE_IMPOSSIBLE xbt_die("The Impossible Did Happen (yet again)")
96
97 /** Display an exception */
98 XBT_PUBLIC void xbt_ex_display(xbt_ex_t* e);
99
100 SG_END_DECL()
101
102 /** @} */
103 #endif                          /* __XBT_EX_H__ */