Home | History | Annotate | Download | only in bindings
      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_HASH_MAP_H_
      6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_HASH_MAP_H_
      7 
      8 #include "base/logging.h"
      9 #include "mojo/public/cpp/bindings/map_traits.h"
     10 #include "third_party/blink/renderer/platform/wtf/hash_map.h"
     11 
     12 namespace mojo {
     13 
     14 template <typename K, typename V>
     15 struct MapTraits<WTF::HashMap<K, V>> {
     16   using Key = K;
     17   using Value = V;
     18   using Iterator = typename WTF::HashMap<K, V>::iterator;
     19   using ConstIterator = typename WTF::HashMap<K, V>::const_iterator;
     20 
     21   static bool IsNull(const WTF::HashMap<K, V>& input) {
     22     // WTF::HashMap<> is always converted to non-null mojom map.
     23     return false;
     24   }
     25 
     26   static void SetToNull(WTF::HashMap<K, V>* output) {
     27     // WTF::HashMap<> doesn't support null state. Set it to empty instead.
     28     output->clear();
     29   }
     30 
     31   static size_t GetSize(const WTF::HashMap<K, V>& input) {
     32     return input.size();
     33   }
     34 
     35   static ConstIterator GetBegin(const WTF::HashMap<K, V>& input) {
     36     return input.begin();
     37   }
     38   static Iterator GetBegin(WTF::HashMap<K, V>& input) { return input.begin(); }
     39 
     40   static void AdvanceIterator(ConstIterator& iterator) { ++iterator; }
     41   static void AdvanceIterator(Iterator& iterator) { ++iterator; }
     42 
     43   static const K& GetKey(Iterator& iterator) { return iterator->key; }
     44   static const K& GetKey(ConstIterator& iterator) { return iterator->key; }
     45 
     46   static V& GetValue(Iterator& iterator) { return iterator->value; }
     47   static const V& GetValue(ConstIterator& iterator) { return iterator->value; }
     48 
     49   template <typename IK, typename IV>
     50   static bool Insert(WTF::HashMap<K, V>& input, IK&& key, IV&& value) {
     51     if (!WTF::HashMap<K, V>::IsValidKey(key)) {
     52       LOG(ERROR) << "The key value is disallowed by WTF::HashMap";
     53       return false;
     54     }
     55     input.insert(std::forward<IK>(key), std::forward<IV>(value));
     56     return true;
     57   }
     58 
     59   static void SetToEmpty(WTF::HashMap<K, V>* output) { output->clear(); }
     60 };
     61 
     62 }  // namespace mojo
     63 
     64 #endif  // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_HASH_MAP_H_
     65