Discontinuous Galerkin Library
#include "dg/algorithm.h"
exceptions.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <exception>
9#include <iostream>
10#include <sstream>
11
12#define _ping_ __FILE__, __LINE__
13
14
15
19namespace dg
20{
21
29{
30 private:
31 std::stringstream sstream_;
32 Message( const Message&); //we can't copy ostreams in C++
33 Message& operator=(const Message&);
34 public:
43 Message(const char* file, const int line){
44 sstream_ << "\n Message from file **"<<file<<"** in line **" <<line<<"**:\n ";
45 }
50 Message( std::string m){ sstream_<<m;}
54 template<class T>
55 Message & operator << (const T& value)
56 {
57 sstream_ << value;
58 return *this;
59 }
61 std::string str() const {return sstream_.str();}
64 friend std::ostream& operator<<(std::ostream& os, const Message& m)
65 {
66 os<<m.str();
67 return os;
68 }
69};
70
71
82class Error : public std::exception
83{
84 private:
85 std::string m;//with a string the Error is copyable
86 public:
87
92 Error(const Message& message){
93 m = message.str();
94 }
95
96 std::string get_message( ) const{return m;}
97
98
101 void append( const Message& message)
102 {
103 m+= message.str();
104 }
105
108 void append_line( const Message& message)
109 {
110 m+= "\n"+message.str();
111 }
113 virtual const char* what() const throw()
114 {
115 return m.c_str();
116 }
117 virtual ~Error() throw(){}
118};
119
120
125struct Fail : public Error
126{
127
133 Fail( double eps): Fail(eps, Message("")){}
134
141 Fail( double eps, const Message& m): Error(
142 Message("\n FAILED to converge to ")<< eps << "! "<<m),
143 eps( eps) {}
149 double epsilon() const { return eps;}
150 virtual ~Fail() throw(){}
151 private:
152 double eps;
153};
154
167static inline void abort_program(int code = -1){
168#ifdef MPI_VERSION
169 MPI_Abort(MPI_COMM_WORLD, code);
170#endif //WITH_MPI
171 exit( code);
172}
173
174
175}//namespace dg
class intended for the use in throw statements
Definition: exceptions.h:83
Error(const Message &message)
Constructor.
Definition: exceptions.h:92
std::string get_message() const
Definition: exceptions.h:96
virtual ~Error()
Definition: exceptions.h:117
void append_line(const Message &message)
Appends a newline and a message verbatim to the what string.
Definition: exceptions.h:108
void append(const Message &message)
Appends a message verbatim to the what string.
Definition: exceptions.h:101
virtual const char * what() const
Definition: exceptions.h:113
small class holding a stringstream
Definition: exceptions.h:29
friend std::ostream & operator<<(std::ostream &os, const Message &m)
Definition: exceptions.h:64
std::string str() const
return the message contained in the stream as a string
Definition: exceptions.h:61
Message(const char *file, const int line)
Initiate message with the file and line it comes from.
Definition: exceptions.h:43
Message()
construct an empty message
Definition: exceptions.h:36
Message & operator<<(const T &value)
add values to the message stream
Definition: exceptions.h:55
Message(std::string m)
Construct message with string.
Definition: exceptions.h:50
~Message()
Definition: exceptions.h:51
static void abort_program(int code=-1)
Abort program (both MPI and non-MPI)
Definition: exceptions.h:167
This is the namespace for all functions and classes defined and used by the discontinuous Galerkin li...
Indicate failure to converge.
Definition: exceptions.h:126
Fail(double eps, const Message &m)
Construct from error limit.
Definition: exceptions.h:141
double epsilon() const
Return error limit.
Definition: exceptions.h:149
virtual ~Fail()
Definition: exceptions.h:150
Fail(double eps)
Construct from error limit.
Definition: exceptions.h:133