Index: webrtc/base/array_view.h |
diff --git a/webrtc/base/array_view.h b/webrtc/base/array_view.h |
index 783e717b7bdee1c25b12c437d26543fc0532924b..27390c652db450c617f9061467cf937a31a36cb8 100644 |
--- a/webrtc/base/array_view.h |
+++ b/webrtc/base/array_view.h |
@@ -11,10 +11,40 @@ |
#ifndef WEBRTC_BASE_ARRAY_VIEW_H_ |
#define WEBRTC_BASE_ARRAY_VIEW_H_ |
+#include <type_traits> |
+ |
#include "webrtc/base/checks.h" |
namespace rtc { |
+namespace internal { |
+ |
+// (Internal; please don't use outside this file.) Determines if the given |
+// class has zero-argument .data() and .size() methods whose return values are |
+// convertible to T* and size_t, respectively. |
+template <typename DS, typename T> |
+class HasDataAndSize { |
+ private: |
+ using SizeOne = char[1]; |
+ using SizeTwo = char[2]; |
+ |
+ template < |
+ typename C, |
+ typename std::enable_if< |
+ std::is_convertible<decltype(std::declval<C>().data()), T*>::value && |
+ std::is_convertible<decltype(std::declval<C>().size()), |
+ size_t>::value>::type* = nullptr> |
+ static SizeOne& Test(int); |
+ |
+ template <typename> |
+ static SizeTwo& Test(...); |
+ |
+ public: |
+ static constexpr bool value = (sizeof(Test<DS>(0)) == 1); |
tommi
2016/09/03 14:56:47
nit: == sizeof(SizeOne) to help readers connect th
kwiberg-webrtc
2016/09/03 15:56:43
I shortened it some more, with the help of std::is
|
+}; |
+ |
+} // namespace internal |
+ |
// Many functions read from or write to arrays. The obvious way to do this is |
// to use two arguments, a pointer to the first element and an element count: |
// |
@@ -95,7 +125,9 @@ class ArrayView final { |
// or ArrayView<const T>, const std::vector<T> to ArrayView<const T>, and |
// rtc::Buffer to ArrayView<uint8_t> (with the same const behavior as |
// std::vector). |
- template <typename U> |
+ template <typename U, |
+ typename std::enable_if< |
+ internal::HasDataAndSize<U, T>::value>::type* = nullptr> |
ArrayView(U& u) : ArrayView(u.data(), u.size()) {} |
// Indexing, size, and iteration. These allow mutation even if the ArrayView |