Error.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2004 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/Error.h
10
11#ifndef __Error_h
12#define __Error_h
13
14#include <iostream>
15#include <vector>
16#include <string>
17
19enum ErrorCode {
21 Undefined,
23 BadAllocation,
25 InvalidPointer,
27 InvalidParam,
29 InvalidState,
31 InvalidRange,
33 FileNotFound,
35 EndOfFile,
37 FailedCall,
39 FailedSys,
41 HelpMessage
42};
43
45/*
46 It is useful to throw an exception with both an error code and a
47 text message. As well, on catching an exception, it is useful if a
48 function or method can add its name to an "exception stack",
49 enabling the error to be traced back to its origin. The static
50 method, Error::handle_signals(), may also be invoked so that signal
51 interrupts, such as SIGFPE and SIGSEGV, will be intercepted and an
52 Error exception will be thrown.
53*/
54class Error {
55
56 public:
57
59 static const char* err2str (ErrorCode code);
60
62 static bool verbose;
63
65 static bool complete_abort;
66
68 Error (ErrorCode c, std::string func, const char* msg=0, ...);
69
71 Error (ErrorCode c, std::string func, std::string msg);
72
74 virtual ~Error ();
75
77 const Error& operator += (const char* func);
78
80 const Error& operator += (const std::string& func);
81
83 virtual void report (std::ostream& ostr) const;
84
86 virtual std::string warning () const;
87
89 const std::string get_message() const;
90
92 ErrorCode get_code () const { return code; }
93
95 template<class T>
96 friend Error& operator<< (Error& error, const T& t);
97
98protected:
99
101 ErrorCode code;
103 std::string message;
105 std::vector<std::string> functions;
106
108 Error ();
109
111 void errno_check ();
112
114 void construct (ErrorCode c, const std::string& func, const char* msg);
115};
116
118std::ostream& operator<< (std::ostream& ostr, const Error& error);
119
120#include "tostring.h"
121
122template<class T>
123Error& operator<< (Error& error, const T& t)
124{
125 error.message += tostring(t);
126 return error;
127}
128
129#endif
A convenient exception handling class.
Definition Error.h:54
virtual std::string warning() const
Write function name and error message to screen.
Definition Error.C:122
void construct(ErrorCode c, const std::string &func, const char *msg)
Contruct the basic deal.
Definition Error.C:49
virtual ~Error()
Virtual destructor enables inheritance.
Definition Error.C:76
void errno_check()
If errno is set, adds a string to message.
Definition Error.C:68
ErrorCode code
the error code
Definition Error.h:101
static bool complete_abort
After construction raise a sigkill.
Definition Error.h:65
Error(ErrorCode c, std::string func, const char *msg=0,...)
Error with optional printf-style message.
Definition Error.C:25
ErrorCode get_code() const
Get the error code.
Definition Error.h:92
const std::string get_message() const
Get the error message.
Definition Error.C:133
std::vector< std::string > functions
function name stack
Definition Error.h:105
static const char * err2str(ErrorCode code)
convert an error code to a character string
Definition Error.C:144
const Error & operator+=(const char *func)
Add function name to the list.
Definition Error.C:81
friend Error & operator<<(Error &error, const T &t)
Add to the Error message.
Definition Error.h:123
std::string message
auxilliary message
Definition Error.h:103
static bool verbose
During constructor, print messages.
Definition Error.h:62
Error()
Null constructor is protected.
Definition Error.C:20
virtual void report(std::ostream &ostr) const
Write error and auxilliary info to ostream.
Definition Error.C:98

Generated using doxygen 1.14.0