Loading...
Searching...
No Matches
exceptions.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_EXCEPTIONS_HH
#define DUNE_EXCEPTIONS_HH
#include <exception>
#include <string>
#include <sstream>
#include <type_traits>
#include <utility>
namespace Dune {
/* forward declarations */
class Exception;
struct ExceptionHook;
class Exception
: public std::exception
{
public:
Exception ();
void message(const std::string &msg);
const char* what() const noexcept override;
static void registerHook (ExceptionHook * hook);
static void clearHook ();
private:
std::string _message;
static ExceptionHook * _hook;
};
struct ExceptionHook
{
virtual ~ExceptionHook() {}
virtual void operator () () = 0;
};
inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
{
return stream << e.what();
}
template<class E>
class ExceptionStream
: public E
{
public:
using E::E;
ExceptionStream(const E& other) :
E(other)
{}
ExceptionStream(E&& other) :
E(std::move(other))
{}
template<class T>
requires
(requires(std::ostringstream& oss, T t) { oss << t; }
and not std::is_integral_v<T>)
friend ExceptionStream& operator<<(ExceptionStream& es, const T& t)
{
es.sstream_ << t;
es.message(es.sstream_.str());
return es;
}
template<class T>
requires
(requires(std::ostringstream& oss, T t) { oss << t; }
and not std::is_integral_v<T>)
friend ExceptionStream operator<<(ExceptionStream&& es, const T& t)
{
es << t;
return std::move(es);
}
template<class T>
requires std::is_integral_v<T>
friend ExceptionStream& operator<<(ExceptionStream& es, T t)
{
es.sstream_ << t;
es.message(es.sstream_.str());
return es;
}
template<class T>
requires std::is_integral_v<T>
friend ExceptionStream operator<<(ExceptionStream&& es, T t)
{
es << t;
return std::move(es);
}
friend ExceptionStream& operator<<(ExceptionStream& es, std::ostream& (*t)(std::ostream&))
{
es.sstream_ << t;
es.message(es.sstream_.str());
return es;
}
friend ExceptionStream operator<<(ExceptionStream&& es, std::ostream& (*t)(std::ostream&))
{
es << t;
return std::move(es);
}
private:
std::ostringstream sstream_;
};
#ifndef DOXYGEN
// the "format" the exception-type gets printed. __FILE__ and
// __LINE__ are standard C-defines, the GNU cpp-infofile claims that
// C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
#define THROWSPEC(E) # E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
#endif // DOXYGEN
#define DUNE_THROW(E, ...) throw Dune::ExceptionStream(E()) << THROWSPEC(E) __VA_OPT__(<<) __VA_ARGS__
class IOError : public Exception {};
class MathError : public Exception {};
class RangeError : public Exception {};
class NotImplemented : public Exception {};
class SystemError : public Exception {};
class OutOfMemoryError : public SystemError {};
class InvalidStateException : public Exception {};
class ParallelError : public Exception {};
} // end namespace
#endif
Dune::operator<<
std::ostream & operator<<(std::ostream &s, const bigunsignedint< k > &x)
Definition bigunsignedint.hh:301
Dune::Exception::Exception
Exception()
Definition exceptions.cc:16
Dune::ExceptionStream::operator<<
friend ExceptionStream operator<<(ExceptionStream &&es, std::ostream &(*t)(std::ostream &))
Stream operator for r-value ExceptionStream and io manipulator.
Definition exceptions.hh:270
Dune::ExceptionStream::ExceptionStream
ExceptionStream(const E &other)
Definition exceptions.hh:205
Dune::Exception::registerHook
static void registerHook(ExceptionHook *hook)
add a functor which is called before a Dune::Exception is emitted (see Dune::ExceptionHook)
Definition exceptions.cc:22
Dune::ExceptionStream::operator<<
friend ExceptionStream operator<<(ExceptionStream &&es, T t)
Stream operator for r-value ExceptionStream and integral values.
Definition exceptions.hh:255
Dune::ExceptionStream::operator<<
friend ExceptionStream & operator<<(ExceptionStream &es, T t)
Stream operator for l-value ExceptionStream and integral values.
Definition exceptions.hh:245
Dune::Exception::clearHook
static void clearHook()
remove all hooks
Definition exceptions.cc:27
Dune::ExceptionStream::operator<<
friend ExceptionStream & operator<<(ExceptionStream &es, const T &t)
Stream operator for l-value ExceptionStream and lvalue-reference.
Definition exceptions.hh:218
Dune::Exception::message
void message(const std::string &msg)
store string in internal message buffer
Definition exceptions.cc:32
Dune::ExceptionStream::operator<<
friend ExceptionStream & operator<<(ExceptionStream &es, std::ostream &(*t)(std::ostream &))
Stream operator for l-value ExceptionStream and io manipulator.
Definition exceptions.hh:262
Dune::ExceptionHook::operator()
virtual void operator()()=0
Dune::ExceptionHook::~ExceptionHook
virtual ~ExceptionHook()
Definition exceptions.hh:177
Dune::Exception::what
const char * what() const noexcept override
output internal message buffer
Definition exceptions.cc:37
Dune::ExceptionStream::ExceptionStream
ExceptionStream(E &&other)
Definition exceptions.hh:209
std
STL namespace.
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::Exception
Base class for Dune-Exceptions.
Definition exceptions.hh:98
Dune::ExceptionHook
Base class to add a hook to the Dune::Exception.
Definition exceptions.hh:176
Dune::IOError
Default exception class for I/O errors.
Definition exceptions.hh:325
Dune::MathError
Default exception class for mathematical errors.
Definition exceptions.hh:335
Dune::RangeError
Default exception class for range errors.
Definition exceptions.hh:348
Dune::NotImplemented
Default exception for dummy implementations.
Definition exceptions.hh:357
Dune::SystemError
Default exception class for OS errors.
Definition exceptions.hh:365
Dune::OutOfMemoryError
Default exception if memory allocation fails.
Definition exceptions.hh:370
Dune::InvalidStateException
Default exception if a function was called while the object is not in a valid state for that function...
Definition exceptions.hh:375
Dune::ParallelError
Default exception if an error in the parallel communication of the program occurred.
Definition exceptions.hh:381