Theoretica
Mathematical Library
Loading...
Searching...
No Matches
error.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_IO_ERROR_H
7#define THEORETICA_IO_ERROR_H
8
9#if defined(THEORETICA_THROW_EXCEPTIONS) || defined(THEORETICA_ONLY_EXCEPTIONS)
10#include <exception>
11#ifndef THEORETICA_NO_PRINT
12#include <sstream>
13#endif
14#endif
15
16#include <string>
17#include <cerrno>
18
19
20namespace theoretica {
21namespace io {
22
23
25 enum class IoError : int {
26
28 None = 0x00,
29
31 FileNotFound = 0x01,
32
34 PermissionDenied = 0x02,
35
37 ReadError = 0x04,
38
40 WriteError = 0x08,
41
43 FormatError = 0x10,
44
46 EndOfFile = 0x20
47 };
48
49
51 inline int to_errno(IoError err) {
52 switch(err) {
53 case IoError::None: return 0;
54 case IoError::FileNotFound: return ENOENT;
56 case IoError::ReadError: return EIO;
57 case IoError::WriteError: return EIO;
58 case IoError::FormatError: return EINVAL;
59 case IoError::EndOfFile: return ENODATA;
60 default: return 0;
61 }
62 }
63
64
66 inline const char* to_cstring(IoError err) {
67 switch (err) {
68 case IoError::None: return "No error";
69 case IoError::FileNotFound: return "File not found";
70 case IoError::PermissionDenied: return "Permission denied";
71 case IoError::ReadError: return "Failed to read data from IO stream";
72 case IoError::WriteError: return "Failed to write data to IO stream";
73 case IoError::FormatError: return "Invalid or corrupted file format";
74 case IoError::EndOfFile: return "Unexpected end of file";
75 default: return "Unknown IO error";
76 }
77 }
78
79 inline std::string to_string(IoError err) {
80 return to_cstring(err);
81 }
82
83
84#if defined(THEORETICA_THROW_EXCEPTIONS) || defined(THEORETICA_ONLY_EXCEPTIONS)
85
86 class io_exception : public std::exception {
87 private:
88
89 IoError err;
90 std::string func_name;
91 std::string code_file_name;
92 unsigned int code_line;
93 std::string resource;
94
95 public:
96
97 io_exception(IoError a_err, const std::string& a_func_name,
98 const std::string& a_code_file_name, unsigned int a_code_line, const std::string& a_target_file)
99 : err(a_err), func_name(a_func_name), code_file_name(a_code_file_name),
100 code_line(a_code_line), resource(a_target_file) {}
101
102 ~io_exception() = default;
103
104 inline const char* what() const noexcept override {
105 return to_cstring(err);
106 }
107
108
110 inline IoError err_code() const {
111 return err;
112 }
113
114
116 inline std::string get_function_name() const {
117 return func_name;
118 }
119
120
122 inline std::string get_file_name() const {
123 return code_file_name;
124 }
125
126
128 inline unsigned int get_line_number() const {
129 return code_line;
130 }
131
132
134 inline std::string get_resource_id() const {
135 return resource;
136 }
137
138
139#ifndef THEORETICA_NO_PRINT
140
141 inline std::string to_string() const {
142 std::stringstream err_str;
143 err_str << code_file_name << "(" << code_line << "):";
144 err_str << func_name << "(\"" << resource << "\"): " << to_cstring(err);
145 return err_str.str();
146 }
147
148 inline operator std::string() const {
149 return to_string();
150 }
151
152 inline friend std::ostream& operator<<(std::ostream& out, const io_exception& obj) {
153 return out << obj.to_string();
154 }
155#endif
156
157 };
158
159#endif
160}}
161
162
163// Only throw exceptions, without modifying errno
164#ifdef THEORETICA_ONLY_EXCEPTIONS
165
166#define TH_IO_ERROR(F_NAME, RESOURCE_ID, EXCEPTION) \
167 { throw io::io_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, RESOURCE_ID); }
168
169// Throw exceptions and modify errno
170#elif defined(THEORETICA_THROW_EXCEPTIONS)
171
172#define TH_IO_ERROR(F_NAME, RESOURCE_ID, EXCEPTION) \
173 { errno = io::to_errno(EXCEPTION); \
174 throw io::io_exception(EXCEPTION, F_NAME, __FILE__, __LINE__, RESOURCE_ID); }
175
176// Modify errno only by default
177#else
178
179#define TH_IO_ERROR(F_NAME, RESOURCE_ID, EXCEPTION) \
180 { errno = io::to_errno(EXCEPTION); }
181
182#endif
183
184
185#endif
std::ostream & operator<<(std::ostream &os, const hdf5_node &node)
Prints the HDF5 file tree to a stream.
Definition hdf5.h:460
int to_errno(IoError err)
Convert an IoError class enum to conventional errno codes.
Definition error.h:51
IoError
IO error enumeration.
Definition error.h:25
@ FileNotFound
File or directory not found.
@ WriteError
Error occurred while writing to the file or stream.
@ PermissionDenied
Permission denied when accessing the file or directory.
@ FormatError
The file format is invalid or the data is corrupted.
@ EndOfFile
The end of the file was reached unexpectedly during a read operation.
@ ReadError
Error occurred while reading from the file or stream.
const char * to_cstring(IoError err)
Convert an IoError class enum to a string description.
Definition error.h:66
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
TH_CONSTEXPR Type make_error()
Create a number representing an error state, constructed from a NaN value.
Definition real_analysis.h:1322
std::string to_string(MathError err)
Convert a MathError class enum to a string description.
Definition error.h:72
const char * to_cstring(MathError err)
Convert a MathError class enum to a string description.
Definition error.h:56