Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into fix/execute_benched
[simgrid.git] / include / xbt / ex.h
1 /* Copyright (c) 2005-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /*  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>       */
5 /*  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>         */
6 /*  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>           */
7 /*  All rights reserved.                                                    */
8
9 /* This code is inspirated from the OSSP version (as retrieved back in 2004)*/
10 /* It was heavily modified to fit the SimGrid framework.                    */
11
12 /* The OSSP version has the following copyright notice:
13 **  OSSP ex - Exception Handling
14 **  Copyright (c) 2002-2004 Ralf S. Engelschall <rse@engelschall.com>
15 **  Copyright (c) 2002-2004 The OSSP Project <http://www.ossp.org/>
16 **  Copyright (c) 2002-2004 Cable & Wireless <http://www.cw.com/>
17 **
18 **  This file is part of OSSP ex, an exception handling library
19 **  which can be found at http://www.ossp.org/pkg/lib/ex/.
20 **
21 **  Permission to use, copy, modify, and distribute this software for
22 **  any purpose with or without fee is hereby granted, provided that
23 **  the above copyright notice and this permission notice appear in all
24 **  copies.
25 **
26 **  THIS SOFTWARE IS PROVIDED `AS IS'' AND ANY EXPRESSED OR IMPLIED
27 **  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
28 **  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 **  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
30 **  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 **  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 **  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
33 **  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
34 **  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 **  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
36 **  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 **  SUCH DAMAGE.
38  */
39
40 /* The extensions made for the SimGrid project can either be distributed    */
41 /* under the same license, or under the LGPL v2.1                           */
42
43 #ifndef XBT_EX_H
44 #define XBT_EX_H
45
46 #include <stdlib.h>
47
48 #include "xbt/base.h"
49 #include "xbt/sysdep.h"
50 #include "xbt/misc.h"
51 #include "xbt/virtu.h"
52
53 /** @addtogroup XBT_ex_c
54  *  @brief Exceptions support (C)
55  *
56  *  Those fonctions are used to throw C++ exceptions from C code. This feature
57  *  should probably be removed in the future because C and exception do not
58  *  exactly play nicely together.
59  */
60
61 /** Categories of errors
62  *
63  *  This very similar to std::error_catgory and should probably be replaced
64  *  by this in the future.
65  *
66  *  @ingroup XBT_ex_c
67  */
68 typedef enum {
69   unknown_error = 0,            /**< unknown error */
70   arg_error,                    /**< Invalid argument */
71   bound_error,                  /**< Out of bounds argument */
72   mismatch_error,               /**< The provided ID does not match */
73   not_found_error,              /**< The searched element was not found */
74   system_error,                 /**< a syscall did fail */
75   network_error,                /**< error while sending/receiving data */
76   timeout_error,                /**< not quick enough, dude */
77   cancel_error,                 /**< an action was canceled */
78   thread_error,                 /**< error while [un]locking */
79   host_error,                   /**< host failed */
80   tracing_error,                /**< error during the simulation tracing */
81   io_error,                     /**< disk or file error */
82   vm_error                      /**< vm  error */
83 } xbt_errcat_t;
84
85 SG_BEGIN_DECL()
86
87 /** Get the name of a category
88  *  @ingroup XBT_ex_c
89  */
90 XBT_PUBLIC(const char *) xbt_ex_catname(xbt_errcat_t cat);
91
92 typedef struct xbt_ex xbt_ex_t;
93
94 /** Helper function used to throw exceptions in C */
95 XBT_ATTRIB_NORETURN XBT_PUBLIC(void)
96     _xbt_throw(char* message, xbt_errcat_t errcat, int value, const char* file, int line, const char* func);
97
98 /** Builds and throws an exception
99  *  @ingroup XBT_ex_c
100  *  @hideinitializer
101  */
102 #define THROW(c, v)             { _xbt_throw(NULL, (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__); }
103
104 /** Builds and throws an exception with a printf-like formatted message
105  *  @ingroup XBT_ex_c
106  *  @hideinitializer
107  */
108 #define THROWF(c, v, ...)       _xbt_throw(bprintf(__VA_ARGS__), (xbt_errcat_t) c, v, __FILE__, __LINE__, __func__)
109
110 /** Throw an exception because someting impossible happened
111  *  @ingroup XBT_ex_c
112  */
113 #define THROW_IMPOSSIBLE \
114   THROWF(unknown_error, 0, "The Impossible Did Happen (yet again)")
115
116 /** Throw an exception because someting unimplemented stuff has been attempted
117  *  @ingroup XBT_ex_c
118  */
119 #define THROW_UNIMPLEMENTED \
120   THROWF(unknown_error, 0, "Function %s unimplemented",__func__)
121
122 /** Throw an exception because some dead code was reached
123  *  @ingroup XBT_ex_c
124  */
125 #define THROW_DEADCODE \
126   THROWF(unknown_error, 0, "Function %s was supposed to be DEADCODE, but it's not",__func__)
127
128 /** Die because something impossible happened
129  *  @ingroup XBT_ex_c
130  */
131 #define DIE_IMPOSSIBLE xbt_die("The Impossible Did Happen (yet again)")
132
133 /** Display an exception */
134 XBT_PUBLIC(void) xbt_ex_display(xbt_ex_t * e);
135
136 SG_END_DECL()
137
138 /** @} */
139 #endif                          /* __XBT_EX_H__ */