dune-common  2.11
exceptions.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 
6 #ifndef DUNE_EXCEPTIONS_HH
7 #define DUNE_EXCEPTIONS_HH
8 
9 #include <exception>
10 #include <string>
11 #include <sstream>
12 #include <type_traits>
13 #include <utility>
14 
15 namespace Dune {
16 
75  /* forward declarations */
76  class Exception;
77  struct ExceptionHook;
78 
96  class Exception
97  : public std::exception
98  {
99  public:
100  Exception ();
101  void message(const std::string &msg);
102  const char* what() const noexcept override;
103  static void registerHook (ExceptionHook * hook);
104  static void clearHook ();
105  private:
106  std::string _message;
107  static ExceptionHook * _hook;
108  };
109 
176  {
177  virtual ~ExceptionHook() {}
178  virtual void operator () () = 0;
179  };
180 
181  inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
182  {
183  return stream << e.what();
184  }
185 
186 
198  template<class E>
200  : public E
201  {
202  public:
203  using E::E;
204 
205  ExceptionStream(const E& other) :
206  E(other)
207  {}
208 
209  ExceptionStream(E&& other) :
210  E(std::move(other))
211  {}
212 
214  template<class T>
215  requires
216  (requires(std::ostringstream& oss, T t) { oss << t; }
217  and not std::is_integral_v<T>)
218  friend ExceptionStream& operator<<(ExceptionStream& es, const T& t)
219  {
220  es.sstream_ << t;
221  es.message(es.sstream_.str());
222  return es;
223  }
224 
226  template<class T>
227  requires
228  (requires(std::ostringstream& oss, T t) { oss << t; }
229  and not std::is_integral_v<T>)
230  friend ExceptionStream operator<<(ExceptionStream&& es, const T& t)
231  {
232  es << t;
233  return std::move(es);
234  }
235 
243  template<class T>
244  requires std::is_integral_v<T>
246  {
247  es.sstream_ << t;
248  es.message(es.sstream_.str());
249  return es;
250  }
251 
253  template<class T>
254  requires std::is_integral_v<T>
256  {
257  es << t;
258  return std::move(es);
259  }
260 
262  friend ExceptionStream& operator<<(ExceptionStream& es, std::ostream& (*t)(std::ostream&))
263  {
264  es.sstream_ << t;
265  es.message(es.sstream_.str());
266  return es;
267  }
268 
270  friend ExceptionStream operator<<(ExceptionStream&& es, std::ostream& (*t)(std::ostream&))
271  {
272  es << t;
273  return std::move(es);
274  }
275 
276  private:
277  std::ostringstream sstream_;
278  };
279 
280 
281 
282 #ifndef DOXYGEN
283  // the "format" the exception-type gets printed. __FILE__ and
284  // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
285  // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
286 #define THROWSPEC(E) # E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
287 #endif // DOXYGEN
288 
314 #define DUNE_THROW(E, ...) throw Dune::ExceptionStream(E()) << THROWSPEC(E) __VA_OPT__(<<) __VA_ARGS__
315 
325  class IOError : public Exception {};
326 
335  class MathError : public Exception {};
336 
348  class RangeError : public Exception {};
349 
357  class NotImplemented : public Exception {};
358 
365  class SystemError : public Exception {};
366 
370  class OutOfMemoryError : public SystemError {};
371 
375  class InvalidStateException : public Exception {};
376 
381  class ParallelError : public Exception {};
382 
383 } // end namespace
384 
385 #endif
std::ostream & operator<<(std::ostream &s, const bigunsignedint< k > &x)
Definition: bigunsignedint.hh:301
friend ExceptionStream & operator<<(ExceptionStream &es, std::ostream &(*t)(std::ostream &))
Stream operator for l-value ExceptionStream and io manipulator.
Definition: exceptions.hh:262
Default exception if an error in the parallel communication of the program occurred.
Definition: exceptions.hh:381
friend ExceptionStream operator<<(ExceptionStream &&es, std::ostream &(*t)(std::ostream &))
Stream operator for r-value ExceptionStream and io manipulator.
Definition: exceptions.hh:270
ExceptionStream(const E &other)
Definition: exceptions.hh:205
requires(requires(std::ostringstream &oss, T t) { oss<< t;} and not std::is_integral_v< T >) friend ExceptionStream &operator<<(ExceptionStream &es
Stream operator for l-value ExceptionStream and lvalue-reference.
static void clearHook()
remove all hooks
Definition: exceptions.cc:27
const char * what() const noexcept override
output internal message buffer
Definition: exceptions.cc:37
requires std::is_integral_v< T > friend ExceptionStream operator<<(ExceptionStream &&es, T t)
Stream operator for r-value ExceptionStream and integral values.
Definition: exceptions.hh:255
virtual ~ExceptionHook()
Definition: exceptions.hh:177
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
return es
Definition: exceptions.hh:222
Default exception class for range errors.
Definition: exceptions.hh:348
Class for extending a Dune::Exception with a stream interface.
Definition: exceptions.hh:199
Dune namespace
Definition: alignedallocator.hh:12
virtual void operator()()=0
void message(const std::string &msg)
store string in internal message buffer
Definition: exceptions.cc:32
Default exception class for OS errors.
Definition: exceptions.hh:365
Default exception class for mathematical errors.
Definition: exceptions.hh:335
Exception()
Definition: exceptions.cc:16
Base class to add a hook to the Dune::Exception.
Definition: exceptions.hh:175
return std::move(es)
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:375
STL namespace.
Default exception for dummy implementations.
Definition: exceptions.hh:357
requires std::is_integral_v< T > friend ExceptionStream & operator<<(ExceptionStream &es, T t)
Stream operator for l-value ExceptionStream and integral values.
Definition: exceptions.hh:245
Default exception class for I/O errors.
Definition: exceptions.hh:325
const T & t
Definition: exceptions.hh:219
ExceptionStream(E &&other)
Definition: exceptions.hh:209
Default exception if memory allocation fails.
Definition: exceptions.hh:370
static void registerHook(ExceptionHook *hook)
add a functor which is called before a Dune::Exception is emitted (see Dune::ExceptionHook) ...
Definition: exceptions.cc:22