wibble  1.1
exception.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 #ifndef WIBBLE_EXCEPTION_H
3 #define WIBBLE_EXCEPTION_H
4 
5 /*
6  * Generic base exception hierarchy
7  *
8  * Copyright (C) 2003,2004,2005,2006 Enrico Zini <enrico@debian.org>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23  */
24 
25 #include <wibble/test.h> // for assert
26 #include <exception>
27 #include <typeinfo>
28 #include <string>
29 #include <sstream>
30 #include <iterator>
31 #include <vector>
32 
80 namespace wibble {
81 namespace exception {
82 
84 
92 void DefaultUnexpected();
93 
97 {
98 protected:
99  void (*old)();
100 public:
101  InstallUnexpected(void (*func)() = DefaultUnexpected);
103 };
104 
105 // TODO this needs to be made useful with threading as well
106 struct AddContext {
107  static std::vector< std::string > *s_context;
108 
109  static std::vector< std::string > &context() {
110  if ( s_context )
111  return *s_context;
112  s_context = new std::vector< std::string >();
113  return *s_context;
114  }
115 
116  template< typename O >
117  static void copyContext( O out ) {
118  std::copy( context().begin(), context().end(), out );
119  }
120 
121  std::string m_context;
122 
123  AddContext( std::string s )
124  : m_context( s )
125  {
126  context().push_back( s );
127  }
128 
130  assert_eq( context().back(), m_context );
131  context().pop_back();
132  }
133 };
134 
136 class Context
137 {
138 protected:
139  std::vector<std::string> m_context;
140 
141 public:
142  Context() throw ()
143  {
144  AddContext::copyContext( std::back_inserter( m_context ) );
145  }
146 
147  Context(const std::string& context) throw ()
148  {
149  AddContext::copyContext( std::back_inserter( m_context ) );
151  }
152 
153  void addContext(const std::string& c) throw () { m_context.push_back(c); }
154  std::string formatContext() const throw ()
155  {
156  if (m_context.empty())
157  return "no context information available";
158 
159  std::stringstream res;
160  std::copy( m_context.begin(), m_context.end(),
161  std::ostream_iterator< std::string >( res, ", \n " ) );
162  std::string r = res.str();
163  return std::string( r, 0, r.length() - 7 );
164  }
165 
166  const std::vector<std::string>& context() const throw ()
167  {
168  return m_context;
169  }
170 };
171 
173 
179 class Generic : public std::exception, public Context
180 {
181 protected:
182  mutable std::string m_formatted;
183 
184 public:
185  Generic() throw () {}
186  Generic(const std::string& context) throw () : Context(context) {}
187  virtual ~Generic() throw () {}
188 
190  virtual const char* type() const throw () { return "Generic"; }
191 
193  virtual std::string desc() const throw ()
194  {
195  return "an unspecified problem happened; if you see this message, please report a bug to the maintainer";
196  }
197 
205  virtual const std::string& fullInfo() const throw ()
206  {
207  if (m_formatted.empty())
208  m_formatted = desc() + ". Context:\n "
209  + formatContext();
210  return m_formatted;
211  }
212 
213  virtual const char* what() const throw () { return fullInfo().c_str(); }
214 };
215 
218 
224 class Interrupted : public Generic
225 {
226 public:
227  Interrupted() throw () {}
228  Interrupted(const std::string& context) throw () : Generic(context) {}
229 
230  virtual const char* type() const throw () { return "Interrupted"; }
231 };
232 
234 
242 {
243 public:
244  WaitInterrupted(const std::string& context) throw () :
245  Interrupted(context) {}
246 
247  virtual const char* type() const throw () { return "WaitInterrupted"; }
248 };
249 
251 
254 class Consistency : public Generic
255 {
256  std::string m_error;
257 
258 public:
259  Consistency(const std::string& context, const std::string& error = std::string()) throw () :
260  Generic(context), m_error(error) {}
261  ~Consistency() throw () {}
262 
263  virtual const char* type() const throw () { return "Consistency"; }
264 
265  virtual std::string desc() const throw ()
266  {
267  if (m_error.empty())
268  return "consistency check failed";
269  return m_error;
270  }
271 };
272 
273 struct BadCast : public Consistency
274 {
275  BadCast( const std::string &context ) throw()
276  : Consistency( context )
277  {}
278  ~BadCast() throw() {}
279  virtual std::string typeinfo() const throw() { return "unknown types"; }
280  virtual std::string desc() const throw() {
281  return std::string( "bad cast: " ) + typeinfo();
282  }
283 };
284 
285 #ifndef NO_RTTI
286 template< typename From, typename To >
287 struct BadCastExt : public BadCast
288 {
289  BadCastExt( const std::string &error = std::string() ) throw()
290  : BadCast( error )
291  {}
292  ~BadCastExt() throw() {}
293  virtual std::string typeinfo() const throw() { return std::string( "from " )
294  + typeid( From ).name()
295  + " to "
296  + typeid( To ).name(); }
297 };
298 #endif
299 
309 class OutOfRange : public Consistency
310 {
311 protected:
312  std::string m_var_desc;
313 
314 public:
315  OutOfRange(const std::string& var_desc, const std::string& context) throw ()
317  ~OutOfRange() throw () {}
318 
319  virtual const char* type() const throw () { return "OutOfRange"; }
320 
322  virtual std::string var_desc() const throw () { return m_var_desc; }
323 
324  virtual std::string desc() const throw () { return m_var_desc + " out of range"; }
325 };
326 
328 
343 template <class C>
344 class ValOutOfRange : public OutOfRange
345 {
346 protected:
347  C m_val;
348  C m_inf;
349  C m_sup;
350 
351 public:
355  ValOutOfRange(const std::string& var_desc, C val, C inf, C sup,
356  const std::string& context) throw ()
358  m_val(val), m_inf(inf), m_sup(sup) {}
359 
361 
362  virtual C val() const throw () { return m_val; }
365  virtual C inf() const throw () { return m_inf; }
367  virtual C sup() const throw () { return m_sup; }
369 
370  virtual const char* type() const throw ()
371  {
372  return "ValOutOfRange<>";
373  }
374 
375  virtual std::string desc() const throw ();
376 };
377 
379 
396 class System : public Generic
397 {
398 protected:
399  int m_errno;
400 
401 public:
402  System(const std::string& context) throw ();
403  System(int code, const std::string& context) throw ();
404 
405  virtual const char* type() const throw () { return "System"; }
406 
408  virtual int code() const throw () { return m_errno; }
409 
411  virtual std::string desc() const throw ();
412 };
413 
415 
420 class File : public System
421 {
422 protected:
423  std::string m_name;
424 
425 public:
426  File(const std::string& name, const std::string& context) throw () :
427  System(context), m_name(name) {}
428  ~File() throw () {}
429 
430  virtual const char* type() const throw () { return "File"; }
431 
432  virtual std::string desc() const throw () { return m_name + ": " + System::desc(); }
433 };
434 
435 }
436 }
437 
438 // vim:set ts=4 sw=4:
439 #endif