Skip to content

Commit aa3d8d2

Browse files
committed
Add std::source_location to ChipError for C++20 builds
1 parent 035d302 commit aa3d8d2

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

src/lib/core/CHIPError.h

+34
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include <limits>
3636
#include <type_traits>
3737

38+
#if __cplusplus >= 202002L
39+
#include <source_location>
40+
#endif // __cplusplus >= 202002L
41+
3842
namespace chip {
3943

4044
/**
@@ -112,7 +116,11 @@ class ChipError
112116

113117
// Helper for declaring constructors without too much repetition.
114118
#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
115122
#define CHIP_INITIALIZE_ERROR_SOURCE(f, l) , mFile((f)), mLine((l))
123+
#endif // __cplusplus >= 202002L
116124
#else // CHIP_CONFIG_ERROR_SOURCE
117125
#define CHIP_INITIALIZE_ERROR_SOURCE(f, l)
118126
#endif // CHIP_CONFIG_ERROR_SOURCE
@@ -123,23 +131,39 @@ class ChipError
123131
* @note
124132
* The result is valid only if CanEncapsulate() is true.
125133
*/
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
126142
constexpr ChipError(Range range, ValueType value) :
127143
mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0)
128144
{}
129145
constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line) :
130146
mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
131147
{}
148+
#endif // __cplusplus >= 202002L
132149

133150
/**
134151
* Construct a CHIP_ERROR for SdkPart @a part with @a code.
135152
*
136153
* @note
137154
* The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed.
138155
*/
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
139162
constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {}
140163
constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) :
141164
mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
142165
{}
166+
#endif // __cplusplus >= 202002L
143167

144168
/**
145169
* Construct a CHIP_ERROR constant for SdkPart @a part with @a code at the current source line.
@@ -159,10 +183,17 @@ class ChipError
159183
* @note
160184
* This is intended to be used only in foreign function interfaces.
161185
*/
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
162192
explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {}
163193
explicit constexpr ChipError(StorageType error, const char * file, unsigned int line) :
164194
mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line)
165195
{}
196+
#endif // __cplusplus >= 202002L
166197

167198
#undef CHIP_INITIALIZE_ERROR_SOURCE
168199

@@ -365,6 +396,9 @@ class ChipError
365396
#if CHIP_CONFIG_ERROR_SOURCE
366397
const char * mFile;
367398
unsigned int mLine;
399+
#if __cplusplus >= 202002L
400+
std::source_location mSourceLocation;
401+
#endif // __cplusplus >= 202002L
368402
#endif // CHIP_CONFIG_ERROR_SOURCE
369403

370404
public:

0 commit comments

Comments
 (0)