Home | History | Annotate | Download | only in lib
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
      6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
      7 
      8 #include <type_traits>
      9 
     10 #include "mojo/public/cpp/bindings/lib/clone_equals_util.h"
     11 #include "third_party/WebKit/Source/wtf/HashMap.h"
     12 #include "third_party/WebKit/Source/wtf/Optional.h"
     13 #include "third_party/WebKit/Source/wtf/Vector.h"
     14 #include "third_party/WebKit/Source/wtf/text/WTFString.h"
     15 
     16 namespace mojo {
     17 namespace internal {
     18 
     19 template <typename T>
     20 struct CloneTraits<WTF::Vector<T>, false> {
     21   static WTF::Vector<T> Clone(const WTF::Vector<T>& input) {
     22     WTF::Vector<T> result;
     23     result.reserveCapacity(input.size());
     24     for (const auto& element : input)
     25       result.append(internal::Clone(element));
     26 
     27     return result;
     28   }
     29 };
     30 
     31 template <typename K, typename V>
     32 struct CloneTraits<WTF::HashMap<K, V>, false> {
     33   static WTF::HashMap<K, V> Clone(const WTF::HashMap<K, V>& input) {
     34     WTF::HashMap<K, V> result;
     35     auto input_end = input.end();
     36     for (auto it = input.begin(); it != input_end; ++it)
     37       result.add(internal::Clone(it->key), internal::Clone(it->value));
     38     return result;
     39   }
     40 };
     41 
     42 template <typename T>
     43 struct EqualsTraits<WTF::Vector<T>, false> {
     44   static bool Equals(const WTF::Vector<T>& a, const WTF::Vector<T>& b) {
     45     if (a.size() != b.size())
     46       return false;
     47     for (size_t i = 0; i < a.size(); ++i) {
     48       if (!internal::Equals(a[i], b[i]))
     49         return false;
     50     }
     51     return true;
     52   }
     53 };
     54 
     55 template <typename K, typename V>
     56 struct EqualsTraits<WTF::HashMap<K, V>, false> {
     57   static bool Equals(const WTF::HashMap<K, V>& a, const WTF::HashMap<K, V>& b) {
     58     if (a.size() != b.size())
     59       return false;
     60 
     61     auto a_end = a.end();
     62     auto b_end = b.end();
     63 
     64     for (auto iter = a.begin(); iter != a_end; ++iter) {
     65       auto b_iter = b.find(iter->key);
     66       if (b_iter == b_end || !internal::Equals(iter->value, b_iter->value))
     67         return false;
     68     }
     69     return true;
     70   }
     71 };
     72 
     73 }  // namespace internal
     74 }  // namespace mojo
     75 
     76 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
     77