Logo AND Algorithmique Numérique Distribuée

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