- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / template_util.h
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef TALK_BASE_TEMPLATE_UTIL_H_
6 #define TALK_BASE_TEMPLATE_UTIL_H_
7
8 #include <cstddef>  // For size_t.
9
10 namespace talk_base {
11
12 // template definitions from tr1
13
14 template<class T, T v>
15 struct integral_constant {
16   static const T value = v;
17   typedef T value_type;
18   typedef integral_constant<T, v> type;
19 };
20
21 template <class T, T v> const T integral_constant<T, v>::value;
22
23 typedef integral_constant<bool, true> true_type;
24 typedef integral_constant<bool, false> false_type;
25
26 template <class T> struct is_pointer : false_type {};
27 template <class T> struct is_pointer<T*> : true_type {};
28
29 template <class T, class U> struct is_same : public false_type {};
30 template <class T> struct is_same<T,T> : true_type {};
31
32 template<class> struct is_array : public false_type {};
33 template<class T, size_t n> struct is_array<T[n]> : public true_type {};
34 template<class T> struct is_array<T[]> : public true_type {};
35
36 template <class T> struct is_non_const_reference : false_type {};
37 template <class T> struct is_non_const_reference<T&> : true_type {};
38 template <class T> struct is_non_const_reference<const T&> : false_type {};
39
40 template <class T> struct is_void : false_type {};
41 template <> struct is_void<void> : true_type {};
42
43 namespace internal {
44
45 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
46 // sizeof(NoType).
47 typedef char YesType;
48
49 struct NoType {
50   YesType dummy[2];
51 };
52
53 // This class is an implementation detail for is_convertible, and you
54 // don't need to know how it works to use is_convertible. For those
55 // who care: we declare two different functions, one whose argument is
56 // of type To and one with a variadic argument list. We give them
57 // return types of different size, so we can use sizeof to trick the
58 // compiler into telling us which function it would have chosen if we
59 // had called it with an argument of type From.  See Alexandrescu's
60 // _Modern C++ Design_ for more details on this sort of trick.
61
62 struct ConvertHelper {
63   template <typename To>
64   static YesType Test(To);
65
66   template <typename To>
67   static NoType Test(...);
68
69   template <typename From>
70   static From& Create();
71 };
72
73 // Used to determine if a type is a struct/union/class. Inspired by Boost's
74 // is_class type_trait implementation.
75 struct IsClassHelper {
76   template <typename C>
77   static YesType Test(void(C::*)(void));
78
79   template <typename C>
80   static NoType Test(...);
81 };
82
83 }  // namespace internal
84
85 // Inherits from true_type if From is convertible to To, false_type otherwise.
86 //
87 // Note that if the type is convertible, this will be a true_type REGARDLESS
88 // of whether or not the conversion would emit a warning.
89 template <typename From, typename To>
90 struct is_convertible
91     : integral_constant<bool,
92                         sizeof(internal::ConvertHelper::Test<To>(
93                                    internal::ConvertHelper::Create<From>())) ==
94                         sizeof(internal::YesType)> {
95 };
96
97 template <typename T>
98 struct is_class
99     : integral_constant<bool,
100                         sizeof(internal::IsClassHelper::Test<T>(0)) ==
101                             sizeof(internal::YesType)> {
102 };
103
104 }  // namespace talk_base
105
106 #endif  // TALK_BASE_TEMPLATE_UTIL_H_