Skip to content

Commit 4dfb83a

Browse files
committed
Add more error macros
1 parent a58d43d commit 4dfb83a

File tree

1 file changed

+168
-8
lines changed

1 file changed

+168
-8
lines changed

src/openvic-simulation/utility/ErrorMacros.hpp

Lines changed: 168 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,50 @@
33
#include "openvic-simulation/utility/Logger.hpp" // IWYU pragma: keep for macros
44
#include "openvic-simulation/utility/Utility.hpp"
55

6+
/**
7+
* Try using `ERR_FAIL_COND_MSG`.
8+
* Only use this macro if more complex error detection or recovery is required.
9+
*
10+
* Prints `m_msg`, and the current function returns.
11+
*/
12+
#define OV_ERR_FAIL_MSG(m_msg) \
13+
if (true) { \
14+
::OpenVic::Logger::error("Method/function failed. ", m_msg); \
15+
return; \
16+
} else \
17+
((void)0)
18+
19+
/**
20+
* Try using `ERR_FAIL_COND_V_MSG` or `ERR_FAIL_V_MSG`.
21+
* Only use this macro if more complex error detection or recovery is required, and
22+
* there is no sensible error message.
23+
*
24+
* The current function returns `m_retval`.
25+
*/
26+
#define OV_ERR_FAIL_V(m_retval) \
27+
if (true) { \
28+
::OpenVic::Logger::error("Method/function failed. Returning: " _OV_STR(m_retval)); \
29+
return m_retval; \
30+
} else \
31+
((void)0)
32+
33+
/**
34+
* Try using `ERR_FAIL_COND_V_MSG`.
35+
* Only use this macro if more complex error detection or recovery is required.
36+
*
37+
* Prints `m_msg`, and the current function returns `m_retval`.
38+
*/
39+
#define OV_ERR_FAIL_V_MSG(m_retval, m_msg) \
40+
if (true) { \
41+
::OpenVic::Logger::error("Method/function failed. Returning: " _OV_STR(m_retval), " ", m_msg); \
42+
return m_retval; \
43+
} else \
44+
((void)0)
45+
646
/**
747
* Try using `ERR_FAIL_COND_MSG`.
848
* Only use this macro if there is no sensible error message.
9-
* If checking for null use ERR_FAIL_NULL_MSG instead.
49+
* If checking for null use OV_ERR_FAIL_NULL_MSG instead.
1050
* If checking index bounds use ERR_FAIL_INDEX_MSG instead.
1151
*
1252
* Ensures `m_cond` is false.
@@ -23,7 +63,7 @@
2363
* Ensures `m_cond` is false.
2464
* If `m_cond` is true, prints `m_msg` and the current function returns.
2565
*
26-
* If checking for null use ERR_FAIL_NULL_MSG instead.
66+
* If checking for null use OV_ERR_FAIL_NULL_MSG instead.
2767
* If checking index bounds use ERR_FAIL_INDEX_MSG instead.
2868
*/
2969
#define OV_ERR_FAIL_COND_MSG(m_cond, m_msg) \
@@ -36,17 +76,15 @@
3676
/**
3777
* Try using `ERR_FAIL_COND_V_MSG`.
3878
* Only use this macro if there is no sensible error message.
39-
* If checking for null use ERR_FAIL_NULL_V_MSG instead.
79+
* If checking for null use OV_ERR_FAIL_NULL_V_MSG instead.
4080
* If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
4181
*
4282
* Ensures `m_cond` is false.
4383
* If `m_cond` is true, the current function returns `m_retval`.
4484
*/
4585
#define OV_ERR_FAIL_COND_V(m_cond, m_retval) \
4686
if (OV_unlikely(m_cond)) { \
47-
::OpenVic::Logger::error( \
48-
"Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) \
49-
); \
87+
::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval)); \
5088
return m_retval; \
5189
} else \
5290
((void)0)
@@ -55,14 +93,136 @@
5593
* Ensures `m_cond` is false.
5694
* If `m_cond` is true, prints `m_msg` and the current function returns `m_retval`.
5795
*
58-
* If checking for null use ERR_FAIL_NULL_V_MSG instead.
96+
* If checking for null use OV_ERR_FAIL_NULL_V_MSG instead.
5997
* If checking index bounds use ERR_FAIL_INDEX_V_MSG instead.
6098
*/
6199
#define OV_ERR_FAIL_COND_V_MSG(m_cond, m_retval, m_msg) \
62100
if (OV_unlikely(m_cond)) { \
101+
::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) " ", m_msg); \
102+
return m_retval; \
103+
} else \
104+
((void)0)
105+
106+
/**
107+
* Try using `ERR_FAIL_INDEX_MSG`.
108+
* Only use this macro if there is no sensible error message.
109+
*
110+
* Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
111+
* If not, the current function returns.
112+
*/
113+
#define OV_ERR_FAIL_INDEX(m_index, m_size) \
114+
if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
115+
::OpenVic::Logger::error( \
116+
"Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, ")." \
117+
); \
118+
return; \
119+
} else \
120+
((void)0)
121+
122+
/**
123+
* Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
124+
* If not, prints `m_msg` and the current function returns.
125+
*/
126+
#define OV_ERR_FAIL_INDEX_MSG(m_index, m_size, m_msg) \
127+
if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
63128
::OpenVic::Logger::error( \
64-
"Condition \"" _OV_STR(m_cond) "\" is true. Returning: " _OV_STR(m_retval) " ", m_msg \
129+
"Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, "). ", m_msg \
65130
); \
131+
return; \
132+
} else \
133+
((void)0)
134+
135+
/**
136+
* Try using `ERR_FAIL_INDEX_V_MSG`.
137+
* Only use this macro if there is no sensible error message.
138+
*
139+
* Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
140+
* If not, the current function returns `m_retval`.
141+
*/
142+
#define OV_ERR_FAIL_INDEX_V(m_index, m_size, m_retval) \
143+
if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
144+
::OpenVic::Logger::error( \
145+
"Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, \
146+
"). Returning: " _OV_STR(m_retval) \
147+
); \
148+
return m_retval; \
149+
} else \
150+
((void)0)
151+
152+
/**
153+
* Ensures an integer index `m_index` is less than `m_size` and greater than or equal to 0.
154+
* If not, prints `m_msg` and the current function returns `m_retval`.
155+
*/
156+
#define OV_ERR_FAIL_INDEX_V_MSG(m_index, m_size, m_retval, m_msg) \
157+
if (OV_unlikely((m_index) < 0 || (m_index) >= (m_size))) { \
158+
::OpenVic::Logger::error( \
159+
"Index " _OV_STR(m_index) " = ", m_index, " is out of bounds (" _OV_STR(m_size) " = ", m_size, \
160+
"). Returning: " _OV_STR(m_retval) " ", m_msg \
161+
); \
162+
return m_retval; \
163+
} else \
164+
((void)0)
165+
166+
#define OV_ERR_CONTINUE(m_cond) \
167+
if (OV_unlikely(m_cond)) { \
168+
::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Continuing."); \
169+
continue; \
170+
} else \
171+
((void)0)
172+
173+
#define OV_ERR_BREAK(m_cond) \
174+
if (OV_unlikely(m_cond)) { \
175+
::OpenVic::Logger::error("Condition \"" _OV_STR(m_cond) "\" is true. Breaking."); \
176+
break; \
177+
} else \
178+
((void)0)
179+
180+
/**
181+
* Try using `ERR_FAIL_NULL_MSG`.
182+
* Only use this macro if there is no sensible error message.
183+
*
184+
* Ensures a pointer `m_param` is not null.
185+
* If it is null, the current function returns.
186+
*/
187+
#define OV_ERR_FAIL_NULL(m_param) \
188+
if (OV_unlikely(m_param == nullptr)) { \
189+
::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null."); \
190+
return; \
191+
} else \
192+
((void)0)
193+
194+
/**
195+
* Ensures a pointer `m_param` is not null.
196+
* If it is null, prints `m_msg` and the current function returns.
197+
*/
198+
#define OV_ERR_FAIL_NULL_MSG(m_param, m_msg) \
199+
if (OV_unlikely(m_param == nullptr)) { \
200+
::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null.", m_msg); \
201+
return; \
202+
} else \
203+
((void)0)
204+
205+
/**
206+
* Try using `ERR_FAIL_NULL_V_MSG`.
207+
* Only use this macro if there is no sensible error message.
208+
*
209+
* Ensures a pointer `m_param` is not null.
210+
* If it is null, the current function returns `m_retval`.
211+
*/
212+
#define OV_ERR_FAIL_NULL_V(m_param, m_retval) \
213+
if (OV_unlikely(m_param == nullptr)) { \
214+
::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null."); \
215+
return m_retval; \
216+
} else \
217+
((void)0)
218+
219+
/**
220+
* Ensures a pointer `m_param` is not null.
221+
* If it is null, prints `m_msg` and the current function returns `m_retval`.
222+
*/
223+
#define OV_ERR_FAIL_NULL_V_MSG(m_param, m_retval, m_msg) \
224+
if (OV_unlikely(m_param == nullptr)) { \
225+
::OpenVic::Logger::error("Parameter \"" _OV_STR(m_param) "\" is null.", m_msg); \
66226
return m_retval; \
67227
} else \
68228
((void)0)

0 commit comments

Comments
 (0)