CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Exception.h
Go to the documentation of this file.
1 #ifndef FWCore_Utilities_Exception_h
2 #define FWCore_Utilities_Exception_h
3 
35 #include <list>
36 #include <sstream>
37 #include <string>
38 #include <exception>
39 
40 #include "boost/type_traits/is_base_and_derived.hpp"
41 
43 
44 namespace cms {
45 
46  namespace detail {
47  // The struct template Desired exists in order to allow us to use
48  // SFINAE to control the instantiation of the stream insertion
49  // member template needed to support streaming output to an object
50  // of type cms::Exception, or a subclass of cms::Exception.
51 
52  template <typename T, bool b> struct Desired;
53  template <typename T> struct Desired<T, true> { typedef T type; };
54 
55 
56  // The following struct template is a metafunction which combines
57  // two of the boost type_traits metafunctions.
58 
59  template <typename BASE, typename DERIVED>
61  static bool const value =
63  };
64 
65  }
66 
67 
69  public:
70 
71  explicit Exception(std::string const& aCategory);
72  explicit Exception(char const* aCategory);
73 
74  Exception(std::string const& aCategory,
75  std::string const& message);
76  Exception(char const* aCategory,
77  std::string const& message);
78  Exception(std::string const& aCategory,
79  char const* message);
80  Exception(char const* aCategory,
81  char const* message);
82 
83  Exception(std::string const& aCategory,
84  std::string const& message,
85  Exception const& another);
86 
87  Exception(Exception const& other);
88  virtual ~Exception() throw();
89 
90  void swap(Exception& other);
91 
92  Exception& operator=(Exception const& other);
93 
94  // The signature for what() must be identical to that of std::exception::what().
95  virtual char const* what() const throw();
96 
97  virtual std::string explainSelf() const;
98 
99  std::string const& category() const;
100  std::string message() const;
101  std::list<std::string> const& context() const;
102  std::list<std::string> const& additionalInfo() const;
103  int returnCode() const;
104 
105  void raise() {rethrow();}
106 
107  void append(Exception const& another);
108  void append(std::string const& more_information);
109  void append(char const* more_information);
110 
111  void clearMessage();
112  void clearContext();
113  void clearAdditionalInfo();
114 
115  void addContext(std::string const& context);
116  void addContext(char const* context);
117 
118  void addAdditionalInfo(std::string const& info);
119  void addAdditionalInfo(char const* info);
120 
121  void setContext(std::list<std::string> const& context);
122  void setAdditionalInfo(std::list<std::string> const& info);
123 
124  bool alreadyPrinted() const;
125  void setAlreadyPrinted(bool value);
126 
127  virtual Exception* clone() const;
128 
129  // In the following templates, class E is our Exception class or
130  // any subclass thereof. The complicated return type exists to
131  // make sure the template can only be instantiated for such
132  // classes.
133  //
134 
135  // We have provided versions of these templates which accept, and
136  // return, reference-to-const objects of type E. These are needed
137  // so that the expression:
138  //
139  // throw Exception("category") << "some message";
140  //
141  // shall compile. The technical reason is that the un-named
142  // temporary created by the explicit call to the constructor
143  // allows only const access, except by member functions. It seems
144  // extremely unlikely that any Exception object will be created in
145  // read-only memory; thus it seems unlikely that allowing the
146  // stream operators to write into a nominally 'const' Exception
147  // object is a real danger.
148 
149  template <typename E, typename T>
150  friend
152  operator<<(E& e, T const& stuff);
153 
154  template <typename E, typename T>
155  friend
157  operator<<(E const& e, T const& stuff);
158 
159  template <typename E>
160  friend
162  operator<<(E& e, std::ostream&(*f)(std::ostream&));
163 
164  template <typename E>
165  friend
167  operator<<(E const& e, std::ostream&(*f)(std::ostream&));
168 
169 
170  template <typename E>
171  friend
173  operator<<(E& e, std::ios_base&(*f)(std::ios_base&));
174 
175  template <typename E>
176  friend
178  operator<<(E const& e, std::ios_base&(*f)(std::ios_base&));
179 
180  // The following two function templates should be included, to help
181  // reduce the number of function templates instantiated. However,
182  // GCC 3.2.3 crashes with an internal compiler error when
183  // instantiating these functions.
184 
185  // template <typename E>
186  // friend
187  // typename detail::Desired<E, (boost::is_base_and_derived<Exception,E>::value ||
188  // boost::is_same<Exception,E>::value)>::type &
189  // operator<<(E& e, const char*);
190 
191  // template <typename E>
192  // friend
193  // typename detail::Desired<E, (boost::is_base_and_derived<Exception,E>::value ||
194  // boost::is_same<Exception,E>::value)>::type &
195  // operator<<(E& e, char*);
196 
197  // This function is deprecated and we are in the process of removing
198  // all code that uses it from CMSSW. It will then be deleted.
199  std::list<std::string> history() const;
200 
201  private:
202 
203  void init(std::string const& message);
204  virtual void rethrow();
205  virtual int returnCode_() const;
206 
207  // data members
208  std::ostringstream ost_;
211  std::list<std::string> context_;
212  std::list<std::string> additionalInfo_;
214  };
215 
216  inline
217  std::ostream&
218  operator<<(std::ostream& ost, Exception const& e)
219  {
220  ost << e.explainSelf();
221  return ost;
222  }
223 
224  // -------- implementation ---------
225 
226  template <typename E, typename T>
227  inline
229  operator<<(E& e, T const& stuff)
230  {
231  e.ost_ << stuff;
232  return e;
233  }
234 
235  template <typename E, typename T>
236  inline
238  operator<<(E const& e, T const& stuff)
239  {
240  E& ref = const_cast<E&>(e);
241  ref.ost_ << stuff;
242  return e;
243  }
244 
245  template <typename E>
246  inline
248  operator<<(E& e, std::ostream&(*f)(std::ostream&))
249  {
250  f(e.ost_);
251  return e;
252  }
253 
254  template <typename E>
255  inline
257  operator<<(E const& e, std::ostream&(*f)(std::ostream&))
258  {
259  E& ref = const_cast<E&>(e);
260  f(ref.ost_);
261  return e;
262  }
263 
264  template <typename E>
265  inline
267  operator<<(E& e, std::ios_base&(*f)(std::ios_base&))
268  {
269  f(e.ost_);
270  return e;
271  }
272 
273 
274  template <typename E>
275  inline
277  operator<<(E const& e, std::ios_base&(*f)(std::ios_base&))
278  {
279  E& ref = const_cast<E&>(e);
280  f(ref.ost_);
281  return e;
282  }
283 
284  // The following four function templates should be included, to help
285  // reduce the number of function templates instantiated. However,
286  // GCC 3.2.3 crashes with an internal compiler error when
287  // instantiating these functions.
288 
289  // template <typename E>
290  // inline
291  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type &
292  // operator<<(E& e, char const* c)
293  // {
294  // e.ost_ << c;
295  // return e;
296  // }
297 
298  // template <typename E>
299  // inline
300  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type const&
301  // operator<<(E const& e, char const* c)
302  // {
303  // E& ref = const_cast<E&>(e);
304  // ref.ost_ << c;
305  // return e;
306  // }
307 
308  // template <typename E>
309  // inline
310  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type &
311  // operator<<(E& e, char* c)
312  // {
313  // e.ost_ << c;
314  // return e;
315  // }
316 
317  // template <typename E>
318  // inline
319  // typename detail::Desired<E, detail::is_derived_or_same<Exception,E>::value>::type const&
320  // operator<<(E const& e, char* c)
321  // {
322  // E& ref = const_cast<E&>(e);
323  // ref.ost_ << c;
324  // return e;
325  // }
326 }
327 
328 #endif
void swap(ora::Record &rh, ora::Record &lh)
Definition: Record.h:70
type
Definition: HCALResponse.h:21
std::list< std::string > additionalInfo_
Definition: Exception.h:212
static const TGPicture * info(bool iBackgroundIsBlack)
virtual std::string explainSelf() const
Definition: Exception.cc:146
int init
Definition: HydjetWrapper.h:67
std::ostringstream ost_
Definition: Exception.h:208
#define dso_export
std::string category_
Definition: Exception.h:209
double f[11][100]
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
static bool const value
Definition: Exception.h:61
std::list< std::string > context_
Definition: Exception.h:211
std::ostream & operator<<(std::ostream &os, MD5Result const &r)
Definition: Digest.h:46
bool alreadyPrinted_
Definition: Exception.h:213
std::string what_
Definition: Exception.h:210
long double T