OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ |
7 | 7 |
| 8 #include <map> |
| 9 #include <set> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "mojo/public/cpp/bindings/array_traits.h" | 12 #include "mojo/public/cpp/bindings/array_traits.h" |
11 | 13 |
12 namespace mojo { | 14 namespace mojo { |
13 | 15 |
14 template <typename T> | 16 template <typename T> |
15 struct ArrayTraits<std::vector<T>> { | 17 struct ArrayTraits<std::vector<T>> { |
16 using Element = T; | 18 using Element = T; |
17 | 19 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 return input.begin(); | 80 return input.begin(); |
79 } | 81 } |
80 static void AdvanceIterator(ConstIterator& iterator) { | 82 static void AdvanceIterator(ConstIterator& iterator) { |
81 ++iterator; | 83 ++iterator; |
82 } | 84 } |
83 static const T& GetValue(ConstIterator& iterator) { | 85 static const T& GetValue(ConstIterator& iterator) { |
84 return *iterator; | 86 return *iterator; |
85 } | 87 } |
86 }; | 88 }; |
87 | 89 |
| 90 template <typename K, typename V> |
| 91 struct MapValuesArrayView { |
| 92 explicit MapValuesArrayView(const std::map<K, V>& map) : map(map) {} |
| 93 const std::map<K, V>& map; |
| 94 }; |
| 95 |
| 96 // Convenience function to create a MapValuesArrayView<> that infers the |
| 97 // template arguments from its argument type. |
| 98 template <typename K, typename V> |
| 99 MapValuesArrayView<K, V> MapValuesToArray(const std::map<K, V>& map) { |
| 100 return MapValuesArrayView<K, V>(map); |
| 101 } |
| 102 |
| 103 // This ArrayTraits specialization is used only for serialization and converts |
| 104 // a map<K, V> into an array<V>, discarding the keys. |
| 105 template <typename K, typename V> |
| 106 struct ArrayTraits<MapValuesArrayView<K, V>> { |
| 107 using Element = V; |
| 108 using ConstIterator = typename std::map<K, V>::const_iterator; |
| 109 |
| 110 static bool IsNull(const MapValuesArrayView<K, V>& input) { |
| 111 // std::map<> is always converted to non-null mojom array. |
| 112 return false; |
| 113 } |
| 114 |
| 115 static size_t GetSize(const MapValuesArrayView<K, V>& input) { |
| 116 return input.map.size(); |
| 117 } |
| 118 static ConstIterator GetBegin(const MapValuesArrayView<K, V>& input) { |
| 119 return input.map.begin(); |
| 120 } |
| 121 static void AdvanceIterator(ConstIterator& iterator) { ++iterator; } |
| 122 static const V& GetValue(ConstIterator& iterator) { return iterator->second; } |
| 123 }; |
| 124 |
88 } // namespace mojo | 125 } // namespace mojo |
89 | 126 |
90 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ | 127 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ |
OLD | NEW |