exception.h
Go to the documentation of this file.
// -*- C++ -*-
#ifndef WIBBLE_EXCEPTION_H
#define WIBBLE_EXCEPTION_H
/*
* Generic base exception hierarchy
*
* Copyright (C) 2003,2004,2005,2006 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <wibble/test.h> // for assert
#include <exception>
#include <typeinfo>
#include <string>
#include <sstream>
#include <iterator>
#include <vector>
namespace wibble {
namespace exception {
void DefaultUnexpected();
class InstallUnexpected
{
protected:
void (*old)();
public:
InstallUnexpected(void (*func)() = DefaultUnexpected);
~InstallUnexpected();
};
// TODO this needs to be made useful with threading as well
struct AddContext {
static std::vector< std::string > *s_context;
static std::vector< std::string > &context() {
if ( s_context )
return *s_context;
s_context = new std::vector< std::string >();
return *s_context;
}
template< typename O >
static void copyContext( O out ) {
std::copy( context().begin(), context().end(), out );
}
std::string m_context;
AddContext( std::string s )
: m_context( s )
{
context().push_back( s );
}
~AddContext() {
assert_eq( context().back(), m_context );
context().pop_back();
}
};
class Context
{
protected:
std::vector<std::string> m_context;
public:
Context() throw ()
{
AddContext::copyContext( std::back_inserter( m_context ) );
}
Context(const std::string& context) throw ()
{
AddContext::copyContext( std::back_inserter( m_context ) );
addContext(context);
}
void addContext(const std::string& c) throw () { m_context.push_back(c); }
std::string formatContext() const throw ()
{
if (m_context.empty())
return "no context information available";
std::stringstream res;
std::copy( m_context.begin(), m_context.end(),
std::ostream_iterator< std::string >( res, ", \n " ) );
std::string r = res.str();
return std::string( r, 0, r.length() - 7 );
}
const std::vector<std::string>& context() const throw ()
{
return m_context;
}
};
class Generic : public std::exception, public Context
{
protected:
mutable std::string m_formatted;
public:
Generic() throw () {}
Generic(const std::string& context) throw () : Context(context) {}
virtual ~Generic() throw () {}
virtual const char* type() const throw () { return "Generic"; }
virtual std::string desc() const throw ()
{
return "an unspecified problem happened; if you see this message, please report a bug to the maintainer";
}
virtual const std::string& fullInfo() const throw ()
{
if (m_formatted.empty())
m_formatted = desc() + ". Context:\n "
+ formatContext();
return m_formatted;
}
virtual const char* what() const throw () { return fullInfo().c_str(); }
};
class Interrupted : public Generic
{
public:
Interrupted() throw () {}
Interrupted(const std::string& context) throw () : Generic(context) {}
virtual const char* type() const throw () { return "Interrupted"; }
};
class WaitInterrupted : public Interrupted
{
public:
WaitInterrupted(const std::string& context) throw () :
Interrupted(context) {}
virtual const char* type() const throw () { return "WaitInterrupted"; }
};
class Consistency : public Generic
{
std::string m_error;
public:
Consistency(const std::string& context, const std::string& error = std::string()) throw () :
Generic(context), m_error(error) {}
~Consistency() throw () {}
virtual const char* type() const throw () { return "Consistency"; }
virtual std::string desc() const throw ()
{
if (m_error.empty())
return "consistency check failed";
return m_error;
}
};
struct BadCast : public Consistency
{
BadCast( const std::string &context ) throw()
: Consistency( context )
{}
~BadCast() throw() {}
virtual std::string typeinfo() const throw() { return "unknown types"; }
virtual std::string desc() const throw() {
return std::string( "bad cast: " ) + typeinfo();
}
};
#ifndef NO_RTTI
template< typename From, typename To >
struct BadCastExt : public BadCast
{
BadCastExt( const std::string &error = std::string() ) throw()
: BadCast( error )
{}
~BadCastExt() throw() {}
virtual std::string typeinfo() const throw() { return std::string( "from " )
+ typeid( From ).name()
+ " to "
+ typeid( To ).name(); }
};
#endif
class OutOfRange : public Consistency
{
protected:
std::string m_var_desc;
public:
OutOfRange(const std::string& var_desc, const std::string& context) throw ()
: Consistency(context), m_var_desc(var_desc) {}
~OutOfRange() throw () {}
virtual const char* type() const throw () { return "OutOfRange"; }
virtual std::string var_desc() const throw () { return m_var_desc; }
virtual std::string desc() const throw () { return m_var_desc + " out of range"; }
};
template <class C>
class ValOutOfRange : public OutOfRange
{
protected:
C m_val;
C m_inf;
C m_sup;
public:
ValOutOfRange(const std::string& var_desc, C val, C inf, C sup,
const std::string& context) throw ()
: OutOfRange(var_desc, context),
m_val(val), m_inf(inf), m_sup(sup) {}
virtual C val() const throw () { return m_val; }
virtual C inf() const throw () { return m_inf; }
virtual C sup() const throw () { return m_sup; }
virtual const char* type() const throw ()
{
return "ValOutOfRange<>";
}
virtual std::string desc() const throw ();
};
class System : public Generic
{
protected:
int m_errno;
public:
System(const std::string& context) throw ();
System(int code, const std::string& context) throw ();
virtual const char* type() const throw () { return "System"; }
virtual int code() const throw () { return m_errno; }
virtual std::string desc() const throw ();
};
class File : public System
{
protected:
std::string m_name;
public:
File(const std::string& name, const std::string& context) throw () :
System(context), m_name(name) {}
~File() throw () {}
virtual const char* type() const throw () { return "File"; }
virtual std::string desc() const throw () { return m_name + ": " + System::desc(); }
};
}
}
// vim:set ts=4 sw=4:
#endif
wibble::exception::System::m_errno
int m_errno
Definition: exception.h:399
wibble::exception::InstallUnexpected::old
void(* old)()
Definition: exception.h:99
wibble::exception::Context
Store context information for an exception.
Definition: exception.h:136
wibble::exception::File
Base class for exceptions for file I/O.
Definition: exception.h:420
wibble::exception::AddContext::context
static std::vector< std::string > & context()
Definition: exception.h:109
wibble::exception::OutOfRange::var_desc
virtual std::string var_desc() const
Get a short description of the variable that has been checked.
Definition: exception.h:322
wibble::exception::ValOutOfRange::m_sup
C m_sup
Definition: exception.h:349
wibble::exception::OutOfRange::~OutOfRange
~OutOfRange()
Definition: exception.h:317
wibble::exception::BadCast::desc
virtual std::string desc() const
Get a string describing what happened that threw the exception.
Definition: exception.h:280
wibble::exception::Generic::what
virtual const char * what() const
Definition: exception.h:213
wibble::exception::Generic::~Generic
virtual ~Generic()
Definition: exception.h:187
wibble::exception::AddContext::copyContext
static void copyContext(O out)
Definition: exception.h:117
wibble::exception::Consistency::~Consistency
~Consistency()
Definition: exception.h:261
wibble::exception::ValOutOfRange::ValOutOfRange
ValOutOfRange(const std::string &var_desc, C val, C inf, C sup, const std::string &context)
Construct the exception; minBound and maxBound are the bounds of the valid index range (inclusive)...
Definition: exception.h:355
test.h
wibble::exception::Generic::Generic
Generic()
Definition: exception.h:185
wibble::exception::Consistency
Exception thrown when some consistency check fails.
Definition: exception.h:254
wibble::exception::BadCastExt::typeinfo
virtual std::string typeinfo() const
Definition: exception.h:293
wibble::exception::System::desc
virtual std::string desc() const
Get the description of the error code.
wibble::exception::System::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:405
wibble::exception::OutOfRange::desc
virtual std::string desc() const
Get a string describing what happened that threw the exception.
Definition: exception.h:324
wibble::exception::OutOfRange::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:319
wibble::exception::ValOutOfRange::sup
virtual C sup() const
Get the maximum allowed value for this index.
Definition: exception.h:367
wibble::exception::BadCast::BadCast
BadCast(const std::string &context)
Definition: exception.h:275
wibble::exception::ValOutOfRange
Exception thrown when index checking fails.
Definition: exception.h:344
wibble::exception::Generic
Base class for all exceptions.
Definition: exception.h:179
wibble::list::begin
ListIterator< List > begin(List l)
Definition: list.h:420
assert_eq
#define assert_eq(x, y)
Definition: test.h:33
wibble::exception::Context::Context
Context()
Definition: exception.h:142
wibble::exception::ValOutOfRange::inf
virtual C inf() const
Get the minimum allowed value for this index.
Definition: exception.h:365
wibble::exception::BadCast
Definition: exception.h:273
wibble::exception::BadCast::typeinfo
virtual std::string typeinfo() const
Definition: exception.h:279
wibble::exception::AddContext::m_context
std::string m_context
Definition: exception.h:121
wibble::exception::File::~File
~File()
Definition: exception.h:428
wibble::exception::Interrupted
Exception thrown when some long event is interrupted by an external event (like a system call being i...
Definition: exception.h:224
wibble::exception::WaitInterrupted
Exception thrown when some system wait is interrupted by a signal.
Definition: exception.h:241
wibble::exception::Consistency::Consistency
Consistency(const std::string &context, const std::string &error=std::string())
Definition: exception.h:259
wibble::exception::Generic::m_formatted
std::string m_formatted
Definition: exception.h:182
wibble::exception::AddContext::s_context
static std::vector< std::string > * s_context
Definition: exception.h:107
wibble::exception::Interrupted::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:230
wibble::exception::AddContext::~AddContext
~AddContext()
Definition: exception.h:129
wibble::exception::ValOutOfRange::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:370
wibble::exception::System::code
virtual int code() const
Get the system error code associated to the exception.
Definition: exception.h:408
wibble::exception::Interrupted::Interrupted
Interrupted()
Definition: exception.h:227
wibble::exception::Consistency::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:263
wibble::exception::AddContext::AddContext
AddContext(std::string s)
Definition: exception.h:123
wibble::exception::Generic::desc
virtual std::string desc() const
Get a string describing what happened that threw the exception.
Definition: exception.h:193
wibble::exception::InstallUnexpected
Install an unexpected handler for the duration of its scope.
Definition: exception.h:96
wibble::exception::Interrupted::Interrupted
Interrupted(const std::string &context)
Definition: exception.h:228
wibble::exception::Context::context
const std::vector< std::string > & context() const
Definition: exception.h:166
wibble::exception::DefaultUnexpected
void DefaultUnexpected()
Basic unexpected handler.
Definition: exception.cpp:69
wibble::exception::WaitInterrupted::WaitInterrupted
WaitInterrupted(const std::string &context)
Definition: exception.h:244
wibble::exception::Generic::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:190
wibble::exception::ValOutOfRange::m_val
C m_val
Definition: exception.h:347
wibble::exception::File::File
File(const std::string &name, const std::string &context)
Definition: exception.h:426
wibble::exception::ValOutOfRange::m_inf
C m_inf
Definition: exception.h:348
wibble::exception::File::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:430
wibble::exception::BadCastExt::~BadCastExt
~BadCastExt()
Definition: exception.h:292
wibble::exception::Context::Context
Context(const std::string &context)
Definition: exception.h:147
wibble::exception::Generic::fullInfo
virtual const std::string & fullInfo() const
Format in a string all available information about the exception.
Definition: exception.h:205
wibble
Definition: amorph.h:17
wibble::exception::OutOfRange
Exception thrown when some value is out of range.
Definition: exception.h:309
wibble::exception::File::m_name
std::string m_name
Definition: exception.h:423
wibble::exception::Context::formatContext
std::string formatContext() const
Definition: exception.h:154
wibble::exception::AddContext
Definition: exception.h:106
wibble::exception::InstallUnexpected::InstallUnexpected
InstallUnexpected(void(*func)()=DefaultUnexpected)
Definition: exception.cpp:76
wibble::exception::File::desc
virtual std::string desc() const
Get the description of the error code.
Definition: exception.h:432
wibble::exception::OutOfRange::OutOfRange
OutOfRange(const std::string &var_desc, const std::string &context)
Definition: exception.h:315
wibble::exception::Context::m_context
std::vector< std::string > m_context
Definition: exception.h:139
wibble::exception::BadCast::~BadCast
~BadCast()
Definition: exception.h:278
wibble::exception::Consistency::desc
virtual std::string desc() const
Get a string describing what happened that threw the exception.
Definition: exception.h:265
wibble::exception::Generic::Generic
Generic(const std::string &context)
Definition: exception.h:186
wibble::exception::BadCastExt::BadCastExt
BadCastExt(const std::string &error=std::string())
Definition: exception.h:289
wibble::exception::OutOfRange::m_var_desc
std::string m_var_desc
Definition: exception.h:312
wibble::list::end
ListIterator< List > end(List)
Definition: list.h:425
wibble::exception::InstallUnexpected::~InstallUnexpected
~InstallUnexpected()
Definition: exception.cpp:81
wibble::exception::BadCastExt
Definition: exception.h:287
wibble::exception::Context::addContext
void addContext(const std::string &c)
Definition: exception.h:153
wibble::exception::System
Base class for system exceptions.
Definition: exception.h:396
wibble::exception::WaitInterrupted::type
virtual const char * type() const
Get a string tag identifying the exception type.
Definition: exception.h:247