Adding support for enum class in RTC_CHECK and RTC_LOG.
Enum class types are by design not convertible to arithmetic types. As a result they are currently not supported in RTC_CHECK and RTC_LOG. The current workaround was to use something like RTC_CHECK(v1 == v2) instead of RTC_CHECK_EQ(v1, v2). This change adds support for any enum class type by converting it to the underlying type. Bug: webrtc:10418 Change-Id: I59e6608e6a97a4cc007c903f8e021a58d4c49ff8 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/128202 Reviewed-by: Karl Wiberg <kwiberg@webrtc.org> Commit-Queue: Amit Hilbuch <amithi@webrtc.org> Cr-Commit-Position: refs/heads/master@{#27166}
This commit is contained in:
@ -168,6 +168,16 @@ inline Val<CheckArgType::kVoidP, const void*> MakeVal(const void* x) {
|
|||||||
return {x};
|
return {x};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The enum class types are not implicitly convertible to arithmetic types.
|
||||||
|
template <
|
||||||
|
typename T,
|
||||||
|
typename std::enable_if<std::is_enum<T>::value &&
|
||||||
|
!std::is_arithmetic<T>::value>::type* = nullptr>
|
||||||
|
inline decltype(MakeVal(std::declval<typename std::underlying_type<T>::type>()))
|
||||||
|
MakeVal(T x) {
|
||||||
|
return {static_cast<typename std::underlying_type<T>::type>(x)};
|
||||||
|
}
|
||||||
|
|
||||||
// Ephemeral type that represents the result of the logging << operator.
|
// Ephemeral type that represents the result of the logging << operator.
|
||||||
template <typename... Ts>
|
template <typename... Ts>
|
||||||
class LogStreamer;
|
class LogStreamer;
|
||||||
@ -176,18 +186,18 @@ class LogStreamer;
|
|||||||
template <>
|
template <>
|
||||||
class LogStreamer<> final {
|
class LogStreamer<> final {
|
||||||
public:
|
public:
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<std::is_arithmetic<U>::value ||
|
||||||
typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
|
std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
||||||
U arg) const {
|
U arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<!std::is_arithmetic<U>::value &&
|
||||||
typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
|
!std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
||||||
const U& arg) const {
|
const U& arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
||||||
@ -222,18 +232,18 @@ class LogStreamer<T, Ts...> final {
|
|||||||
RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
|
RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
|
||||||
: arg_(arg), prior_(prior) {}
|
: arg_(arg), prior_(prior) {}
|
||||||
|
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<std::is_arithmetic<U>::value ||
|
||||||
typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
|
std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
||||||
operator<<(U arg) const {
|
operator<<(U arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
||||||
MakeVal(arg), this);
|
MakeVal(arg), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<!std::is_arithmetic<U>::value &&
|
||||||
typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
|
!std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
||||||
operator<<(const U& arg) const {
|
operator<<(const U& arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
||||||
|
@ -248,6 +248,16 @@ inline Val<LogArgType::kLogMetadataErr, LogMetadataErr> MakeVal(
|
|||||||
return {x};
|
return {x};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The enum class types are not implicitly convertible to arithmetic types.
|
||||||
|
template <
|
||||||
|
typename T,
|
||||||
|
typename std::enable_if<std::is_enum<T>::value &&
|
||||||
|
!std::is_arithmetic<T>::value>::type* = nullptr>
|
||||||
|
inline decltype(MakeVal(std::declval<typename std::underlying_type<T>::type>()))
|
||||||
|
MakeVal(T x) {
|
||||||
|
return {static_cast<typename std::underlying_type<T>::type>(x)};
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef WEBRTC_ANDROID
|
#ifdef WEBRTC_ANDROID
|
||||||
inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
|
inline Val<LogArgType::kLogMetadataTag, LogMetadataTag> MakeVal(
|
||||||
const LogMetadataTag& x) {
|
const LogMetadataTag& x) {
|
||||||
@ -285,18 +295,18 @@ class LogStreamer;
|
|||||||
template <>
|
template <>
|
||||||
class LogStreamer<> final {
|
class LogStreamer<> final {
|
||||||
public:
|
public:
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<std::is_arithmetic<U>::value ||
|
||||||
typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
|
std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
||||||
U arg) const {
|
U arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
||||||
this);
|
this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<!std::is_arithmetic<U>::value &&
|
||||||
typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
|
!std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>()))> operator<<(
|
||||||
const U& arg) const {
|
const U& arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
return LogStreamer<decltype(MakeVal(std::declval<U>()))>(MakeVal(arg),
|
||||||
@ -318,18 +328,18 @@ class LogStreamer<T, Ts...> final {
|
|||||||
RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
|
RTC_FORCE_INLINE LogStreamer(T arg, const LogStreamer<Ts...>* prior)
|
||||||
: arg_(arg), prior_(prior) {}
|
: arg_(arg), prior_(prior) {}
|
||||||
|
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<std::is_arithmetic<U>::value ||
|
||||||
typename std::enable_if<std::is_arithmetic<U>::value>::type* = nullptr>
|
std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
||||||
operator<<(U arg) const {
|
operator<<(U arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
||||||
MakeVal(arg), this);
|
MakeVal(arg), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <
|
template <typename U,
|
||||||
typename U,
|
typename std::enable_if<!std::is_arithmetic<U>::value &&
|
||||||
typename std::enable_if<!std::is_arithmetic<U>::value>::type* = nullptr>
|
!std::is_enum<U>::value>::type* = nullptr>
|
||||||
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
RTC_FORCE_INLINE LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>
|
||||||
operator<<(const U& arg) const {
|
operator<<(const U& arg) const {
|
||||||
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
return LogStreamer<decltype(MakeVal(std::declval<U>())), T, Ts...>(
|
||||||
|
@ -368,4 +368,16 @@ TEST(LogTest, Perf) {
|
|||||||
<< " total bytes logged: " << str.size();
|
<< " total bytes logged: " << str.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(LogTest, EnumsAreSupported) {
|
||||||
|
enum class TestEnum { kValue0 = 0, kValue1 = 1 };
|
||||||
|
std::string str;
|
||||||
|
LogSinkImpl<StringStream> stream(&str);
|
||||||
|
LogMessage::AddLogToStream(&stream, LS_INFO);
|
||||||
|
RTC_LOG(LS_INFO) << "[" << TestEnum::kValue0 << "]";
|
||||||
|
EXPECT_NE(std::string::npos, str.find("[0]"));
|
||||||
|
EXPECT_EQ(std::string::npos, str.find("[1]"));
|
||||||
|
RTC_LOG(LS_INFO) << "[" << TestEnum::kValue1 << "]";
|
||||||
|
EXPECT_NE(std::string::npos, str.find("[1]"));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace rtc
|
} // namespace rtc
|
||||||
|
Reference in New Issue
Block a user