Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(434)

Unified Diff: webrtc/base/type_traits.h

Issue 2809513002: Move IsIntlike to type_traits.h (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « webrtc/base/safe_compare.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/base/type_traits.h
diff --git a/webrtc/base/type_traits.h b/webrtc/base/type_traits.h
index d6a6c37334602d21111a2fe60f1204d8f90f5e41..a57bead6c18899123814739788fc036f0bcd92d0 100644
--- a/webrtc/base/type_traits.h
+++ b/webrtc/base/type_traits.h
@@ -72,6 +72,69 @@ static_assert(!HasDataAndSize<Test4, int>::value,
} // namespace test_has_data_and_size
+namespace type_traits_impl {
+
+// Determines if the given type is an enum that converts implicitly to
+// an integral type.
+template <typename T>
+struct IsIntEnum {
+ private:
+ // This overload is used if the type is an enum, and unary plus
+ // compiles and turns it into an integral type.
+ template <typename X,
+ typename std::enable_if<
+ std::is_enum<X>::value &&
+ std::is_integral<decltype(+std::declval<X>())>::value>::type* =
+ nullptr>
+ static int Test(int);
+
+ // Otherwise, this overload is used.
+ template <typename>
+ static char Test(...);
+
+ public:
+ static constexpr bool value =
+ std::is_same<decltype(Test<typename std::remove_reference<T>::type>(0)),
+ int>::value;
+};
+
+} // namespace type_traits_impl
+
+// Determines if the given type is integral, or an enum that
+// converts implicitly to an integral type.
+template <typename T>
+struct IsIntlike {
+ private:
+ using X = typename std::remove_reference<T>::type;
+
+ public:
+ static constexpr bool value =
+ std::is_integral<X>::value || type_traits_impl::IsIntEnum<X>::value;
+};
+
+namespace test_enum_intlike {
+
+enum E1 { e1 };
+enum { e2 };
+enum class E3 { e3 };
+struct S {};
+
+static_assert(type_traits_impl::IsIntEnum<E1>::value, "");
+static_assert(type_traits_impl::IsIntEnum<decltype(e2)>::value, "");
+static_assert(!type_traits_impl::IsIntEnum<E3>::value, "");
+static_assert(!type_traits_impl::IsIntEnum<int>::value, "");
+static_assert(!type_traits_impl::IsIntEnum<float>::value, "");
+static_assert(!type_traits_impl::IsIntEnum<S>::value, "");
+
+static_assert(IsIntlike<E1>::value, "");
+static_assert(IsIntlike<decltype(e2)>::value, "");
+static_assert(!IsIntlike<E3>::value, "");
+static_assert(IsIntlike<int>::value, "");
+static_assert(!IsIntlike<float>::value, "");
+static_assert(!IsIntlike<S>::value, "");
+
+} // test_enum_intlike
+
} // namespace rtc
#endif // WEBRTC_BASE_TYPE_TRAITS_H_
« no previous file with comments | « webrtc/base/safe_compare.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698