OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 class Test4 { | 66 class Test4 { |
67 int* data(); | 67 int* data(); |
68 size_t size(); | 68 size_t size(); |
69 }; | 69 }; |
70 static_assert(!HasDataAndSize<Test4, int>::value, | 70 static_assert(!HasDataAndSize<Test4, int>::value, |
71 ".data() and .size() are private"); | 71 ".data() and .size() are private"); |
72 | 72 |
73 } // namespace test_has_data_and_size | 73 } // namespace test_has_data_and_size |
74 | 74 |
| 75 namespace type_traits_impl { |
| 76 |
| 77 // Determines if the given type is an enum that converts implicitly to |
| 78 // an integral type. |
| 79 template <typename T> |
| 80 struct IsIntEnum { |
| 81 private: |
| 82 // This overload is used if the type is an enum, and unary plus |
| 83 // compiles and turns it into an integral type. |
| 84 template <typename X, |
| 85 typename std::enable_if< |
| 86 std::is_enum<X>::value && |
| 87 std::is_integral<decltype(+std::declval<X>())>::value>::type* = |
| 88 nullptr> |
| 89 static int Test(int); |
| 90 |
| 91 // Otherwise, this overload is used. |
| 92 template <typename> |
| 93 static char Test(...); |
| 94 |
| 95 public: |
| 96 static constexpr bool value = |
| 97 std::is_same<decltype(Test<typename std::remove_reference<T>::type>(0)), |
| 98 int>::value; |
| 99 }; |
| 100 |
| 101 } // namespace type_traits_impl |
| 102 |
| 103 // Determines if the given type is integral, or an enum that |
| 104 // converts implicitly to an integral type. |
| 105 template <typename T> |
| 106 struct IsIntlike { |
| 107 private: |
| 108 using X = typename std::remove_reference<T>::type; |
| 109 |
| 110 public: |
| 111 static constexpr bool value = |
| 112 std::is_integral<X>::value || type_traits_impl::IsIntEnum<X>::value; |
| 113 }; |
| 114 |
| 115 namespace test_enum_intlike { |
| 116 |
| 117 enum E1 { e1 }; |
| 118 enum { e2 }; |
| 119 enum class E3 { e3 }; |
| 120 struct S {}; |
| 121 |
| 122 static_assert(type_traits_impl::IsIntEnum<E1>::value, ""); |
| 123 static_assert(type_traits_impl::IsIntEnum<decltype(e2)>::value, ""); |
| 124 static_assert(!type_traits_impl::IsIntEnum<E3>::value, ""); |
| 125 static_assert(!type_traits_impl::IsIntEnum<int>::value, ""); |
| 126 static_assert(!type_traits_impl::IsIntEnum<float>::value, ""); |
| 127 static_assert(!type_traits_impl::IsIntEnum<S>::value, ""); |
| 128 |
| 129 static_assert(IsIntlike<E1>::value, ""); |
| 130 static_assert(IsIntlike<decltype(e2)>::value, ""); |
| 131 static_assert(!IsIntlike<E3>::value, ""); |
| 132 static_assert(IsIntlike<int>::value, ""); |
| 133 static_assert(!IsIntlike<float>::value, ""); |
| 134 static_assert(!IsIntlike<S>::value, ""); |
| 135 |
| 136 } // test_enum_intlike |
| 137 |
75 } // namespace rtc | 138 } // namespace rtc |
76 | 139 |
77 #endif // WEBRTC_BASE_TYPE_TRAITS_H_ | 140 #endif // WEBRTC_BASE_TYPE_TRAITS_H_ |
OLD | NEW |