13
13
14
14
#pragma once
15
15
16
+ #include " log.h"
16
17
#include < stdint.h>
17
18
18
19
namespace tokenizers {
@@ -59,11 +60,14 @@ enum class Error : error_code_t {
59
60
* TODO: Add logging support
60
61
* @param[in] cond__ The condition to be checked, asserted as true.
61
62
* @param[in] error__ Error enum value to return without the `Error::` prefix,
62
- * like `InvalidArgument`.
63
+ * like `Base64DecodeFailure`.
64
+ * @param[in] message__ Format string for the log error message.
65
+ * @param[in] ... Optional additional arguments for the format string.
63
66
*/
64
- #define TK_CHECK_OR_RETURN_ERROR (cond__, error__ ) \
67
+ #define TK_CHECK_OR_RETURN_ERROR (cond__, error__, message__, ...) \
65
68
{ \
66
69
if (!(cond__)) { \
70
+ TK_LOG (Error, message__, ##__VA_ARGS__); \
67
71
return ::tokenizers::Error::error__; \
68
72
} \
69
73
}
@@ -72,11 +76,80 @@ enum class Error : error_code_t {
72
76
* If error__ is not Error::Ok, return the specified Error
73
77
* TODO: Add logging support
74
78
* @param[in] error__ Error enum value to return without the `Error::` prefix,
75
- * like `InvalidArgument`.
79
+ * like `Base64DecodeFailure`.
80
+ * @param[in] ... Optional format string for the log error message and its
81
+ * arguments.
76
82
*/
77
- #define TK_CHECK_OK_OR_RETURN_ERROR (error__ ) \
78
- { \
79
- if (error__ != ::tokenizers::Error::Ok) { \
80
- return error__; \
83
+ #define TK_CHECK_OK_OR_RETURN_ERROR (error__, ...) \
84
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR (error__, ##__VA_ARGS__)
85
+
86
+ // Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
87
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR (...) \
88
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT (__VA_ARGS__, 10 , 9 , 8 , 7 , 6 , 5 , \
89
+ 4 , 3 , 2 , 1 ) \
90
+ (__VA_ARGS__)
91
+
92
+ /* *
93
+ * Internal only: Use TK_CHECK_OK_OR_RETURN_ERROR() instead.
94
+ * This macro selects the correct version of
95
+ * TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR based on the number of arguments passed.
96
+ * It uses a trick with the preprocessor to count the number of arguments and
97
+ * then selects the appropriate macro.
98
+ *
99
+ * The macro expansion uses __VA_ARGS__ to accept any number of arguments and
100
+ * then appends them to TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_, followed by the
101
+ * count of arguments. The count is determined by the macro
102
+ * TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT which takes the arguments and
103
+ * passes them along with a sequence of numbers (2, 1). The preprocessor then
104
+ * matches this sequence to the correct number of arguments provided.
105
+ *
106
+ * If two arguments are passed, TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 is
107
+ * selected, suitable for cases where an error code and a custom message are
108
+ * provided. If only one argument is passed,
109
+ * TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1 is selected, which is used for cases
110
+ * with just an error code.
111
+ *
112
+ * Usage:
113
+ * TK_CHECK_OK_OR_RETURN_ERROR(error_code); // Calls v1
114
+ * TK_CHECK_OK_OR_RETURN_ERROR(error_code, "Error message", ...); // Calls v2
115
+ */
116
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_SELECT (_1, _2, _3, _4, _5, _6, \
117
+ _7, _8, _9, _10, N, ...) \
118
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_##N
119
+
120
+ // Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
121
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_1 (error__ ) \
122
+ do { \
123
+ const auto et_error__ = (error__); \
124
+ if (et_error__ != ::tokenizers::Error::Ok) { \
125
+ return et_error__; \
81
126
} \
82
- }
127
+ } while (0 )
128
+
129
+ // Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
130
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2 (error__, message__, ...) \
131
+ do { \
132
+ const auto et_error__ = (error__); \
133
+ if (et_error__ != ::tokenizers::Error::Ok) { \
134
+ TK_LOG (Error, message__, ##__VA_ARGS__); \
135
+ return et_error__; \
136
+ } \
137
+ } while (0 )
138
+
139
+ // Internal only: Use ET_CHECK_OK_OR_RETURN_ERROR() instead.
140
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_3 \
141
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
142
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_4 \
143
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
144
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_5 \
145
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
146
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_6 \
147
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
148
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_7 \
149
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
150
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_8 \
151
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
152
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_9 \
153
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
154
+ #define TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_10 \
155
+ TK_INTERNAL_CHECK_OK_OR_RETURN_ERROR_2
0 commit comments