summaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/array_traits_stl.h
blob: dec47bfe7df9343cfc83778822b3c28ef50c59e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_
#define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_

#include <map>
#include <set>
#include <vector>

#include "mojo/public/cpp/bindings/array_traits.h"

namespace mojo {

template <typename T>
struct ArrayTraits<std::vector<T>> {
  using Element = T;

  static bool IsNull(const std::vector<T>& input) {
    // std::vector<> is always converted to non-null mojom array.
    return false;
  }

  static void SetToNull(std::vector<T>* output) {
    // std::vector<> doesn't support null state. Set it to empty instead.
    output->clear();
  }

  static size_t GetSize(const std::vector<T>& input) { return input.size(); }

  static T* GetData(std::vector<T>& input) { return input.data(); }

  static const T* GetData(const std::vector<T>& input) { return input.data(); }

  static typename std::vector<T>::reference GetAt(std::vector<T>& input,
                                                  size_t index) {
    return input[index];
  }

  static typename std::vector<T>::const_reference GetAt(
      const std::vector<T>& input,
      size_t index) {
    return input[index];
  }

  static inline bool Resize(std::vector<T>& input, size_t size) {
    // Instead of calling std::vector<T>::resize() directly, this is a hack to
    // make compilers happy. Some compilers (e.g., Mac, Android, Linux MSan)
    // currently don't allow resizing types like
    // std::vector<std::vector<MoveOnlyType>>.
    // Because the deserialization code doesn't care about the original contents
    // of |input|, we discard them directly.
    //
    // The "inline" keyword of this method matters. Without it, we have observed
    // significant perf regression with some tests on Mac. crbug.com/631415
    if (input.size() != size) {
      std::vector<T> temp(size);
      input.swap(temp);
    }

    return true;
  }
};

// This ArrayTraits specialization is used only for serialization.
template <typename T>
struct ArrayTraits<std::set<T>> {
  using Element = T;
  using ConstIterator = typename std::set<T>::const_iterator;

  static bool IsNull(const std::set<T>& input) {
    // std::set<> is always converted to non-null mojom array.
    return false;
  }

  static size_t GetSize(const std::set<T>& input) { return input.size(); }

  static ConstIterator GetBegin(const std::set<T>& input) {
    return input.begin();
  }
  static void AdvanceIterator(ConstIterator& iterator) {
    ++iterator;
  }
  static const T& GetValue(ConstIterator& iterator) {
    return *iterator;
  }
};

template <typename K, typename V>
struct MapValuesArrayView {
  explicit MapValuesArrayView(const std::map<K, V>& map) : map(map) {}
  const std::map<K, V>& map;
};

// Convenience function to create a MapValuesArrayView<> that infers the
// template arguments from its argument type.
template <typename K, typename V>
MapValuesArrayView<K, V> MapValuesToArray(const std::map<K, V>& map) {
  return MapValuesArrayView<K, V>(map);
}

// This ArrayTraits specialization is used only for serialization and converts
// a map<K, V> into an array<V>, discarding the keys.
template <typename K, typename V>
struct ArrayTraits<MapValuesArrayView<K, V>> {
  using Element = V;
  using ConstIterator = typename std::map<K, V>::const_iterator;

  static bool IsNull(const MapValuesArrayView<K, V>& input) {
    // std::map<> is always converted to non-null mojom array.
    return false;
  }

  static size_t GetSize(const MapValuesArrayView<K, V>& input) {
    return input.map.size();
  }
  static ConstIterator GetBegin(const MapValuesArrayView<K, V>& input) {
    return input.map.begin();
  }
  static void AdvanceIterator(ConstIterator& iterator) { ++iterator; }
  static const V& GetValue(ConstIterator& iterator) { return iterator->second; }
};

}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_