From ed78b585cfc4ecb44972e346857e887b183fd7a7 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 2 Sep 2016 14:41:49 +0300 Subject: [PATCH] Move all exception classes to a common header --- source/binaryparser.cpp | 22 +--------- source/dataerror.h | 30 -------------- source/{dataerror.cpp => except.cpp} | 22 +++++++++- source/except.h | 62 ++++++++++++++++++++++++++++ source/loader.cpp | 2 +- source/parser.cpp | 2 +- source/textparser.cpp | 23 +---------- 7 files changed, 87 insertions(+), 76 deletions(-) delete mode 100644 source/dataerror.h rename source/{dataerror.cpp => except.cpp} (62%) create mode 100644 source/except.h 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.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/dataerror.cpp b/source/except.cpp similarity index 62% rename from source/dataerror.cpp rename to source/except.cpp index b21b84f..eea692e 100644 --- a/source/dataerror.cpp +++ b/source/except.cpp @@ -1,7 +1,7 @@ #include #include #include -#include "dataerror.h" +#include "except.h" using namespace std; @@ -28,5 +28,25 @@ string data_error::make_what(const string &s, unsigned l, const string &w) 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) { } -- 2.43.0