From: Mikko Rasa Date: Fri, 2 Sep 2016 11:41:49 +0000 (+0300) Subject: Move all exception classes to a common header X-Git-Url: http://git.tdb.fi/?p=libs%2Fdatafile.git;a=commitdiff_plain;h=ed78b585cfc4ecb44972e346857e887b183fd7a7 Move all exception classes to a common header --- diff --git a/source/binaryparser.cpp b/source/binaryparser.cpp index e2ced82..4ed2877 100644 --- a/source/binaryparser.cpp +++ b/source/binaryparser.cpp @@ -5,6 +5,7 @@ #include "argumentstore.h" #include "binaryparser.h" #include "binfloat.h" +#include "except.h" #include "input.h" #include "loaderaction.h" @@ -13,27 +14,6 @@ using namespace std; namespace Msp { namespace DataFile { -class bad_definition: public runtime_error -{ -public: - bad_definition(const std::string &w): - runtime_error(w) - { } - - virtual ~bad_definition() throw() { } -}; - -class nesting_error: public logic_error -{ -public: - nesting_error(const std::string &w): - logic_error(w) - { } - - virtual ~nesting_error() throw() { } -}; - - BinaryParser::BinaryParser(Input &i, const string &s): ParserMode(i, s), float_precision(32), diff --git a/source/dataerror.cpp b/source/dataerror.cpp deleted file mode 100644 index b21b84f..0000000 --- a/source/dataerror.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include -#include -#include -#include "dataerror.h" - -using namespace std; - -namespace Msp { -namespace DataFile { - -data_error::data_error(const string &s, unsigned l, const string &w): - runtime_error(make_what(s, l, w)), - source(s), - line(l) -{ } - -data_error::data_error(const string &s, unsigned l, const exception &e): - runtime_error(make_what(s, l, format("%s (%s)", Debug::demangle(typeid(e).name()), e.what()))), - source(s), - line(l) -{ } - -string data_error::make_what(const string &s, unsigned l, const string &w) -{ - if(l) - return format("%s:%d: %s", s, l, w); - else - return format("%s: %s", s, w); -} - -} // namespace DataFile -} // namespace Msp diff --git a/source/dataerror.h b/source/dataerror.h deleted file mode 100644 index 5fca0c3..0000000 --- a/source/dataerror.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef MSP_DATAFILE_DATAERROR_H_ -#define MSP_DATAFILE_DATAERROR_H_ - -#include - -namespace Msp { -namespace DataFile { - -class data_error: public std::runtime_error -{ -private: - std::string source; - unsigned line; - -public: - data_error(const std::string &, unsigned, const std::string &); - data_error(const std::string &, unsigned, const std::exception &); - virtual ~data_error() throw() { } - - const std::string &get_source() const { return source; } - unsigned get_line() const { return line; } - -private: - std::string make_what(const std::string &, unsigned, const std::string &); -}; - -} // namespace DataFile -} // namespace Msp - -#endif diff --git a/source/except.cpp b/source/except.cpp new file mode 100644 index 0000000..eea692e --- /dev/null +++ b/source/except.cpp @@ -0,0 +1,52 @@ +#include +#include +#include +#include "except.h" + +using namespace std; + +namespace Msp { +namespace DataFile { + +data_error::data_error(const string &s, unsigned l, const string &w): + runtime_error(make_what(s, l, w)), + source(s), + line(l) +{ } + +data_error::data_error(const string &s, unsigned l, const exception &e): + runtime_error(make_what(s, l, format("%s (%s)", Debug::demangle(typeid(e).name()), e.what()))), + source(s), + line(l) +{ } + +string data_error::make_what(const string &s, unsigned l, const string &w) +{ + if(l) + return format("%s:%d: %s", s, l, w); + else + return format("%s: %s", s, w); +} + + +parse_error::parse_error(const string &t): + runtime_error(t.empty() ? "at end of input" : format("after '%s'", t)) +{ } + + +syntax_error::syntax_error(const string &t): + runtime_error(t.empty() ? "at end of input" : format("at '%s'", t)) +{ } + + +bad_definition::bad_definition(const string &w): + runtime_error(w) +{ } + + +nesting_error::nesting_error(const string &w): + logic_error(w) +{ } + +} // namespace DataFile +} // namespace Msp diff --git a/source/except.h b/source/except.h new file mode 100644 index 0000000..e68ec48 --- /dev/null +++ b/source/except.h @@ -0,0 +1,62 @@ +#ifndef MSP_DATAFILE_EXCEPT_H_ +#define MSP_DATAFILE_EXCEPT_H_ + +#include + +namespace Msp { +namespace DataFile { + +class data_error: public std::runtime_error +{ +private: + std::string source; + unsigned line; + +public: + data_error(const std::string &, unsigned, const std::string &); + data_error(const std::string &, unsigned, const std::exception &); + virtual ~data_error() throw() { } + + const std::string &get_source() const { return source; } + unsigned get_line() const { return line; } + +private: + std::string make_what(const std::string &, unsigned, const std::string &); +}; + + +class parse_error: public std::runtime_error +{ +public: + parse_error(const std::string &); + virtual ~parse_error() throw() { } +}; + + +class syntax_error: public std::runtime_error +{ +public: + syntax_error(const std::string &t); + virtual ~syntax_error() throw() { } +}; + + +class bad_definition: public std::runtime_error +{ +public: + bad_definition(const std::string &w); + virtual ~bad_definition() throw() { } +}; + + +class nesting_error: public std::logic_error +{ +public: + nesting_error(const std::string &); + virtual ~nesting_error() throw() { } +}; + +} // namespace DataFile +} // namespace Msp + +#endif diff --git a/source/loader.cpp b/source/loader.cpp index 8137068..4bc715a 100644 --- a/source/loader.cpp +++ b/source/loader.cpp @@ -1,6 +1,6 @@ #include #include -#include "dataerror.h" +#include "except.h" #include "loader.h" #include "type.h" diff --git a/source/parser.cpp b/source/parser.cpp index a9bbbf2..5972e2d 100644 --- a/source/parser.cpp +++ b/source/parser.cpp @@ -1,6 +1,6 @@ #include #include "binaryparser.h" -#include "dataerror.h" +#include "except.h" #include "parser.h" #include "statement.h" #include "textparser.h" diff --git a/source/textparser.cpp b/source/textparser.cpp index 05521a9..c87a32d 100644 --- a/source/textparser.cpp +++ b/source/textparser.cpp @@ -1,5 +1,6 @@ #include #include +#include "except.h" #include "input.h" #include "textparser.h" #include "token.h" @@ -9,28 +10,6 @@ using namespace std; namespace Msp { namespace DataFile { -class parse_error: public runtime_error -{ -public: - parse_error(const std::string &t): - runtime_error(t.empty() ? "at end of input" : format("after '%s'", t)) - { } - - virtual ~parse_error() throw() { } -}; - - -class syntax_error: public runtime_error -{ -public: - syntax_error(const std::string &t): - runtime_error(t.empty() ? "at end of input" : format("at '%s'", t)) - { } - - virtual ~syntax_error() throw() { } -}; - - TextParser::TextParser(Input &i, const string &s): ParserMode(i, s) { }