1/*
2 *  Copyright (c) 2013 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// Borrowed from Chromium's src/base/template_util.h.
12
13#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TEMPLATE_UTIL_H_
14#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TEMPLATE_UTIL_H_
15
16#include <stddef.h>  // For size_t.
17
18namespace webrtc {
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
51namespace internal {
52
53// Types YesType and NoType are guaranteed such that sizeof(YesType) <
54// sizeof(NoType).
55typedef char YesType;
56
57struct NoType {
58  YesType dummy[2];
59};
60
61// This class is an implementation detail for is_convertible, and you
62// don't need to know how it works to use is_convertible. For those
63// who care: we declare two different functions, one whose argument is
64// of type To and one with a variadic argument list. We give them
65// return types of different size, so we can use sizeof to trick the
66// compiler into telling us which function it would have chosen if we
67// had called it with an argument of type From.  See Alexandrescu's
68// _Modern C++ Design_ for more details on this sort of trick.
69
70struct ConvertHelper {
71  template <typename To>
72  static YesType Test(To);
73
74  template <typename To>
75  static NoType Test(...);
76
77  template <typename From>
78  static From& Create();
79};
80
81// Used to determine if a type is a struct/union/class. Inspired by Boost's
82// is_class type_trait implementation.
83struct IsClassHelper {
84  template <typename C>
85  static YesType Test(void(C::*)(void));
86
87  template <typename C>
88  static NoType Test(...);
89};
90
91}  // namespace internal
92
93// Inherits from true_type if From is convertible to To, false_type otherwise.
94//
95// Note that if the type is convertible, this will be a true_type REGARDLESS
96// of whether or not the conversion would emit a warning.
97template <typename From, typename To>
98struct is_convertible
99    : integral_constant<bool,
100                        sizeof(internal::ConvertHelper::Test<To>(
101                                   internal::ConvertHelper::Create<From>())) ==
102                        sizeof(internal::YesType)> {
103};
104
105template <typename T>
106struct is_class
107    : integral_constant<bool,
108                        sizeof(internal::IsClassHelper::Test<T>(0)) ==
109                            sizeof(internal::YesType)> {
110};
111
112}  // namespace webrtc
113
114#endif  // WEBRTC_SYSTEM_WRAPPERS_INTERFACE_TEMPLATE_UTIL_H_
115