001:
002:
003: #ifndef _RZError_HH
004: #define _RZError_HH 1
005:
006: #include <stdexcept>
007: #include <sstream>
008: #include <cerrno>
009: #include <unistd.h>
010:
011: namespace RZ {
012: struct Finished {};
013:
014: struct Error: std::runtime_error {
015: Error(): std::runtime_error("RZ Error") {}
016: Error(std::string const &w): std::runtime_error(w) {}
017: };
018:
019:
020:
021: struct AssertionError: Error {
022: AssertionError(): Error() {}
023: static void assert_so(bool y) { if (!y) throw AssertionError(); }
024: static void assert_not(bool y) { if (y) throw AssertionError(); }
025: AssertionError(std::string const &w): Error(w) {}
026: static void what_so(std::string const &w, bool y) {
027: if (!y) throw AssertionError(w);
028: }
029: static void what_not(std::string const &w, bool y) {
030: if (y) throw AssertionError(w);
031: }
032: static void what_so(std::ostringstream &m, bool y) {
033: if (!y) throw AssertionError(m.str());
034: }
035: static void what_not(std::ostringstream &m, bool y) {
036: if (y) throw AssertionError(m.str());
037: }
038: };
039:
040: struct ContentError: Error {
041: ContentError(): Error() {}
042: ContentError(std::string const& w): Error(w) {}
043: static void what_if(std::string const &w, bool y) {
044: if (y) throw ContentError(w);
045: }
046: static void what_if(std::ostringstream &m, bool y) {
047: if (y) throw ContentError(m.str());
048: }
049: };
050:
051:
052:
053: struct IOError: Error {
054: IOError(std::string const &w): Error(w) {}
055: static std::string const what_errno(std::string const &w) {
056: std::ostringstream m;
057: m <<w <<" ( errno: " <<errno <<" ) " <<strerror(errno);
058: return m.str();
059: }
060: static std::string const what_errno(std::ostringstream &m) {
061: m <<" ( errno: " <<errno <<" ) " <<strerror(errno);
062: return m.str();
063: }
064: static void what_if(std::string const &w, bool y = false) {
065: if ( errno && errno != EINTR ) throw IOError(what_errno(w));
066: else if (y) throw IOError(w);
067: }
068: static void what_if(std::ostringstream &m, bool y = false) {
069: if (errno && errno != EINTR) throw IOError(what_errno(m));
070: else if (y) throw IOError(m.str());
071: }
072: };
073:
074: struct ScanError: IOError {
075: ScanError(std::string const &w): IOError(w) {}
076: static void what_if(std::string const & w, bool y = false) {
077: if (errno) throw ScanError(what_errno(w));
078: else if (y) throw ScanError(w);
079: }
080: };
081:
082: struct BadDescriptor: IOError {
083: BadDescriptor(std::string const &w): IOError(w) {}
084: static void what_if(std::string const & w, bool y = false) {
085: if (errno) throw BadDescriptor(what_errno(w));
086: else if (y) throw BadDescriptor(w);
087: }
088: };
089:
090: struct MMapError: IOError {
091: MMapError(std::string const &w): IOError(w) {}
092: static void what_if(std::string const & w, bool y = false) {
093: if (errno) throw MMapError(what_errno(w));
094: else if (y) throw MMapError(w);
095: }
096: };
097:
098: struct ReadError: IOError {
099: ReadError(std::string const &w): IOError(w) {}
100: };
101: }
102:
103:
104:
105: #endif