OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
7 | 7 |
8 namespace mojo { | 8 namespace mojo { |
9 namespace internal { | 9 namespace internal { |
10 | 10 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // This goop is a trick used to implement a template that can be used to | 63 // This goop is a trick used to implement a template that can be used to |
64 // determine if a given class is the base class of another given class. | 64 // determine if a given class is the base class of another given class. |
65 template <typename, typename> | 65 template <typename, typename> |
66 struct IsSame { | 66 struct IsSame { |
67 static bool const value = false; | 67 static bool const value = false; |
68 }; | 68 }; |
69 template <typename A> | 69 template <typename A> |
70 struct IsSame<A, A> { | 70 struct IsSame<A, A> { |
71 static bool const value = true; | 71 static bool const value = true; |
72 }; | 72 }; |
| 73 |
| 74 template <typename T> |
| 75 struct EnsureTypeIsComplete { |
| 76 // sizeof() cannot be applied to incomplete types, this line will fail |
| 77 // compilation if T is forward declaration. |
| 78 using CheckSize = char (*)[sizeof(T)]; |
| 79 }; |
| 80 |
73 template <typename Base, typename Derived> | 81 template <typename Base, typename Derived> |
74 struct IsBaseOf { | 82 struct IsBaseOf { |
75 private: | 83 private: |
76 // This class doesn't work correctly with forward declarations. | |
77 // Because sizeof cannot be applied to incomplete types, this line prevents us | |
78 // from passing in forward declarations. | |
79 typedef char (*EnsureTypesAreComplete)[sizeof(Base) + sizeof(Derived)]; | |
80 | |
81 static Derived* CreateDerived(); | 84 static Derived* CreateDerived(); |
82 static char(&Check(Base*))[1]; | 85 static char(&Check(Base*))[1]; |
83 static char(&Check(...))[2]; | 86 static char(&Check(...))[2]; |
84 | 87 |
| 88 EnsureTypeIsComplete<Base> check_base_; |
| 89 EnsureTypeIsComplete<Derived> check_derived_; |
| 90 |
85 public: | 91 public: |
86 static bool const value = sizeof Check(CreateDerived()) == 1 && | 92 static bool const value = sizeof Check(CreateDerived()) == 1 && |
87 !IsSame<Base const, void const>::value; | 93 !IsSame<Base const, void const>::value; |
88 }; | 94 }; |
89 | 95 |
90 template <class T> | 96 template <class T> |
91 struct RemovePointer { | 97 struct RemovePointer { |
92 typedef T type; | 98 typedef T type; |
93 }; | 99 }; |
94 template <class T> | 100 template <class T> |
(...skipping 14 matching lines...) Expand all Loading... |
109 | 115 |
110 template <typename T, typename F> | 116 template <typename T, typename F> |
111 struct Conditional<false, T, F> { | 117 struct Conditional<false, T, F> { |
112 typedef F type; | 118 typedef F type; |
113 }; | 119 }; |
114 | 120 |
115 } // namespace internal | 121 } // namespace internal |
116 } // namespace mojo | 122 } // namespace mojo |
117 | 123 |
118 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ | 124 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_TEMPLATE_UTIL_H_ |
OLD | NEW |