diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 38b76bca539c2a..728ce4d7e16bf4 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -35,6 +35,10 @@ #include #include +#if __cplusplus >= 202002L +#include +#endif // __cplusplus >= 202002L + namespace chip { /** @@ -112,7 +116,11 @@ class ChipError // Helper for declaring constructors without too much repetition. #if CHIP_CONFIG_ERROR_SOURCE +#if __cplusplus >= 202002L +#define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) , mFile((f)), mLine((l)), mSourceLocation((loc)) +#else #define CHIP_INITIALIZE_ERROR_SOURCE(f, l) , mFile((f)), mLine((l)) +#endif // __cplusplus >= 202002L #else // CHIP_CONFIG_ERROR_SOURCE #define CHIP_INITIALIZE_ERROR_SOURCE(f, l) #endif // CHIP_CONFIG_ERROR_SOURCE @@ -123,12 +131,21 @@ class ChipError * @note * The result is valid only if CanEncapsulate() is true. */ +#if __cplusplus >= 202002L + constexpr ChipError(Range range, ValueType value) : + mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) + {} + constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) + {} +#else constexpr ChipError(Range range, ValueType value) : mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line) : mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} +#endif // __cplusplus >= 202002L /** * Construct a CHIP_ERROR for SdkPart @a part with @a code. @@ -136,10 +153,17 @@ class ChipError * @note * The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed. */ +#if __cplusplus >= 202002L + constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} + constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) + {} +#else constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} +#endif // __cplusplus >= 202002L /** * Construct a CHIP_ERROR constant for SdkPart @a part with @a code at the current source line. @@ -159,10 +183,17 @@ class ChipError * @note * This is intended to be used only in foreign function interfaces. */ +#if __cplusplus >= 202002L + explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} + explicit constexpr ChipError(StorageType error, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) + {} +#else explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} explicit constexpr ChipError(StorageType error, const char * file, unsigned int line) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} +#endif // __cplusplus >= 202002L #undef CHIP_INITIALIZE_ERROR_SOURCE @@ -365,6 +396,9 @@ class ChipError #if CHIP_CONFIG_ERROR_SOURCE const char * mFile; unsigned int mLine; +#if __cplusplus >= 202002L + std::source_location mSourceLocation; +#endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE public: