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