Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

220 linhas
5.6 KiB

  1. #pragma once
  2. #include <string>
  3. #include <cstdint>
  4. #include <iomanip>
  5. #include <string.h>
  6. #include <execinfo.h>
  7. #include "flags.h"
  8. namespace cppcore
  9. {
  10. /**
  11. * @brief Basic exception with stack trace and some more small features.
  12. */
  13. struct exception
  14. : public std::exception
  15. {
  16. public:
  17. enum class print_flag
  18. {
  19. print_trace,
  20. resolve_address,
  21. };
  22. using print_flags = shifted_flags<print_flag>;
  23. public:
  24. static constexpr size_t max_stack_size = 15;
  25. /**
  26. * @brief Get the default print flags.
  27. */
  28. static inline const print_flags& default_print_flag();
  29. /**
  30. * @brief Print the exception to the passed stream;
  31. *
  32. * @param[in] os Stream to print exception to.
  33. * @param[in] ex Exception to print to stream.
  34. *
  35. * @return The stream received as parameter (for further processing).
  36. */
  37. inline friend std::ostream& operator <<(std::ostream& os, const exception& ex);
  38. private:
  39. mutable bool _msg_cache_empty; //!< Indicates if the message cache was created or not.
  40. mutable std::string _msg_cache; //!< Caches the message of the exception.
  41. public:
  42. std::string message; //!< message of the exception
  43. void* stack[max_stack_size]; //!< stack trace
  44. int stack_size; //!< number of entries stored in stack
  45. public:
  46. /**
  47. * @brief Default constructor.
  48. */
  49. inline exception();
  50. /**
  51. * @brief Constructor to create an exception.
  52. */
  53. inline exception(std::string msg);
  54. /**
  55. * @brief Destructor.
  56. */
  57. virtual inline ~exception() = default;
  58. /**
  59. * @brief Print the whole exception to the passed stream.
  60. *
  61. * @param[in] os Stream to print exception to.
  62. * @param[in] flags Flags to use for printing.
  63. */
  64. inline void print(
  65. std::ostream& os,
  66. const print_flags& flags = default_print_flag()) const;
  67. /**
  68. * @brief Convert the exception to string.
  69. *
  70. * @param[in] flags Flags to use for printing.
  71. *
  72. * @return String representation of the exception.
  73. */
  74. inline std::string print(
  75. const print_flags& flags = default_print_flag()) const;
  76. /**
  77. * @brief Convert the exception to a string.
  78. */
  79. inline const std::string& to_string() const;
  80. /**
  81. * @brief Get the message of the exception as c-string.
  82. */
  83. inline const char* what() const throw() override;
  84. protected:
  85. /**
  86. * @brief Print the message of the exception to the passed stream.
  87. *
  88. * @param[in] os Stream to print message of the exception to.
  89. */
  90. virtual inline void print_message(std::ostream& os) const;
  91. };
  92. /**
  93. * @brief Error exception. Wrapps the errors from the operating system.
  94. */
  95. struct error_exception
  96. : public exception
  97. {
  98. public:
  99. int error; //!< Error received from the operating system
  100. public:
  101. /**
  102. * @brief Constructor to create a error exception.
  103. *
  104. * @param[in] e Error code received from the operating system.
  105. */
  106. inline error_exception(int e);
  107. /**
  108. * @brief Constructor to create a error exception.
  109. *
  110. * @param[in] msg Message of the error exception.
  111. * @param[in] e Error code received from the operating system.
  112. */
  113. inline error_exception(const std::string& msg, int e);
  114. };
  115. /**
  116. * @brief Range exception.
  117. */
  118. struct range_exception
  119. : public exception
  120. {
  121. public:
  122. size_t min; //!< minimum valid value of the range
  123. size_t max; //!< maximum valid value of the range
  124. size_t index; //!< actual index that was used
  125. public:
  126. /**
  127. * @brief Constructor to create a range exception.
  128. */
  129. inline range_exception(size_t mi, size_t ma, size_t idx, std::string msg = "");
  130. protected:
  131. /**
  132. * @brief Print the message of the exception to the passed stream.
  133. *
  134. * @param[in] os Stream to print message of the exception to.
  135. */
  136. inline void print_message(std::ostream& os) const override;
  137. };
  138. /**
  139. * @brief Argument exception.
  140. */
  141. struct argument_exception
  142. : public exception
  143. {
  144. public:
  145. std::string argument; //!< argument that caused the exception
  146. public:
  147. /**
  148. * @brief Constructor to create the argument exception.
  149. *
  150. * @param[in] arg Argument that caused the exception.
  151. * @param[in] msg Message of the exception.
  152. */
  153. inline argument_exception(std::string arg, std::string msg = "");
  154. protected:
  155. /**
  156. * @brief Print the message of the exception to the passed stream.
  157. *
  158. * @param[in] os Stream to print message of the exception to.
  159. */
  160. inline void print_message(std::ostream& os) const override;
  161. };
  162. /**
  163. * @brief Invalid operation exception.
  164. */
  165. struct invalid_operation_exception
  166. : public exception
  167. {
  168. using exception::exception;
  169. };
  170. /**
  171. * @brief Convert exception.
  172. */
  173. struct convert_exception
  174. : public exception
  175. {
  176. using exception::exception;
  177. };
  178. }
  179. #include "exception.inl"