1 // Copyright 2014 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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_
11 template <class T, T v>
12 struct IntegralConstant {
13 static const T value = v;
16 template <class T, T v>
17 const T IntegralConstant<T, v>::value;
19 typedef IntegralConstant<bool, true> TrueType;
20 typedef IntegralConstant<bool, false> FalseType;
23 struct IsConst : FalseType {};
25 struct IsConst<const T> : TrueType {};
28 struct IsPointer : FalseType {};
30 struct IsPointer<T*> : TrueType {};
32 template <bool B, typename T = void>
36 struct EnableIf<true, T> {
40 // Types YesType and NoType are guaranteed such that sizeof(YesType) <
48 // A helper template to determine if given type is non-const move-only-type,
49 // i.e. if a value of the given type should be passed via .Pass() in a
52 struct IsMoveOnlyType {
54 static YesType Test(const typename U::MoveOnlyTypeForCPP03*);
57 static NoType Test(...);
59 static const bool value =
60 sizeof(Test<T>(0)) == sizeof(YesType) && !IsConst<T>::value;
64 typename EnableIf<!IsMoveOnlyType<T>::value, T>::type& Forward(T& t) {
69 typename EnableIf<IsMoveOnlyType<T>::value, T>::type Forward(T& t) {
73 // This goop is a trick used to implement a template that can be used to
74 // determine if a given class is the base class of another given class.
75 template <typename, typename>
77 static bool const value = false;
81 static bool const value = true;
83 template <typename Base, typename Derived>
86 // This class doesn't work correctly with forward declarations.
87 // Because sizeof cannot be applied to incomplete types, this line prevents us
88 // from passing in forward declarations.
89 typedef char (*EnsureTypesAreComplete)[sizeof(Base) + sizeof(Derived)];
91 static Derived* CreateDerived();
92 static char(&Check(Base*))[1];
93 static char(&Check(...))[2];
96 static bool const value = sizeof Check(CreateDerived()) == 1 &&
97 !IsSame<Base const, void const>::value;
101 struct RemovePointer {};
103 struct RemovePointer<T*> {
107 template <template <typename...> class Template, typename T>
108 struct IsSpecializationOf : FalseType {};
110 template <template <typename...> class Template, typename... Args>
111 struct IsSpecializationOf<Template, Template<Args...>> : TrueType {};
113 } // namespace internal
116 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_