|
- #pragma once
-
- #include <string>
- #include <cstdint>
- #include <iomanip>
- #include <string.h>
- #include <execinfo.h>
-
- #include "flags.h"
-
- namespace cppcore
- {
-
- /**
- * @brief Basic exception with stack trace and some more small features.
- */
- struct exception
- : public std::exception
- {
- public:
- enum class print_flag
- {
- print_trace,
- resolve_address,
- };
- using print_flags = shifted_flags<print_flag>;
-
- public:
- static constexpr size_t max_stack_size = 15;
-
- /**
- * @brief Get the default print flags.
- */
- static inline const print_flags& default_print_flag();
-
- /**
- * @brief Print the exception to the passed stream;
- *
- * @param[in] os Stream to print exception to.
- * @param[in] ex Exception to print to stream.
- *
- * @return The stream received as parameter (for further processing).
- */
- inline friend std::ostream& operator <<(std::ostream& os, const exception& ex);
-
- private:
- mutable bool _msg_cache_empty; //!< Indicates if the message cache was created or not.
- mutable std::string _msg_cache; //!< Caches the message of the exception.
-
- public:
- std::string message; //!< message of the exception
- void* stack[max_stack_size]; //!< stack trace
- int stack_size; //!< number of entries stored in stack
-
- public:
- /**
- * @brief Default constructor.
- */
- inline exception();
-
- /**
- * @brief Constructor to create an exception.
- */
- inline exception(std::string msg);
-
- /**
- * @brief Destructor.
- */
- virtual inline ~exception() = default;
-
- /**
- * @brief Print the whole exception to the passed stream.
- *
- * @param[in] os Stream to print exception to.
- * @param[in] flags Flags to use for printing.
- */
- inline void print(
- std::ostream& os,
- const print_flags& flags = default_print_flag()) const;
-
- /**
- * @brief Convert the exception to string.
- *
- * @param[in] flags Flags to use for printing.
- *
- * @return String representation of the exception.
- */
- inline std::string print(
- const print_flags& flags = default_print_flag()) const;
-
- /**
- * @brief Convert the exception to a string.
- */
- inline const std::string& to_string() const;
-
- /**
- * @brief Get the message of the exception as c-string.
- */
- inline const char* what() const throw() override;
-
- protected:
- /**
- * @brief Print the message of the exception to the passed stream.
- *
- * @param[in] os Stream to print message of the exception to.
- */
- virtual inline void print_message(std::ostream& os) const;
- };
-
-
-
- /**
- * @brief Error exception. Wrapps the errors from the operating system.
- */
- struct error_exception
- : public exception
- {
- public:
- int error; //!< Error received from the operating system
-
- public:
- /**
- * @brief Constructor to create a error exception.
- *
- * @param[in] e Error code received from the operating system.
- */
- inline error_exception(int e);
-
- /**
- * @brief Constructor to create a error exception.
- *
- * @param[in] msg Message of the error exception.
- * @param[in] e Error code received from the operating system.
- */
- inline error_exception(const std::string& msg, int e);
- };
-
-
-
- /**
- * @brief Range exception.
- */
- struct range_exception
- : public exception
- {
- public:
- size_t min; //!< minimum valid value of the range
- size_t max; //!< maximum valid value of the range
- size_t index; //!< actual index that was used
-
- public:
- /**
- * @brief Constructor to create a range exception.
- */
- inline range_exception(size_t mi, size_t ma, size_t idx, std::string msg = "");
-
- protected:
- /**
- * @brief Print the message of the exception to the passed stream.
- *
- * @param[in] os Stream to print message of the exception to.
- */
- inline void print_message(std::ostream& os) const override;
- };
-
-
-
- /**
- * @brief Argument exception.
- */
- struct argument_exception
- : public exception
- {
- public:
- std::string argument; //!< argument that caused the exception
-
- public:
- /**
- * @brief Constructor to create the argument exception.
- *
- * @param[in] arg Argument that caused the exception.
- * @param[in] msg Message of the exception.
- */
- inline argument_exception(std::string arg, std::string msg = "");
-
- protected:
- /**
- * @brief Print the message of the exception to the passed stream.
- *
- * @param[in] os Stream to print message of the exception to.
- */
- inline void print_message(std::ostream& os) const override;
- };
-
-
-
- /**
- * @brief Invalid operation exception.
- */
- struct invalid_operation_exception
- : public exception
- {
- using exception::exception;
- };
-
-
-
- /**
- * @brief Convert exception.
- */
- struct convert_exception
- : public exception
- {
- using exception::exception;
- };
-
- }
-
- #include "exception.inl"
|