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,7 +116,11 @@ class ChipError
112
116
113
117
// Helper for declaring constructors without too much repetition.
114
118
#if CHIP_CONFIG_ERROR_SOURCE
119
+ #if __cplusplus >= 202002L
120
+ #define CHIP_INITIALIZE_ERROR_SOURCE (f, l, loc ) , mFile ((f)), mLine ((l)), mSourceLocation ((loc))
121
+ #else
115
122
#define CHIP_INITIALIZE_ERROR_SOURCE (f, l ) , mFile ((f)), mLine ((l))
123
+ #endif // __cplusplus >= 202002L
116
124
#else // CHIP_CONFIG_ERROR_SOURCE
117
125
#define CHIP_INITIALIZE_ERROR_SOURCE (f, l )
118
126
#endif // CHIP_CONFIG_ERROR_SOURCE
@@ -123,23 +131,39 @@ class ChipError
123
131
* @note
124
132
* The result is valid only if CanEncapsulate() is true.
125
133
*/
134
+ #if __cplusplus >= 202002L
135
+ constexpr ChipError (Range range, ValueType value) :
136
+ mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 , std::source_location())
137
+ {}
138
+ constexpr ChipError (Range range, ValueType value, const char * file, unsigned int line, std::source_location location = std::source_location::current()) :
139
+ mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location)
140
+ {}
141
+ #else
126
142
constexpr ChipError (Range range, ValueType value) :
127
143
mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 )
128
144
{}
129
145
constexpr ChipError (Range range, ValueType value, const char * file, unsigned int line) :
130
146
mError(MakeInteger(range, (value & MakeMask (0 , kValueLength )))) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
131
147
{}
148
+ #endif // __cplusplus >= 202002L
132
149
133
150
/* *
134
151
* Construct a CHIP_ERROR for SdkPart @a part with @a code.
135
152
*
136
153
* @note
137
154
* The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed.
138
155
*/
156
+ #if __cplusplus >= 202002L
157
+ constexpr ChipError (SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 , std::source_location()) {}
158
+ constexpr ChipError (SdkPart part, uint8_t code, const char * file, unsigned int line, std::source_location location = std::source_location::current()) :
159
+ mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location)
160
+ {}
161
+ #else
139
162
constexpr ChipError (SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 ) {}
140
163
constexpr ChipError (SdkPart part, uint8_t code, const char * file, unsigned int line) :
141
164
mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
142
165
{}
166
+ #endif // __cplusplus >= 202002L
143
167
144
168
/* *
145
169
* Construct a CHIP_ERROR constant for SdkPart @a part with @a code at the current source line.
@@ -159,10 +183,17 @@ class ChipError
159
183
* @note
160
184
* This is intended to be used only in foreign function interfaces.
161
185
*/
186
+ #if __cplusplus >= 202002L
187
+ explicit constexpr ChipError (StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 , std::source_location()) {}
188
+ explicit constexpr ChipError (StorageType error, const char * file, unsigned int line, std::source_location location = std::source_location::current()) :
189
+ mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location)
190
+ {}
191
+ #else
162
192
explicit constexpr ChipError (StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr , 0 ) {}
163
193
explicit constexpr ChipError (StorageType error, const char * file, unsigned int line) :
164
194
mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
165
195
{}
196
+ #endif // __cplusplus >= 202002L
166
197
167
198
#undef CHIP_INITIALIZE_ERROR_SOURCE
168
199
@@ -365,6 +396,9 @@ class ChipError
365
396
#if CHIP_CONFIG_ERROR_SOURCE
366
397
const char * mFile ;
367
398
unsigned int mLine ;
399
+ #if __cplusplus >= 202002L
400
+ std::source_location mSourceLocation ;
401
+ #endif // __cplusplus >= 202002L
368
402
#endif // CHIP_CONFIG_ERROR_SOURCE
369
403
370
404
public:
0 commit comments