35
35
#include < limits>
36
36
#include < type_traits>
37
37
38
+ #if __cplusplus >= 202002L
39
+ #include < source_location>
40
+ #endif // __cplusplus >= 202002L
41
+
38
42
namespace chip {
39
43
40
44
/* *
@@ -112,9 +116,13 @@ class ChipError
112
116
113
117
// Helper for declaring constructors without too much repetition.
114
118
#if CHIP_CONFIG_ERROR_SOURCE
115
- #define CHIP_INITIALIZE_ERROR_SOURCE (f, l ) , mFile ((f)), mLine ((l))
116
- #else // CHIP_CONFIG_ERROR_SOURCE
117
- #define CHIP_INITIALIZE_ERROR_SOURCE (f, l )
119
+ #if __cplusplus >= 202002L
120
+ #define CHIP_INITIALIZE_ERROR_SOURCE (f, l, loc ) , mFile ((f)), mLine ((l)), mSourceLocation ((loc))
121
+ #else
122
+ #define CHIP_INITIALIZE_ERROR_SOURCE (f, l, loc ) , mFile ((f)), mLine ((l))
123
+ #endif // __cplusplus >= 202002L
124
+ #else // CHIP_CONFIG_ERROR_SOURCE
125
+ #define CHIP_INITIALIZE_ERROR_SOURCE (f, l, loc )
118
126
#endif // CHIP_CONFIG_ERROR_SOURCE
119
127
120
128
/* *
@@ -123,23 +131,35 @@ class ChipError
123
131
* @note
124
132
* The result is valid only if CanEncapsulate() is true.
125
133
*/
126
- constexpr ChipError (Range range, ValueType value) :
127
- mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 )
134
+ constexpr ChipError (Range range, ValueType value) : ChipError(range, value, /* file=*/ nullptr , /* line=*/ 0 ) {}
135
+ #if __cplusplus >= 202002L
136
+ constexpr ChipError (Range range, ValueType value, const char * file, unsigned int line,
137
+ std::source_location location = std::source_location::current()) :
138
+ mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location)
128
139
{}
140
+ #else
129
141
constexpr ChipError (Range range, ValueType value, const char * file, unsigned int line) :
130
- mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
142
+ mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /* loc= */ nullptr )
131
143
{}
144
+ #endif // __cplusplus >= 202002L
132
145
133
146
/* *
134
147
* Construct a CHIP_ERROR for SdkPart @a part with @a code.
135
148
*
136
149
* @note
137
150
* The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed.
138
151
*/
139
- constexpr ChipError (SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 ) {}
152
+ constexpr ChipError (SdkPart part, uint8_t code) : ChipError(part, code, /* file=*/ nullptr , /* line=*/ 0 ) {}
153
+ #if __cplusplus >= 202002L
154
+ constexpr ChipError (SdkPart part, uint8_t code, const char * file, unsigned int line,
155
+ std::source_location location = std::source_location::current()) :
156
+ mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location)
157
+ {}
158
+ #else
140
159
constexpr ChipError (SdkPart part, uint8_t code, const char * file, unsigned int line) :
141
- mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
160
+ mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /* loc= */ nullptr )
142
161
{}
162
+ #endif // __cplusplus >= 202002L
143
163
144
164
/* *
145
165
* Construct a CHIP_ERROR constant for SdkPart @a part with @a code at the current source line.
@@ -159,10 +179,17 @@ class ChipError
159
179
* @note
160
180
* This is intended to be used only in foreign function interfaces.
161
181
*/
162
- explicit constexpr ChipError (StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 ) {}
182
+ explicit constexpr ChipError (StorageType error) : ChipError(error, /* file=*/ nullptr , /* line=*/ 0 ) {}
183
+ #if __cplusplus >= 202002L
184
+ explicit constexpr ChipError (StorageType error, const char * file, unsigned int line,
185
+ std::source_location location = std::source_location::current()) :
186
+ mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location)
187
+ {}
188
+ #else
163
189
explicit constexpr ChipError (StorageType error, const char * file, unsigned int line) :
164
- mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
190
+ mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /* loc= */ nullptr )
165
191
{}
192
+ #endif // __cplusplus >= 202002L
166
193
167
194
#undef CHIP_INITIALIZE_ERROR_SOURCE
168
195
@@ -299,6 +326,12 @@ class ChipError
299
326
*/
300
327
unsigned int GetLine () const { return mLine ; }
301
328
329
+ #if __cplusplus >= 202002L
330
+ /* *
331
+ * Get the source_location of the point where the error occurred.
332
+ */
333
+ const std::source_location & GetSourceLocation () { return mSourceLocation ; }
334
+ #endif // __cplusplus >= 202002L
302
335
#endif // CHIP_CONFIG_ERROR_SOURCE
303
336
304
337
private:
@@ -365,6 +398,9 @@ class ChipError
365
398
#if CHIP_CONFIG_ERROR_SOURCE
366
399
const char * mFile ;
367
400
unsigned int mLine ;
401
+ #if __cplusplus >= 202002L
402
+ std::source_location mSourceLocation ;
403
+ #endif // __cplusplus >= 202002L
368
404
#endif // CHIP_CONFIG_ERROR_SOURCE
369
405
370
406
public:
0 commit comments