ReFRACtor
fp_exception.h
Go to the documentation of this file.
1 #ifndef FP_EXCEPTION_H
2 #define FP_EXCEPTION_H
3 #include "printable.h"
4 #include <boost/backtrace.hpp>
5 #include <sstream> // Definition of ostringstream.
6 #include <gsl/gsl_errno.h>
7 
8 namespace FullPhysics {
9 
10 /****************************************************************/
16 class Exception: public std::exception, public Printable<Exception>,
17  public boost::backtrace {
18 public:
19 //-----------------------------------------------------------------------
22 //-----------------------------------------------------------------------
23 
24  Exception(const std::string& W = "")
25  {
26  // This reserve shouldn't really be necessary, but on a Mac
27  // 10.4.11 using gcc 4.0.1, there is some kind of bug where we get
28  // a "Double free" error when printing in Ruby. I never tracked
29  // exactly where this occurred, but it was somewhere in the
30  // iostream library when the buffer of os was resized. We just
31  // reserve enough space up front so this isn't an issue. Since
32  // this only gets called when an exception occurs, there shouldn't
33  // be much of a performance issue with this.
34  std::string buf("blah");
35  buf.reserve(1000);
36  s_.str(buf);
37  s_ << W;
38  }
39 
40 //-----------------------------------------------------------------------
42 //-----------------------------------------------------------------------
43 
44  Exception(const Exception& E)
45  {
46  try {
47  s_ << E.s_.str();
48  } catch(...) { // Ignore all errors.
49  }
50  }
51 
52 //-----------------------------------------------------------------------
54 //-----------------------------------------------------------------------
55 
56  virtual ~Exception() throw() {}
57 
58 //-----------------------------------------------------------------------
60 //-----------------------------------------------------------------------
61 
62  template<class T> inline Exception& operator<<(const T& V)
63  {
64  s_ << V;
65  return *this;
66  }
67 
68 //-----------------------------------------------------------------------
70 //-----------------------------------------------------------------------
71 
72  virtual void print(std::ostream& Os) const
73  {
74  Os << "Full Physics Exception:\n"
75  << "=========================\n"
76  << what() << "\n"
77  << "=========================\n"
78  << "Backtrace:\n"
79  << "=========================\n"
80  << boost::trace(*this) << "\n"
81  << "=========================\n";
82  }
83 
84 //-----------------------------------------------------------------------
86 //-----------------------------------------------------------------------
87 
88  virtual const char* what() const throw()
89  {
90 
91 //-----------------------------------------------------------------------
92 // We can't just do s_.str().c_str(), because the temporary variable
93 // returned by str() disappears when we exit this function. Instead,
94 // we use a scratch variable that has the lifetime of this object.
95 //-----------------------------------------------------------------------
96  try {
97  scratch_ = s_.str();
98  } catch(...) { // If an error condition occurs,
99  // ignore it and return a null
100  // pointer.
101  return 0;
102  }
103  return scratch_.c_str();
104  }
105  private:
106  std::ostringstream s_;
107  mutable std::string scratch_;
108 };
109 
110 
113 //-----------------------------------------------------------------------
115 //-----------------------------------------------------------------------
116 
117 template <class T> inline void range_check_template(
118 const T& Val, // Value to be checked.
119 const T& Min, // Minimum allowed value.
120 const T& Max, // Maximum allowed value.
121 const char* File,
122 int Line
123 )
124 {
125  if(Val < Min ||
126  !(Val < Max)) {
127  Exception e;
128  e << "Out of range error in file " << File << " at line " << Line << "\n"
129  << "Value: " << Val << "\n"
130  << "Minimum allowed: " << Min << "\n"
131  << "Maximum allowed: " << Max;
132  throw e;
133  }
134 }
135 
136 //-----------------------------------------------------------------------
138 //-----------------------------------------------------------------------
139 
140 #define range_check(V, Min, Max) \
141  FullPhysics::range_check_template(V, Min, Max, __FILE__, __LINE__)
142 
143 //-----------------------------------------------------------------------
145 //-----------------------------------------------------------------------
146 
147 template <class T> inline void range_min_check_template(
148 const T& Val, // Value to be checked.
149 const T& Min, // Minimum allowed value.
150 const char* File,
151 int Line
152 )
153 {
154  if(Val < Min) {
155  Exception e;
156  e << "Out of range error in file " << File << " at line " << Line << "\n"
157  << "Value: " << Val << "\n"
158  << "Minimum allowed: " << Min << "\n";
159  throw e;
160  }
161 }
162 
163 //-----------------------------------------------------------------------
165 //-----------------------------------------------------------------------
166 
167 #define range_min_check(V, Min) \
168  FullPhysics::range_min_check_template(V, Min, __FILE__, __LINE__)
169 
170 //-----------------------------------------------------------------------
172 //-----------------------------------------------------------------------
173 
174 template <class T> inline void range_max_check_template(
175 const T& Val, // Value to be checked.
176 const T& Max, // Maximum allowed value.
177 const char* File,
178 int Line
179 )
180 {
181  if(!(Val < Max)) {
182  Exception e;
183  e << "Out of range error in file " << File << " at line " << Line << "\n"
184  << "Value: " << Val << "\n"
185  << "Maximum allowed: " << Max << "\n";
186  throw e;
187  }
188 }
189 
190 //-----------------------------------------------------------------------
192 //-----------------------------------------------------------------------
193 
194 #define range_max_check(V, Max) \
195  FullPhysics::range_max_check_template(V, Max, __FILE__, __LINE__)
196 
197 //-----------------------------------------------------------------------
199 //-----------------------------------------------------------------------
200 
201 inline void no_gsl_abort()
202 {
203  gsl_set_error_handler_off();
204 }
205 
206 //-----------------------------------------------------------------------
208 //-----------------------------------------------------------------------
209 
210 inline void gsl_check_func
211 (int status,
212 const char* File,
213 int Line
214 )
215 {
216  if(status != 0) {
217  Exception e;
218  e << "GSL error in file " << File << " at line " << Line << "\n"
219  << "GSL error: " << gsl_strerror(status) << "\n";
220  throw e;
221  }
222 }
223 
224 //-----------------------------------------------------------------------
226 //-----------------------------------------------------------------------
227 
228 #define gsl_check(status) \
229  FullPhysics::gsl_check_func(status, __FILE__, __LINE__)
230 
231 }
233 #endif
virtual const char * what() const
Description of what the error is.
Definition: fp_exception.h:88
void no_gsl_abort()
Turn off gsl errors abort.
Definition: fp_exception.h:201
void gsl_check_func(int status, const char *File, int Line)
Check for gsl errors.
Definition: fp_exception.h:211
void range_min_check_template(const T &Val, const T &Min, const char *File, int Line)
Range check.
Definition: fp_exception.h:147
This is the base of the exception hierarchy for Full Physics code.
Definition: fp_exception.h:16
Exception(const std::string &W="")
Default constructor.
Definition: fp_exception.h:24
This is a Mixin for classes that can be printed.
Definition: printable.h:24
Exception & operator<<(const T &V)
Write to exception what() string.
Definition: fp_exception.h:62
details::trace_manip trace(E const &e)
Definition: backtrace.hpp:184
virtual void print(std::ostream &Os) const
Print out description of object.
Definition: fp_exception.h:72
void range_max_check_template(const T &Val, const T &Max, const char *File, int Line)
Range check.
Definition: fp_exception.h:174
Contains classes to abstract away details in various Spurr Radiative Transfer software.
Definition: doxygen_python.h:1
virtual ~Exception()
Destructor.
Definition: fp_exception.h:56
Exception(const Exception &E)
Copy constructor.
Definition: fp_exception.h:44
void range_check_template(const T &Val, const T &Min, const T &Max, const char *File, int Line)
Range check.
Definition: fp_exception.h:117
const Unit W("W", J/s)

Copyright © 2017, California Institute of Technology.
ALL RIGHTS RESERVED.
U.S. Government Sponsorship acknowledged.
Generated Fri Aug 24 2018 15:44:11