blob: 04e5e37fc05c9aff32fd2f78be6bc7f45650d147 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
kwiberg@webrtc.orgaf9d56f2015-01-13 20:32:04 +00002 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00003 *
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
kwiberg@webrtc.orgaf9d56f2015-01-13 20:32:04 +000011// Borrowed from Chromium's src/base/template_util.h.
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#ifndef RTC_BASE_TEMPLATE_UTIL_H_
14#define RTC_BASE_TEMPLATE_UTIL_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <stddef.h> // For size_t.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020018namespace rtc {
19
20// Template definitions from tr1.
21
22template<class T, T v>
23struct integral_constant {
24 static const T value = v;
25 typedef T value_type;
26 typedef integral_constant<T, v> type;
27};
28
29template <class T, T v> const T integral_constant<T, v>::value;
30
31typedef integral_constant<bool, true> true_type;
32typedef integral_constant<bool, false> false_type;
33
34template <class T> struct is_pointer : false_type {};
35template <class T> struct is_pointer<T*> : true_type {};
36
37template <class T, class U> struct is_same : public false_type {};
38template <class T> struct is_same<T, T> : true_type {};
39
40template<class> struct is_array : public false_type {};
41template<class T, size_t n> struct is_array<T[n]> : public true_type {};
42template<class T> struct is_array<T[]> : public true_type {};
43
44template <class T> struct is_non_const_reference : false_type {};
45template <class T> struct is_non_const_reference<T&> : true_type {};
46template <class T> struct is_non_const_reference<const T&> : false_type {};
47
48template <class T> struct is_void : false_type {};
49template <> struct is_void<void> : true_type {};
50
51// Helper useful for converting a tuple to variadic template function
52// arguments.
53//
54// sequence_generator<3>::type will be sequence<0, 1, 2>.
55template <int...>
56struct sequence {};
57template <int N, int... S>
58struct sequence_generator : sequence_generator<N - 1, N - 1, S...> {};
59template <int... S>
60struct sequence_generator<0, S...> {
61 typedef sequence<S...> type;
62};
63
64namespace internal {
65
66// Types YesType and NoType are guaranteed such that sizeof(YesType) <
67// sizeof(NoType).
68typedef char YesType;
69
70struct NoType {
71 YesType dummy[2];
72};
73
74// This class is an implementation detail for is_convertible, and you
75// don't need to know how it works to use is_convertible. For those
76// who care: we declare two different functions, one whose argument is
77// of type To and one with a variadic argument list. We give them
78// return types of different size, so we can use sizeof to trick the
79// compiler into telling us which function it would have chosen if we
80// had called it with an argument of type From. See Alexandrescu's
81// _Modern C++ Design_ for more details on this sort of trick.
82
83struct ConvertHelper {
84 template <typename To>
85 static YesType Test(To);
86
87 template <typename To>
88 static NoType Test(...);
89
90 template <typename From>
91 static From& Create();
92};
93
94// Used to determine if a type is a struct/union/class. Inspired by Boost's
95// is_class type_trait implementation.
96struct IsClassHelper {
97 template <typename C>
98 static YesType Test(void(C::*)(void));
99
100 template <typename C>
101 static NoType Test(...);
102};
103
104} // namespace internal
105
106// Inherits from true_type if From is convertible to To, false_type otherwise.
107//
108// Note that if the type is convertible, this will be a true_type REGARDLESS
109// of whether or not the conversion would emit a warning.
110template <typename From, typename To>
111struct is_convertible
112 : integral_constant<bool,
113 sizeof(internal::ConvertHelper::Test<To>(
114 internal::ConvertHelper::Create<From>())) ==
115 sizeof(internal::YesType)> {
116};
117
118template <typename T>
119struct is_class
120 : integral_constant<bool,
121 sizeof(internal::IsClassHelper::Test<T>(0)) ==
122 sizeof(internal::YesType)> {
123};
124
125} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000126
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200127#endif // RTC_BASE_TEMPLATE_UTIL_H_