• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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_FLAT_MAP_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_FLAT_MAP_H_
7 
8 #include "base/containers/flat_map.h"
9 #include "mojo/public/cpp/bindings/map_traits.h"
10 
11 namespace mojo {
12 
13 template <typename K, typename V, typename Compare>
14 struct MapTraits<base::flat_map<K, V, Compare>> {
15   using Key = K;
16   using Value = V;
17   using Iterator = typename base::flat_map<K, V, Compare>::iterator;
18   using ConstIterator = typename base::flat_map<K, V, Compare>::const_iterator;
19 
20   static size_t GetSize(const base::flat_map<K, V, Compare>& input) {
21     return input.size();
22   }
23 
24   static ConstIterator GetBegin(const base::flat_map<K, V, Compare>& input) {
25     return input.begin();
26   }
27   static Iterator GetBegin(base::flat_map<K, V, Compare>& input) {
28     return input.begin();
29   }
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->first; }
35   static const K& GetKey(ConstIterator& iterator) { return iterator->first; }
36 
37   static V& GetValue(Iterator& iterator) { return iterator->second; }
38   static const V& GetValue(ConstIterator& iterator) { return iterator->second; }
39 
40   template <typename MaybeConstKeyType, typename MaybeConstValueType>
41   static bool Insert(base::flat_map<K, V, Compare>& input,
42                      MaybeConstKeyType&& key,
43                      MaybeConstValueType&& value) {
44     input.emplace(std::forward<MaybeConstKeyType>(key),
45                   std::forward<MaybeConstValueType>(value));
46     return true;
47   }
48 
49   static void SetToEmpty(base::flat_map<K, V, Compare>* output) {
50     output->clear();
51   }
52 };
53 
54 }  // namespace mojo
55 
56 #endif  // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_FLAT_MAP_H_
57