err_hnd.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2017, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_ERR_HND_H
24 #define O2SCL_ERR_HND_H
25 
26 /** \file err_hnd.h
27  \brief Error handling classes, \ref o2scl::err_hnd_type and
28  \ref o2scl::err_hnd_gsl
29 
30  See also \ref exception.h .
31 */
32 
33 #include <iostream>
34 #include <string>
35 
36 #ifndef DOXYGEN_NO_O2NS
37 namespace o2scl {
38 #endif
39 
40  /** \brief The integer error definitions
41 
42  The errors associated with the integers between -2 and 32
43  are based on GSL, the rest are specific to \o2 .
44  */
45  enum {
46  /// Success
48  /// Failure
50  /// iteration has not converged
52  /// input domain error, e.g sqrt(-1)
54  /// output range error, e.g. exp(1e100)
56  /// invalid pointer
58  /// invalid argument supplied by user
60  /// generic failure
62  /// factorization failed
64  /// sanity check failed - shouldn't happen
66  /// malloc failed
68  /// problem with user-supplied function
70  /// iterative process is out of control
72  /// exceeded max number of iterations
74  /// tried to divide by zero
76  /// user specified an invalid tolerance
78  /// failed to reach the specified tolerance
79  exc_etol=14,
80  /// underflow
82  /// overflow
84  /// loss of accuracy
85  exc_eloss=17,
86  /// failed because of roundoff error
88  /// matrix, vector lengths are not conformant
90  /// matrix not square
92  /// apparent singularity detected
93  exc_esing=21,
94  /// integral or series is divergent
96  /// requested feature is not supported by the hardware
98  /// requested feature not (yet) implemented
100  /// cache limit exceeded
102  /// \table limit exceeded
104  /// iteration is not making progress toward solution
106  /// \jacobian evaluations are not improving the solution
108  /// cannot reach the specified tolerance in f
110  /// cannot reach the specified tolerance in x
112  /// cannot reach the specified tolerance in \gradient
114  /// end of file
115  exc_eof=32,
116  /// Generic "not found" result
118  /// Incorrect type for memory object
120  /// File not found
122  /// Invalid index for array or matrix
124  /// Outside constraint region
126  };
127 
128  // Forward declaration
129  class err_hnd_type;
130 
131  /** \brief The global error handler pointer
132  */
133  extern err_hnd_type *err_hnd;
134 
135  /** \brief Class defining an error handler [abstract base]
136 
137  A global object of this type is defined, \ref err_hnd .
138 
139  \future There may be an issue associated with the string
140  manipulations causing errors in the error handler.
141  */
142  class err_hnd_type {
143 
144  public:
145 
146  err_hnd_type() {}
147 
148  virtual ~err_hnd_type() {}
149 
150  /** \brief Set an error
151 
152  This is separate from set(), since the gsl error handler
153  needs to be a static function.
154  */
155  static void gsl_hnd(const char *reason, const char *file,
156  int line, int lerrno) {
157  err_hnd->set(reason,file,line,lerrno);
158  }
159 
160  /// Set an error
161  virtual void set(const char *reason, const char *file,
162  int line, int lerrno)=0;
163 
164  /// Get the last error
165  virtual void get(const char *&reason, const char *&file,
166  int &line, int &lerrno)=0;
167 
168  /// Return the last error number
169  virtual int get_errno() const=0;
170 
171  /// Return the line number of the last error
172  virtual int get_line() const=0;
173 
174  /// Return the reason for the last error
175  virtual const char *get_reason() const=0;
176 
177  /// Return the file name of the last error
178  virtual const char *get_file() const=0;
179 
180  /// Return a string summarizing the last error
181  virtual const char *get_str()=0;
182 
183  /// Remove last error information
184  virtual void reset()=0;
185 
186  /// Return type
187  virtual const char *type() const=0;
188 
189  };
190 
191  /** \brief The error handler
192 
193  An error handler for use in \o2 which replaces the GSL error handler
194 
195  Note that the string arguments to set() can refer to temporary
196  storage, since they are copied when the function is called and
197  an error is set.
198  */
199  class err_hnd_gsl : public err_hnd_type {
200 
201  public:
202 
203  err_hnd_gsl();
204 
205  virtual ~err_hnd_gsl() {}
206 
207  /// Set an error
208  virtual void set(const char *reason, const char *file,
209  int line, int lerrno);
210 
211  /// Get the last error
212  virtual void get(const char *&reason, const char *&file,
213  int &line, int &lerrno);
214 
215  /// Return the last error number
216  virtual int get_errno() const;
217 
218  /// Return the line number of the last error
219  virtual int get_line() const;
220 
221  /// Return the reason for the last error
222  virtual const char *get_reason() const;
223 
224  /// Return the file name of the last error
225  virtual const char *get_file() const;
226 
227  /// Return a string summarizing the last error
228  virtual const char *get_str();
229 
230  /// Remove last error information
231  virtual void reset();
232 
233  /// Number of characters from filename to print (default 28)
234  size_t fname_size;
235 
236  /// Return type ("err_hnd_gsl")
237  virtual const char *type() const { return "err_hnd_gsl"; }
238 
239  protected:
240 
241 #ifndef DOXYGEN_INTERNAL
242 
243  /// Convert an error number to a string
244  std::string errno_to_string(int errnox);
245 
246  /// The maximum size of error explanations
247  static const int rsize=300;
248  /// The maximum size of error explanations with the line and file info
249  static const int fsize=400;
250 
251  /// The error number
252  int a_errno;
253  /// The line number
254  int a_line;
255 
256  /// The filename
257  char *a_file;
258  /// The error explanation
259  char a_reason[rsize];
260  /// A full string with explanation and line and file info
261  char fullstr[fsize];
262 
263 #endif
264 
265  };
266 
267  /** \brief An alternate GSL-like error handler
268  */
269  extern err_hnd_gsl alt_err_hnd;
270 
271  /** \brief Set an error with message \c d and code \c n
272  */
273 #define O2SCL_ERR(d,n) o2scl::set_err_fn(d,__FILE__,__LINE__,n);
274 
275  /** \brief Set a "convergence" error
276  */
277 #define O2SCL_CONV(d,n,b) {if (b) o2scl::set_err_fn(d,__FILE__,__LINE__,n);}
278 
279  /** \brief Set an error, two-string version
280  */
281 #define O2SCL_ERR2(d,d2,n) o2scl::set_err_fn((std::string(d)+d2).c_str(), \
282  __FILE__,__LINE__,n);
283 
284  /** \brief Set a "convergence" error, two-string version
285  */
286 #define O2SCL_CONV2(d,d2,n,b) {if (b) \
287  o2scl::set_err_fn((std::string(d)+d2).c_str(), \
288  __FILE__,__LINE__,n);}
289 
290  /** \brief Set a "convergence" error and return the error value
291  */
292 #define O2SCL_CONV_RET(d,n,b) \
293  do { if (!b) { return n; } else { \
294  o2scl::set_err_fn(d,__FILE__,__LINE__,n); return n; } } while (0)
295 
296  /** \brief Set an error and return the error value, two-string version
297  */
298 #define O2SCL_CONV2_RET(d,d2,n,b) \
299  do { if (!b) { return n; } else { \
300  o2scl::set_err_fn((std::string(d)+d2).c_str(), \
301  __FILE__,__LINE__,n); return n; } } while (0)
302 
303  /// \name The error handler function
304  //@{
305  /** \brief Call the error handler
306  */
307  inline void set_err_fn(const char *desc, const char *file, int line,
308  int errnum) {
309  err_hnd->set(desc,file,line,errnum);
310  return;
311  }
312  //@}
313 
314  /** \brief Update an error value \c err with the value in \c ret
315 
316  If \c ret is zero, this sets \c ret to the value \c err, and
317  if \c ret is nonzero this function does nothing.
318  */
319  inline void error_update(int &ret, int err) { if (ret==0) ret=err; }
320 
321 #ifdef O2SCL_NEVER_DEFINED
322 
323  /** \brief A version of \c assert, i.e. exit if the error value is
324  non-zero and do nothing otherwise
325 
326  \future Make this consistent with assert() using NDEBUG?
327  */
328 #define O2SCL_ASSERT(ev) \
329  do { if (ev!=0) { std::cout << "O2scl: Macro err_assert() causing exit" \
330  << " from error " << ev << " at " \
331  << __LINE__ << " in file:\n " \
332  << __FILE__ << std::endl; \
333  std::cout << "Error handler string:\n " << err_hnd->get_str() \
334  << std::endl; exit(ev); } } while (0)
335 
336  /** \brief A version of \c assert for bool types. Exit if the argument
337  is false
338  */
339 #define O2SCL_BOOL_ASSERT(ev,str) \
340  do { if (ev==false) { \
341  std::cout << "O2scl: Macro bool_assert() causing exit at line " \
342  << __LINE__ << " in file:\n " \
343  << __FILE__ << std::endl; \
344  std::cout << "Given string: " << str \
345  << std::endl; exit(-1); } } while (0)
346 
347 #endif
348 
349 #ifndef DOXYGEN_NO_O2NS
350 }
351 #endif
352 
353 #endif
static void gsl_hnd(const char *reason, const char *file, int line, int lerrno)
Set an error.
Definition: err_hnd.h:155
iterative process is out of control
Definition: err_hnd.h:71
virtual const char * type() const =0
Return type.
Class defining an error handler [abstract base].
Definition: err_hnd.h:142
invalid pointer
Definition: err_hnd.h:57
malloc failed
Definition: err_hnd.h:67
underflow
Definition: err_hnd.h:81
tried to divide by zero
Definition: err_hnd.h:75
The main O<span style=&#39;position: relative; top: 0.3em; font-size: 0.8em&#39;>2</span>scl O$_2$scl names...
Definition: anneal.h:42
err_hnd_gsl alt_err_hnd
An alternate GSL-like error handler.
err_hnd_type * err_hnd
The global error handler pointer.
void set_err_fn(const char *desc, const char *file, int line, int errnum)
Call the error handler.
Definition: err_hnd.h:307
sanity check failed - shouldn&#39;t happen
Definition: err_hnd.h:65
overflow
Definition: err_hnd.h:83
invalid argument supplied by user
Definition: err_hnd.h:59
requested feature is not supported by the hardware
Definition: err_hnd.h:97
apparent singularity detected
Definition: err_hnd.h:93
exceeded max number of iterations
Definition: err_hnd.h:73
failed to reach the specified tolerance
Definition: err_hnd.h:79
virtual int get_line() const =0
Return the line number of the last error.
table table limit exceeded
Definition: err_hnd.h:103
iteration has not converged
Definition: err_hnd.h:51
void error_update(int &ret, int err)
Update an error value err with the value in ret.
Definition: err_hnd.h:319
Generic "not found" result.
Definition: err_hnd.h:117
generic failure
Definition: err_hnd.h:61
virtual const char * type() const
Return type ("err_hnd_gsl")
Definition: err_hnd.h:237
requested feature not (yet) implemented
Definition: err_hnd.h:99
iteration is not making progress toward solution
Definition: err_hnd.h:105
matrix, vector lengths are not conformant
Definition: err_hnd.h:89
virtual const char * get_str()=0
Return a string summarizing the last error.
Failure.
Definition: err_hnd.h:49
char * a_file
The filename.
Definition: err_hnd.h:257
jacobian jacobian evaluations are not improving the solution
Definition: err_hnd.h:107
input domain error, e.g sqrt(-1)
Definition: err_hnd.h:53
virtual const char * get_reason() const =0
Return the reason for the last error.
matrix not square
Definition: err_hnd.h:91
int a_errno
The error number.
Definition: err_hnd.h:252
factorization failed
Definition: err_hnd.h:63
The error handler.
Definition: err_hnd.h:199
virtual void reset()=0
Remove last error information.
cannot reach the specified tolerance in x
Definition: err_hnd.h:111
cannot reach the specified tolerance in gradient gradient
Definition: err_hnd.h:113
output range error, e.g. exp(1e100)
Definition: err_hnd.h:55
Outside constraint region.
Definition: err_hnd.h:125
int a_line
The line number.
Definition: err_hnd.h:254
File not found.
Definition: err_hnd.h:121
problem with user-supplied function
Definition: err_hnd.h:69
virtual void set(const char *reason, const char *file, int line, int lerrno)=0
Set an error.
cache limit exceeded
Definition: err_hnd.h:101
Incorrect type for memory object.
Definition: err_hnd.h:119
virtual int get_errno() const =0
Return the last error number.
size_t fname_size
Number of characters from filename to print (default 28)
Definition: err_hnd.h:234
Invalid index for array or matrix.
Definition: err_hnd.h:123
loss of accuracy
Definition: err_hnd.h:85
virtual const char * get_file() const =0
Return the file name of the last error.
user specified an invalid tolerance
Definition: err_hnd.h:77
end of file
Definition: err_hnd.h:115
Success.
Definition: err_hnd.h:47
failed because of roundoff error
Definition: err_hnd.h:87
integral or series is divergent
Definition: err_hnd.h:95
cannot reach the specified tolerance in f
Definition: err_hnd.h:109

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).