MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
global.h
1 /*
2 
3  ooo ooooo oooooooooo. ooooo ooo
4  `88. .888' `888' `Y8b `888b. `8'
5  888b d'888 .ooooo. 888 888 .ooooo. 8 `88b. 8 .oooo.
6  8 Y88. .P 888 d88' `88b 888 888 d88' `88b 8 `88b. 8 `P )88b
7  8 `888' 888 888 888 888 888 888ooo888 8 `88b.8 .oP"888
8  8 Y 888 888 888 888 d88' 888 .o 8 `888 d8( 888
9  o8o o888o `Y8bod8P' o888bood8P' `Y8bod8P' o8o `8 `Y888""8o
10 
11 Copyright
12  2014-2016 MoDeNa Consortium, All rights reserved.
13 
14 License
15  This file is part of Modena.
16 
17  The Modena interface library is free software; you can redistribute it
18  and/or modify it under the terms of the GNU Lesser General Public License
19  as published by the Free Software Foundation, either version 3 of the
20  License, or (at your option) any later version.
21 
22  Modena is distributed in the hope that it will be useful, but WITHOUT ANY
23  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
25  details.
26 
27  You should have received a copy of the GNU General Public License along
28  with Modena. If not, see <http://www.gnu.org/licenses/>.
29 
30 Description
31  Interface Library
32 
33 Authors
34  Henrik Rusche
35 
36 Contributors
37 */
38 
39 #ifndef __GLOBAL_H__
40 #define __GLOBAL_H__
41 
42 #include "Python.h"
43 #include "inline.h"
44 #include <stdbool.h>
45 
46 #undef __BEGIN_DECLS
47 #undef __END_DECLS
48 #ifdef __cplusplus
49 # define __BEGIN_DECLS extern "C" {
50 # define __END_DECLS }
51 #else
52 # define __BEGIN_DECLS /* empty */
53 # define __END_DECLS /* empty */
54 #endif
55 
56 __BEGIN_DECLS
57 
58 #ifndef thread_local
59 # if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__
60 # define thread_local _Thread_local
61 # elif defined _WIN32 && ( \
62  defined _MSC_VER || \
63  defined __ICL || \
64  defined __DMC__ || \
65  defined __BORLANDC__ )
66 # define thread_local __declspec(thread)
67 /* note that ICC (linux) and Clang are covered by __GNUC__ */
68 # elif defined __GNUC__ || \
69  defined __SUNPRO_C || \
70  defined __xlC__
71 # define thread_local __thread
72 # else
73 # error "Cannot define thread_local"
74 # endif
75 #endif
76 
77 // Declare variable for error handling
78 extern thread_local int modena_error_code;
79 
80 // Declare python exceptions
81 extern PyObject *modena_DoesNotExist;
82 extern PyObject *modena_OutOfBounds;
83 extern PyObject *modena_ParametersNotValid;
84 
85 enum modena_error_t
86 {
87  MODENA_SUCCESS,
88  MODENA_MODEL_NOT_FOUND,
89  MODENA_FUNCTION_NOT_FOUND,
90  MODENA_INDEX_SET_NOT_FOUND,
91  MODENA_MODEL_LAST
92 };
93 
94 // Returns true when an error has been raised
95 INLINE_DECL bool modena_error_occurred();
96 
97 // Returns error code and resets it
98 INLINE_DECL int modena_error();
99 
100 #ifdef HAVE_INLINE
101 
102 INLINE_FUN bool modena_error_occurred()
103 {
104  return modena_error_code != MODENA_SUCCESS;
105 }
106 
107 INLINE_FUN int modena_error()
108 {
109  int ret = modena_error_code;
110  modena_error_code = 0;
111  return ret;
112 }
113 
114 #endif /* HAVE_INLINE */
115 
116 // Returns error message for error code
117 const char* modena_error_message(int error_code);
118 
119 void modena_print_backtrace();
120 
121 #define Modena_Info_Print(...) \
122  char Modena_message[256]; \
123  sprintf(Modena_message, __VA_ARGS__); \
124  fprintf(stdout, "%s in line %i of %s\n", Modena_message, __LINE__, __FILE__);
125 
126 #define Modena_Error_Print(...) \
127  char Modena_message[256]; \
128  sprintf(Modena_message, __VA_ARGS__); \
129  fprintf(stderr, "%s in line %i of %s\n", Modena_message, __LINE__, __FILE__);
130 
131 #define Modena_PyErr_Print() \
132  PyErr_Print(); \
133  Modena_Error_Print("Error in python catched"); \
134  modena_print_backtrace();
135 
136 __END_DECLS
137 
138 #endif /* __GLOBAL_H__ */
139