OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. | |
3 * | |
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 | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #ifndef WEBRTC_BASE_TYPE_TRAITS_H_ | |
12 #define WEBRTC_BASE_TYPE_TRAITS_H_ | |
13 | |
14 #include <cstddef> | |
15 #include <type_traits> | |
16 | |
17 namespace rtc { | |
18 | |
19 // Determines if the given class has zero-argument .data() and .size() methods | |
20 // whose return values are convertible to T* and size_t, respectively. | |
21 template <typename DS, typename T> | |
22 class HasDataAndSize { | |
23 private: | |
24 template < | |
25 typename C, | |
26 typename std::enable_if< | |
27 std::is_convertible<decltype(std::declval<C>().data()), T*>::value && | |
28 std::is_convertible<decltype(std::declval<C>().size()), | |
29 std::size_t>::value>::type* = nullptr> | |
30 static int Test(int); | |
31 | |
32 template <typename> | |
33 static char Test(...); | |
34 | |
35 public: | |
36 static constexpr bool value = std::is_same<decltype(Test<DS>(0)), int>::value; | |
37 }; | |
38 | |
39 namespace test_has_data_and_size { | |
ossu
2016/09/05 13:33:48
Should this be hidden away in a .cc file somewhere
kwiberg-webrtc
2016/09/05 14:04:11
It used to be in a separate file (array_view_unitt
| |
40 | |
41 template <typename DR, typename SR> | |
42 struct Test1 { | |
43 DR data(); | |
44 SR size(); | |
45 }; | |
46 static_assert(HasDataAndSize<Test1<int*, int>, int>::value, ""); | |
47 static_assert(HasDataAndSize<Test1<int*, int>, const int>::value, ""); | |
48 static_assert(HasDataAndSize<Test1<const int*, int>, const int>::value, ""); | |
49 static_assert(!HasDataAndSize<Test1<const int*, int>, int>::value, | |
50 "implicit cast of const int* to int*"); | |
51 static_assert(!HasDataAndSize<Test1<char*, size_t>, int>::value, | |
52 "implicit cast of char* to int*"); | |
53 | |
54 struct Test2 { | |
55 int* data; | |
56 size_t size; | |
57 }; | |
58 static_assert(!HasDataAndSize<Test2, int>::value, | |
59 ".data and .size aren't functions"); | |
60 | |
61 struct Test3 { | |
62 int* data(); | |
63 }; | |
64 static_assert(!HasDataAndSize<Test3, int>::value, ".size() is missing"); | |
65 | |
66 class Test4 { | |
67 int* data(); | |
68 size_t size(); | |
69 }; | |
70 static_assert(!HasDataAndSize<Test4, int>::value, | |
71 ".data() and .size() are private"); | |
72 | |
73 } // namespace test_has_data_and_size | |
74 | |
75 } // namespace rtc | |
76 | |
77 #endif // WEBRTC_BASE_TYPE_TRAITS_H_ | |
OLD | NEW |