Not Enough Math
Lightweight zero-dependency C++ math library
Loading...
Searching...
No Matches
err.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <stdexcept>
4#include <limits>
5
6#include "config.hpp"
7#include "nan.hpp"
8
9#define NEM_RUNTIME_ASSERT(condition, message) assert((condition) && (message))
10
11namespace nem
12{
13 namespace error
14 {
15 enum class Kind : unsigned char
16 {
17 RuntimeError = 0,
22 };
23
24 namespace internal
25 {
26 template <nem::scalar_type T>
28 {
29#ifdef NEM_ERR_USE_NAN
30 return std::numeric_limits<T>::quiet_NaN();
31#else
32 return (T)0.0;
33#endif
34 }
35
39 template <typename T>
41 {
42#ifdef NEM_ERR_USE_NAN
43 return T::NaN();
44#else
45 return T{ };
46#endif
47 }
48
49#if defined(NEM_ERR_LOG)
51 {
52 printf("%s\n", GET_C_STRING(msg));
53 }
54
56 {
57 printf("%s %s\n", GET_C_STRING(prefix), GET_C_STRING(msg));
58 }
59
61 {
62 switch (type)
63 {
64 case Kind::InvalidArgument: push_log_prefix("Not Enough Math [Invalid Argument] Error: ", msg); break;
65 case Kind::DivisionByZero: push_log_prefix("Not Enough Math [Division By Zero] Error: ", msg); break;
66 case Kind::OutOfRange: push_log_prefix("Not Enough Math [Out Of Range] Error: ", msg); break;
67 default: push_log_prefix("Not Enough Math Error: ", msg); break;
68 }
69 }
70#endif
71
72#if defined(_STDEXCEPT_) && defined(NEM_ERR_THROW)
73 NEM_INLINE void force_throw(nem_string msg, nem::error::Kind type = Kind::RuntimeError)
74 {
75 switch (type)
76 {
78 {
79 throw std::out_of_range(msg);
80 } break;
82 {
83 throw std::logic_error(msg);
84 } break;
86 {
87 throw std::invalid_argument(msg);
88 } break;
90 default:
91 {
92 throw std::runtime_error(msg);
93 }
94 }
95 }
96#endif
97 }
98
100 {
101#ifdef NEM_ERR_THROW
102 internal::force_throw(msg, type);
103#endif
104#ifdef NEM_ERR_LOG
105 internal::force_log(msg, type);
106#endif
107 }
108
109 NEM_INLINE void report_invalid(nem::error::Kind kind = Kind::RuntimeError, const char* const msg = nullptr)
110 {
111#ifdef NEM_ERR_THROW
112 internal::force_throw(msg ? msg : "Invalid result", kind);
113#endif
114#ifdef NEM_ERR_LOG
115 internal::force_log(msg ? msg : "Invalid result", kind);
116#endif
117 }
118
119 template <typename T>
120 NEM_INLINE T invalid_result(nem::error::Kind kind = Kind::RuntimeError, const char* const msg = nullptr)
121 {
122 report_invalid(kind, msg);
124 }
125 }
126}
#define NEM_INLINE
Definition config.hpp:12
void force_log(nem_string msg, nem::error::Kind type=Kind::RuntimeError)
Definition err.hpp:60
void push_log_prefix(nem_string prefix, nem_string msg)
Definition err.hpp:55
T invalid_result_base()
For non-scalar objects.
Definition err.hpp:27
void push_log(nem_string msg)
Definition err.hpp:50
void report(nem_string msg, nem::error::Kind type=Kind::RuntimeError)
Definition err.hpp:99
T invalid_result(nem::error::Kind kind=Kind::RuntimeError, const char *const msg=nullptr)
Definition err.hpp:120
void report_invalid(nem::error::Kind kind=Kind::RuntimeError, const char *const msg=nullptr)
Definition err.hpp:109
Definition config.hpp:16
const char * GET_C_STRING(nem_string str)
Definition config.hpp:49
const char * nem_string
Definition config.hpp:47