2021-04-15 02:42:06 +02:00
|
|
|
/** @file error.hh
|
|
|
|
*
|
|
|
|
* @brief Error handling API.
|
|
|
|
*
|
|
|
|
* Defines structures and methods used for error handling in the library.
|
|
|
|
*
|
|
|
|
* @copyright See COPYING.md in the project tree for further information.
|
|
|
|
*/
|
|
|
|
|
2021-04-05 04:40:52 +02:00
|
|
|
#ifndef LIBCUBESCRIPT_CUBESCRIPT_ERROR_HH
|
|
|
|
#define LIBCUBESCRIPT_CUBESCRIPT_ERROR_HH
|
|
|
|
|
|
|
|
#include <string_view>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
namespace cubescript {
|
|
|
|
|
|
|
|
struct state;
|
|
|
|
|
2021-04-20 02:52:27 +02:00
|
|
|
/** @brief Represents a Cubescript error.
|
|
|
|
*
|
|
|
|
* This is a standard error that can be thrown by either the Cubescript APIs
|
|
|
|
* or from the language itself (either by the user, or by incorrect use of
|
|
|
|
* the API).
|
|
|
|
*
|
|
|
|
* It has a message attached, as well as the current state of the call stack,
|
|
|
|
* represented as cubescript::stack_state.
|
|
|
|
*
|
|
|
|
* Each Cubescript thread internally stores a buffer for the error message,
|
|
|
|
* which is reused for each error raised from that thread.
|
|
|
|
*/
|
2021-04-05 04:40:52 +02:00
|
|
|
struct LIBCUBESCRIPT_EXPORT error {
|
2021-05-09 20:21:35 +02:00
|
|
|
/** @brief A node in the call stack.
|
|
|
|
*
|
|
|
|
* The nodes are indexed. The bottommost node has index 1, the topmost
|
|
|
|
* node has index N (where N is the number of levels the call stack has).
|
|
|
|
*
|
|
|
|
* There can be a gap in the stack (i.e. the bottommost node will have
|
|
|
|
* index 1 and the one above it greater than 2). The gap is controlled
|
|
|
|
* by the value of the `dbgalias` cubescript variable at the time of
|
|
|
|
* creation of the error (the stack list will contain at most N nodes).
|
2021-05-15 23:27:34 +02:00
|
|
|
*
|
|
|
|
* When getting the stack state, it will be represented as a span with
|
|
|
|
* the first element being the topmost node and the last element being
|
|
|
|
* the bottommost (index 1).
|
2021-05-09 20:21:35 +02:00
|
|
|
*/
|
|
|
|
struct stack_node {
|
2021-05-15 04:23:47 +02:00
|
|
|
struct ident const &id; /**< @brief The ident of this level. */
|
2021-05-09 20:21:35 +02:00
|
|
|
std::size_t index; /**< @brief The level index. */
|
|
|
|
};
|
2021-04-05 04:40:52 +02:00
|
|
|
|
|
|
|
error() = delete;
|
|
|
|
error(error const &) = delete;
|
2021-04-20 02:52:27 +02:00
|
|
|
|
|
|
|
/** @brief Errors are move constructible. */
|
2021-05-15 23:27:34 +02:00
|
|
|
error(error &&v);
|
|
|
|
|
|
|
|
error &operator=(error const &) = delete;
|
|
|
|
|
|
|
|
/** @brief Errors are move assignable. */
|
|
|
|
error &operator=(error &&v);
|
2021-04-05 04:40:52 +02:00
|
|
|
|
2021-05-08 17:20:56 +02:00
|
|
|
/** @brief Construct an error using a string. */
|
|
|
|
error(state &cs, std::string_view msg);
|
|
|
|
|
2021-05-09 20:21:35 +02:00
|
|
|
/** @brief Destroy the error. */
|
|
|
|
~error();
|
|
|
|
|
2021-04-20 02:52:27 +02:00
|
|
|
/** @brief Get a view of the error message. */
|
2021-05-15 23:27:34 +02:00
|
|
|
std::string_view what() const;
|
|
|
|
|
|
|
|
/** @brief Get the call stack state at the point of error. */
|
|
|
|
span_type<stack_node const> stack() const;
|
|
|
|
|
2021-04-05 04:40:52 +02:00
|
|
|
private:
|
2021-05-08 17:20:56 +02:00
|
|
|
friend struct error_p;
|
|
|
|
|
|
|
|
error(state &cs, char const *errbeg, char const *errend);
|
2021-04-05 04:40:52 +02:00
|
|
|
|
|
|
|
char const *p_errbeg, *p_errend;
|
2021-05-15 23:27:34 +02:00
|
|
|
stack_node *p_sbeg, *p_send;
|
2021-05-09 20:21:35 +02:00
|
|
|
state *p_state;
|
2021-04-05 04:40:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace cubescript */
|
|
|
|
|
|
|
|
#endif /* LIBCUBESCRIPT_CUBESCRIPT_ERROR_HH */
|