1 /*
2  * Copyright 2014 Google Inc. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef FRUIT_META_ALGOS_H
18 #define FRUIT_META_ALGOS_H
19 
20 #include <fruit/impl/fruit-config.h>
21 #include <fruit/impl/meta/immutable_map.h>
22 
23 namespace fruit {
24 namespace impl {
25 namespace meta {
26 
27 // We need a different (slower) implementation to workaround a Clang bug:
28 // https://llvm.org/bugs/show_bug.cgi?id=25669
29 // TODO: remove this once that bug is fixed (for the appropriate Clang versions).
30 #if FRUIT_HAS_CLANG_ARBITRARY_OVERLOAD_RESOLUTION_BUG
31 
32 struct HasDuplicatesHelper {
33   template <typename... Types>
34   struct apply {
35     using type = Bool<false>;
36   };
37 
38   template <typename Type, typename... Types>
39   struct apply<Type, Types...> {
40     using type = Or(StaticOr<std::is_same<Type, Types>::value...>, Id<HasDuplicatesHelper(Types...)>);
41   };
42 };
43 
44 struct HasDuplicates {
45   template <typename V>
46   struct apply;
47 
48   template <typename... Types>
49   struct apply<Vector<Types...>> {
50     using type = HasDuplicatesHelper(Types...);
51   };
52 };
53 
54 #else // !FRUIT_HAS_CLANG_ARBITRARY_OVERLOAD_RESOLUTION_BUG
55 
56 // Checks if the given Vector has duplicated types.
57 struct HasDuplicates {
58   template <typename V>
59   struct apply;
60 
61   template <typename... Types>
62   struct apply<Vector<Types...>> {
63     using M = VectorsToImmutableMap(Vector<Types...>, GenerateIntSequence(Int<sizeof...(Types)>));
64     using type = Not(StaticAnd<Eval<ImmutableMapContainsKey(M, Types)>::value...>);
65   };
66 };
67 
68 #endif // FRUIT_HAS_CLANG_ARBITRARY_OVERLOAD_RESOLUTION_BUG
69 
70 } // namespace meta
71 } // namespace impl
72 } // namespace fruit
73 
74 #endif // FRUIT_META_ALGOS_H
75