SST/macro
errors.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SPROCKIT_COMMON_ERRORS_H_INCLUDED
13 #define SPROCKIT_COMMON_ERRORS_H_INCLUDED
14 
15 #include <exception>
16 #include <stdlib.h>
17 #include <sprockit/spkt_string.h>
18 
19 namespace sprockit {
20 
21 //----------------------------------------------------------------------
22 // ASSERT
23 // If condition is false, print a message and dump core.
24 // Useful for documenting assumptions in the code.
25 //
26 // NOTE: needs to be a #define, to be able to print the location
27 // where the error occurred.
28 //----------------------------------------------------------------------
29 #define SPROCKIT_ASSERT(condition) \
30  if (!(condition)) { \
31  fprintf(stderr, "Assertion failed: line %d, file \"%s\"\n", \
32  __LINE__, __FILE__); \
33  fflush(stderr); \
34  exit(1); \
35  }
36 
37 #define spkt_throw_printf(exc, template_str, ...) \
38  throw exc(::sprockit::printf(#exc ": " template_str "\n%s %d", ##__VA_ARGS__, __FILE__, __LINE__))
39 
40 #define spkt_abort_printf(template_str, ...) \
41  std::cerr << ::sprockit::printf("error: " template_str "\n%s %d\n", ##__VA_ARGS__, __FILE__, __LINE__); \
42  ::abort()
43 
44 #define spkt_throw(exc, ...) \
45  { \
46  std::stringstream sstr; \
47  ::sprockit::spkt_to_stream(sstr, __VA_ARGS__, "\n", __FILE__, ":", __LINE__); \
48  throw exc(sstr.str()); \
49  }
50 
51 /**
52  * General errors, or base class for more specific errors.
53  */
54 struct spkt_error : public std::exception {
55  spkt_error(const std::string &msg) :
56  message(msg) {
57  }
58  virtual
59  ~spkt_error() throw () {
60  }
61  virtual const char*
62  what() const throw () {
63  return message.c_str();
64  }
65  const std::string message;
66 };
67 
68 /**
69  * Error indicating something was null and shouldn't have been
70  */
71 struct null_error : public spkt_error {
72  null_error(const std::string &msg) :
73  spkt_error(msg) {
74  }
75  virtual
76  ~null_error() throw () {
77  }
78 };
79 
80 /**
81  * Error indicating some internal value was unexpected.
82  */
83 struct value_error : public spkt_error {
84  value_error(const std::string &msg) :
85  spkt_error(msg) {
86  }
87  virtual
88  ~value_error() throw () {
89  }
90 };
91 
92 /**
93  * A general error somewhere from a library
94  */
95 struct library_error : public spkt_error {
96  library_error(const std::string &msg) :
97  spkt_error(msg) {
98  }
99  virtual
100  ~library_error() throw () {
101  }
102 };
103 
104 /**
105  * An error indicating something to do with the advancement of simulation time
106  */
107 struct time_error : public spkt_error {
108  time_error(const std::string &msg) :
109  spkt_error(msg) {
110  }
111  virtual
112  ~time_error() throw () {
113  }
114 };
115 
116 /**
117  * File open, read, or write error
118  */
119 struct io_error : public spkt_error {
120  io_error(const std::string &msg) :
121  spkt_error(msg) {
122  }
123  virtual
124  ~io_error() throw () {
125  }
126 };
127 
128 /**
129  * An error indicating some format was not correct
130  */
131 struct illformed_error : public spkt_error {
132  illformed_error(const std::string &msg) :
133  spkt_error(msg) {
134  }
135  virtual
136  ~illformed_error() throw () {
137  }
138 };
139 
140 /**
141  * An error having to do with an operating system
142  */
143 struct os_error : public spkt_error {
144  os_error(const std::string &msg) :
145  spkt_error(msg) {
146  }
147  virtual
148  ~os_error() throw () {
149  }
150 };
151 
152 /**
153  * Something happened when allocating, deallocating, or mapping
154  * a memory region.
155  */
156 struct memory_error : public spkt_error {
157  memory_error(const std::string &msg) :
158  spkt_error(msg) {
159  }
160  virtual
161  ~memory_error() throw () {
162  }
163 };
164 
165 /**
166  * Something happened when iterating over a collection
167  */
168 struct iterator_error : public spkt_error {
169  iterator_error(const std::string &msg) :
170  spkt_error(msg) {
171  }
172  virtual
173  ~iterator_error() throw () {
174  }
175 };
176 
177 /**
178  * A function was intentionally unimplemented because it doesn't make sense,
179  * or it is ongoing work
180  */
182  unimplemented_error(const std::string &msg) :
183  spkt_error(msg) {
184  }
185  virtual
186  ~unimplemented_error() throw () {
187  }
188 };
189 
190 /**
191  * Exception indicating that chosen path is not yet ported to new framework.
192  * Default to older framework.
193 */
194 struct not_ported_error : public spkt_error {
195  not_ported_error(const std::string& msg) :
196  spkt_error(msg) {
197  }
198  virtual ~not_ported_error() throw() {}
199 };
200 
201 /**
202  * Key to a map was not in the map
203  */
204 struct invalid_key_error : public spkt_error {
205  invalid_key_error(const std::string &msg) :
206  spkt_error(msg) {
207  }
208  virtual
209  ~invalid_key_error() throw () {
210  }
211 };
212 
213 /**
214  * An index was out of range in a collection.
215  */
216 struct range_error : public spkt_error {
217  range_error(const std::string &msg) :
218  spkt_error(msg) {
219  }
220  virtual
221  ~range_error() throw () {
222  }
223 };
224 
225 /**
226  * Invalid user input
227  */
228 struct input_error : public spkt_error {
229  input_error(const std::string &msg) :
230  spkt_error(msg) {
231  }
232  virtual
233  ~input_error() throw () {
234  }
235 };
236 
237 } // end of namespace sprockit
238 #endif
239 
240 
A general error somewhere from a library.
Definition: errors.h:95
virtual ~library_error()
Definition: errors.h:100
os_error(const std::string &msg)
Definition: errors.h:144
Something happened when allocating, deallocating, or mapping a memory region.
Definition: errors.h:156
io_error(const std::string &msg)
Definition: errors.h:120
not_ported_error(const std::string &msg)
Definition: errors.h:195
virtual ~os_error()
Definition: errors.h:148
virtual const char * what() const
Definition: errors.h:62
Something happened when iterating over a collection.
Definition: errors.h:168
range_error(const std::string &msg)
Definition: errors.h:217
memory_error(const std::string &msg)
Definition: errors.h:157
An error indicating something to do with the advancement of simulation time.
Definition: errors.h:107
library_error(const std::string &msg)
Definition: errors.h:96
const std::string message
Definition: errors.h:65
File open, read, or write error.
Definition: errors.h:119
virtual ~time_error()
Definition: errors.h:112
Exception indicating that chosen path is not yet ported to new framework.
Definition: errors.h:194
An error indicating some format was not correct.
Definition: errors.h:131
virtual ~iterator_error()
Definition: errors.h:173
virtual ~range_error()
Definition: errors.h:221
Error indicating something was null and shouldn&#39;t have been.
Definition: errors.h:71
Invalid user input.
Definition: errors.h:228
iterator_error(const std::string &msg)
Definition: errors.h:169
null_error(const std::string &msg)
Definition: errors.h:72
time_error(const std::string &msg)
Definition: errors.h:108
An error having to do with an operating system.
Definition: errors.h:143
illformed_error(const std::string &msg)
Definition: errors.h:132
virtual ~illformed_error()
Definition: errors.h:136
value_error(const std::string &msg)
Definition: errors.h:84
invalid_key_error(const std::string &msg)
Definition: errors.h:205
virtual ~spkt_error()
Definition: errors.h:59
input_error(const std::string &msg)
Definition: errors.h:229
virtual ~invalid_key_error()
Definition: errors.h:209
virtual ~not_ported_error()
Definition: errors.h:198
virtual ~input_error()
Definition: errors.h:233
virtual ~io_error()
Definition: errors.h:124
General errors, or base class for more specific errors.
Definition: errors.h:54
A function was intentionally unimplemented because it doesn&#39;t make sense, or it is ongoing work...
Definition: errors.h:181
virtual ~memory_error()
Definition: errors.h:161
spkt_error(const std::string &msg)
Definition: errors.h:55
virtual ~unimplemented_error()
Definition: errors.h:186
Key to a map was not in the map.
Definition: errors.h:204
virtual ~null_error()
Definition: errors.h:76
An index was out of range in a collection.
Definition: errors.h:216
unimplemented_error(const std::string &msg)
Definition: errors.h:182
Error indicating some internal value was unexpected.
Definition: errors.h:83
virtual ~value_error()
Definition: errors.h:88