| OLD | NEW | 
|    1 // Copyright 2016 The Chromium Authors. All rights reserved. |    1 // Copyright 2016 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_CLONE_EQUALS_UTIL_H_ |    5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_EQUALS_TRAITS_H_ | 
|    6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ |    6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_EQUALS_TRAITS_H_ | 
|    7  |    7  | 
|    8 #include <type_traits> |    8 #include <type_traits> | 
|    9 #include <unordered_map> |    9 #include <unordered_map> | 
|   10 #include <vector> |   10 #include <vector> | 
|   11  |   11  | 
|   12 #include "base/optional.h" |   12 #include "base/optional.h" | 
|   13 #include "mojo/public/cpp/bindings/lib/template_util.h" |   13 #include "mojo/public/cpp/bindings/lib/template_util.h" | 
|   14  |   14  | 
|   15 namespace mojo { |   15 namespace mojo { | 
|   16 namespace internal { |   16 namespace internal { | 
|   17  |   17  | 
|   18 template <typename T> |   18 template <typename T> | 
|   19 struct HasCloneMethod { |  | 
|   20   template <typename U> |  | 
|   21   static char Test(decltype(&U::Clone)); |  | 
|   22   template <typename U> |  | 
|   23   static int Test(...); |  | 
|   24   static const bool value = sizeof(Test<T>(0)) == sizeof(char); |  | 
|   25  |  | 
|   26  private: |  | 
|   27   EnsureTypeIsComplete<T> check_t_; |  | 
|   28 }; |  | 
|   29  |  | 
|   30 template <typename T, bool has_clone_method = HasCloneMethod<T>::value> |  | 
|   31 struct CloneTraits; |  | 
|   32  |  | 
|   33 template <typename T> |  | 
|   34 T Clone(const T& input); |  | 
|   35  |  | 
|   36 template <typename T> |  | 
|   37 struct CloneTraits<T, true> { |  | 
|   38   static T Clone(const T& input) { return input.Clone(); } |  | 
|   39 }; |  | 
|   40  |  | 
|   41 template <typename T> |  | 
|   42 struct CloneTraits<T, false> { |  | 
|   43   static T Clone(const T& input) { return input; } |  | 
|   44 }; |  | 
|   45  |  | 
|   46 template <typename T> |  | 
|   47 struct CloneTraits<base::Optional<T>, false> { |  | 
|   48   static base::Optional<T> Clone(const base::Optional<T>& input) { |  | 
|   49     if (!input) |  | 
|   50       return base::nullopt; |  | 
|   51  |  | 
|   52     return base::Optional<T>(internal::Clone(*input)); |  | 
|   53   } |  | 
|   54 }; |  | 
|   55  |  | 
|   56 template <typename T> |  | 
|   57 struct CloneTraits<std::vector<T>, false> { |  | 
|   58   static std::vector<T> Clone(const std::vector<T>& input) { |  | 
|   59     std::vector<T> result; |  | 
|   60     result.reserve(input.size()); |  | 
|   61     for (const auto& element : input) |  | 
|   62       result.push_back(internal::Clone(element)); |  | 
|   63  |  | 
|   64     return result; |  | 
|   65   } |  | 
|   66 }; |  | 
|   67  |  | 
|   68 template <typename K, typename V> |  | 
|   69 struct CloneTraits<std::unordered_map<K, V>, false> { |  | 
|   70   static std::unordered_map<K, V> Clone(const std::unordered_map<K, V>& input) { |  | 
|   71     std::unordered_map<K, V> result; |  | 
|   72     for (const auto& element : input) { |  | 
|   73       result.insert(std::make_pair(internal::Clone(element.first), |  | 
|   74                                    internal::Clone(element.second))); |  | 
|   75     } |  | 
|   76     return result; |  | 
|   77   } |  | 
|   78 }; |  | 
|   79  |  | 
|   80 template <typename T> |  | 
|   81 T Clone(const T& input) { |  | 
|   82   return CloneTraits<T>::Clone(input); |  | 
|   83 }; |  | 
|   84  |  | 
|   85 template <typename T> |  | 
|   86 struct HasEqualsMethod { |   19 struct HasEqualsMethod { | 
|   87   template <typename U> |   20   template <typename U> | 
|   88   static char Test(decltype(&U::Equals)); |   21   static char Test(decltype(&U::Equals)); | 
|   89   template <typename U> |   22   template <typename U> | 
|   90   static int Test(...); |   23   static int Test(...); | 
|   91   static const bool value = sizeof(Test<T>(0)) == sizeof(char); |   24   static const bool value = sizeof(Test<T>(0)) == sizeof(char); | 
|   92  |   25  | 
|   93  private: |   26  private: | 
|   94   EnsureTypeIsComplete<T> check_t_; |   27   EnsureTypeIsComplete<T> check_t_; | 
|   95 }; |   28 }; | 
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  151 }; |   84 }; | 
|  152  |   85  | 
|  153 template <typename T> |   86 template <typename T> | 
|  154 bool Equals(const T& a, const T& b) { |   87 bool Equals(const T& a, const T& b) { | 
|  155   return EqualsTraits<T>::Equals(a, b); |   88   return EqualsTraits<T>::Equals(a, b); | 
|  156 } |   89 } | 
|  157  |   90  | 
|  158 }  // namespace internal |   91 }  // namespace internal | 
|  159 }  // namespace mojo |   92 }  // namespace mojo | 
|  160  |   93  | 
|  161 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_CLONE_EQUALS_UTIL_H_ |   94 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_EQUALS_TRAITS_H_ | 
| OLD | NEW |