00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00052 #ifndef TOPPERS_DIAGNOSTICS_HPP_
00053 #define TOPPERS_DIAGNOSTICS_HPP_
00054
00055 #include <toppers/kernel.hpp>
00056 #include <toppers/assert.hpp>
00057 #include <cstdlib>
00058
00059 #ifndef TOPPERS_DEFAULT_DIAGNOSTICS
00060 #define TOPPERS_DEFAULT_DIAGNOSTICS null_diagnostics
00061 #endif
00062
00063 namespace toppers
00064 {
00065
00077 class null_diagnostics
00078 {
00079 public:
00080 enum
00081 {
00083 use_exception = false
00084 };
00085
00087 static ER_UINT handle_error(ER_UINT ercd)
00088 {
00089 return ercd;
00090 }
00091
00093 static ER_UINT static_handle_error(ER_UINT ercd)
00094 {
00095 return ercd;
00096 }
00097
00099 static void panic()
00100 {
00101 std::abort();
00102 }
00103 };
00104
00105 typedef ER_UINT (*error_handler_type)(ER_UINT);
00106
00125 template <class T = void>
00126 class error_handler_diagnostics
00127 {
00128 public:
00129 typedef error_handler_type handler_type;
00130
00131 enum
00132 {
00134 use_exception = true
00135 };
00136
00138 static ER_UINT handle_error(ER_UINT ercd)
00139 {
00140 return ercd < 0 ? (*handler_)(ercd) : ercd;
00141 }
00142
00144 static ER_UINT static_handle_error(ER_UINT ercd)
00145 {
00146 return handle_error(ercd);
00147 }
00148
00150 static void panic()
00151 {
00152 std::abort();
00153 }
00154
00156 static handler_type set_error_handler(handler_type handler)
00157 {
00158 handler_type prev = handler_;
00159 handler_ = handler;
00160 return prev;
00161 }
00162
00163 private:
00164 static error_handler_type handler_;
00165 };
00166
00167 template <class T>
00168 error_handler_type error_handler_diagnostics<T>::handler_;
00169
00182 template <class T = void>
00183 class polymorphic_diagnostics : protected error_handler_diagnostics<T>
00184 {
00185 typedef error_handler_diagnostics<T> super;
00186
00187 public:
00188 enum
00189 {
00191 use_exception = true
00192 };
00193
00195 virtual ~polymorphic_diagnostics()
00196 {
00197 }
00198
00200 virtual ER_UINT handle_error(ER_UINT ercd) const
00201 {
00202 return super::handle_error(ercd);
00203 }
00204
00206 virtual void panic() const
00207 {
00208 return super::panic();
00209 }
00210 };
00211
00212 namespace detail
00213 {
00214
00215 extern "C" const char* itron_strerror(ER) throw();
00216
00217 }
00218
00220 inline const char* strerror(ER ercd)
00221 {
00222 return detail::itron_strerror(ercd);
00223 }
00224
00225 }
00226
00227 #endif // ! TOPPERS_DIAGNOSTICS_HPP_