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/clone_traits.h"
11 #include "mojo/public/cpp/bindings/equals_traits.h"
12 #include "third_party/blink/renderer/platform/wtf/hash_map.h"
13 #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
14 #include "third_party/blink/renderer/platform/wtf/vector.h"
15 
16 namespace mojo {
17 
18 template <typename T>
19 struct CloneTraits<WTF::Vector<T>, false> {
20   static WTF::Vector<T> Clone(const WTF::Vector<T>& input) {
21     WTF::Vector<T> result;
22     result.ReserveCapacity(input.size());
23     for (const auto& element : input)
24       result.push_back(mojo::Clone(element));
25 
26     return result;
27   }
28 };
29 
30 template <typename K, typename V>
31 struct CloneTraits<WTF::HashMap<K, V>, false> {
32   static WTF::HashMap<K, V> Clone(const WTF::HashMap<K, V>& input) {
33     WTF::HashMap<K, V> result;
34     for (const auto& element : input)
35       result.insert(mojo::Clone(element.key), mojo::Clone(element.value));
36 
37     return result;
38   }
39 };
40 
41 template <typename T>
42 struct EqualsTraits<WTF::Vector<T>, false> {
43   static bool Equals(const WTF::Vector<T>& a, const WTF::Vector<T>& b) {
44     if (a.size() != b.size())
45       return false;
46     for (size_t i = 0; i < a.size(); ++i) {
47       if (!mojo::Equals(a[i], b[i]))
48         return false;
49     }
50     return true;
51   }
52 };
53 
54 template <typename K, typename V>
55 struct EqualsTraits<WTF::HashMap<K, V>, false> {
56   static bool Equals(const WTF::HashMap<K, V>& a, const WTF::HashMap<K, V>& b) {
57     if (a.size() != b.size())
58       return false;
59 
60     auto a_end = a.end();
61     auto b_end = b.end();
62 
63     for (auto iter = a.begin(); iter != a_end; ++iter) {
64       auto b_iter = b.find(iter->key);
65       if (b_iter == b_end || !mojo::Equals(iter->value, b_iter->value))
66         return false;
67     }
68     return true;
69   }
70 };
71 
72 }  // namespace mojo
73 
74 #endif  // MOJO_PUBLIC_CPP_BINDINGS_LIB_WTF_CLONE_EQUALS_UTIL_H_
75