diff options
Diffstat (limited to 'src/core/common/Exceptions.hpp')
-rw-r--r-- | src/core/common/Exceptions.hpp | 77 |
1 files changed, 40 insertions, 37 deletions
diff --git a/src/core/common/Exceptions.hpp b/src/core/common/Exceptions.hpp index 00d6106..443c176 100644 --- a/src/core/common/Exceptions.hpp +++ b/src/core/common/Exceptions.hpp @@ -27,6 +27,8 @@ #ifndef _OUSIA_EXCEPTIONS_HPP_ #define _OUSIA_EXCEPTIONS_HPP_ +#include "TextCursor.hpp" + namespace ousia { /** @@ -81,80 +83,81 @@ private: * reported to the runtime environment. */ static std::string formatMessage(const std::string &msg, - const std::string &file, int line, - int column); + const TextCursor::Position &pos, + const TextCursor::Context &ctx); public: /** - * Message describing the error that occured. + * Reported error message. */ const std::string msg; /** - * Name of the file in which the error occured. May be empty. + * Position in the document at which the exception occurred. */ - const std::string file; + const TextCursor::Position pos; /** - * Line at which the exception occured. Negative values are ignored. + * Context in the document text in which the exception occurred. */ - const int line; - - /** - * Column at which the exception occured. Negative values are ignored. - */ - const int column; + const TextCursor::Context ctx; /** * Constructor of the LoggableException class. * * @param msg contains the error message. - * @param file provides the context the message refers to. May be empty. - * @param line is the line in the above file the message refers to. - * @param column is the column in the above file the message refers to. + * @param pos is the position at which the error occured. + * @param ctx describes the context in which the error occured. */ - LoggableException(std::string msg, std::string file, int line = -1, - int column = -1) - : OusiaException(formatMessage(msg, file, line, column)), + LoggableException(std::string msg, + TextCursor::Position pos = TextCursor::Position{}, + TextCursor::Context ctx = TextCursor::Context{}) + : OusiaException(formatMessage(msg, pos, ctx)), msg(std::move(msg)), - file(std::move(file)), - line(line), - column(column) + pos(std::move(pos)), + ctx(std::move(ctx)) { } /** - * Constructor of the LoggableException class with empty file. + * Constructor of the LoggableException class. * * @param msg contains the error message. * @param line is the line in the above file the message refers to. * @param column is the column in the above file the message refers to. + * @param offs is the byte offset. */ - LoggableException(std::string msg, int line = -1, int column = -1) - : OusiaException(formatMessage(msg, "", line, column)), - msg(std::move(msg)), - line(line), - column(column) + LoggableException(std::string msg, TextCursor::PosType line, + TextCursor::PosType column, size_t offs) + : LoggableException(msg, TextCursor::Position(line, column, offs)) { } /** - * Constructor of the LoggableException class with empty file and an - * position object. + * Constructor of LoggableException for arbitrary position objects. * * @param msg is the actual log message. - * @param pos is a const reference to a variable which provides position - * information. + * @param pos is a reference to a variable with position and context data. */ template <class PosType> - LoggableException(std::string msg, const PosType &pos) - : OusiaException( - formatMessage(msg, "", pos.getLine(), pos.getColumn())), - msg(std::move(msg)), - line(pos.getLine()), - column(pos.getColumn()) + LoggableException(std::string msg, PosType &pos) + : LoggableException(std::move(msg), pos.getPosition(), pos.getContext()) { } + + /** + * Returns the position at which the exception occured in the text. + * + * @return the position descriptor. + */ + TextCursor::Position getPosition() const { return pos; } + + /** + * Returns the context in which the exception occured in the text. + * + * @return the context descriptor. + */ + TextCursor::Context getContext() const { return ctx; } }; } |