Skip to content

Commit

Permalink
Add std::source_location to ChipError for C++20 builds
Browse files Browse the repository at this point in the history
  • Loading branch information
mwswartwout committed Apr 10, 2024
1 parent 0c0c6d6 commit 95974db
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/lib/core/CHIPError.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#include <limits>
#include <type_traits>

#if __cplusplus >= 202002L
#include <source_location>
#endif // __cplusplus >= 202002L

namespace chip {

/**
Expand Down Expand Up @@ -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
Expand All @@ -123,23 +131,39 @@ 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.
*
* @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.
Expand All @@ -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

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 95974db

Please sign in to comment.