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_MAP_TRAITS_WTF_H_
6#define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_H_
7
8#include "base/logging.h"
9#include "mojo/public/cpp/bindings/map_traits.h"
10#include "mojo/public/cpp/bindings/wtf_map.h"
11
12namespace mojo {
13
14template <typename K, typename V>
15struct MapTraits<WTFMap<K, V>> {
16  using Key = K;
17  using Value = V;
18  using Iterator = typename WTFMap<K, V>::Iterator;
19  using ConstIterator = typename WTFMap<K, V>::ConstIterator;
20
21  static bool IsNull(const WTFMap<K, V>& input) { return input.is_null(); }
22  static void SetToNull(WTFMap<K, V>* output) { *output = nullptr; }
23
24  static size_t GetSize(const WTFMap<K, V>& input) { return input.size(); }
25
26  static ConstIterator GetBegin(const WTFMap<K, V>& input) {
27    return input.begin();
28  }
29  static Iterator GetBegin(WTFMap<K, V>& input) { return input.begin(); }
30
31  static void AdvanceIterator(ConstIterator& iterator) { ++iterator; }
32  static void AdvanceIterator(Iterator& iterator) { ++iterator; }
33
34  static const K& GetKey(Iterator& iterator) { return iterator->key; }
35  static const K& GetKey(ConstIterator& iterator) { return iterator->key; }
36
37  static V& GetValue(Iterator& iterator) { return iterator->value; }
38  static const V& GetValue(ConstIterator& iterator) { return iterator->value; }
39
40  static bool Insert(WTFMap<K, V>& input, const K& key, V&& value) {
41    if (!WTFMap<K, V>::IsValidKey(key)) {
42      LOG(ERROR) << "The key value is disallowed by WTF::HashMap: " << key;
43      return false;
44    }
45    input.insert(key, std::forward<V>(value));
46    return true;
47  }
48  static bool Insert(WTFMap<K, V>& input, const K& key, const V& value) {
49    if (!WTFMap<K, V>::IsValidKey(key)) {
50      LOG(ERROR) << "The key value is disallowed by WTF::HashMap: " << key;
51      return false;
52    }
53    input.insert(key, value);
54    return true;
55  }
56
57  static void SetToEmpty(WTFMap<K, V>* output) { output->SetToEmpty(); }
58};
59
60}  // namespace mojo
61
62#endif  // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_H_
63