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 <vector> | 8 #include <vector> |
9 | 9 |
10 #include "build/build_config.h" | |
11 #include "mojo/public/cpp/bindings/array_traits.h" | 10 #include "mojo/public/cpp/bindings/array_traits.h" |
12 | 11 |
13 namespace mojo { | 12 namespace mojo { |
14 | 13 |
15 template <typename T> | 14 template <typename T> |
16 struct ArrayTraits<std::vector<T>> { | 15 struct ArrayTraits<std::vector<T>> { |
17 using Element = T; | 16 using Element = T; |
18 | 17 |
19 static bool IsNull(const std::vector<T>& input) { | 18 static bool IsNull(const std::vector<T>& input) { |
20 // std::vector<> is always converted to non-null mojom array. | 19 // std::vector<> is always converted to non-null mojom array. |
(...skipping 15 matching lines...) Expand all Loading... |
36 size_t index) { | 35 size_t index) { |
37 return input[index]; | 36 return input[index]; |
38 } | 37 } |
39 | 38 |
40 static typename std::vector<T>::const_reference GetAt( | 39 static typename std::vector<T>::const_reference GetAt( |
41 const std::vector<T>& input, | 40 const std::vector<T>& input, |
42 size_t index) { | 41 size_t index) { |
43 return input[index]; | 42 return input[index]; |
44 } | 43 } |
45 | 44 |
46 static inline bool Resize(std::vector<T>& input, size_t size) { | 45 static bool Resize(std::vector<T>& input, size_t size) { |
47 #if defined(OS_MACOSX) || defined(OS_ANDROID) | |
48 // This is a hack to make compilers for Mac and Android happy. They | |
49 // currently don't allow resizing types like | |
50 // std::vector<std::vector<MoveOnlyType>>. | |
51 // Because the deserialization code doesn't care about the original contents | |
52 // of |input|, we discard them directly. | |
53 // | |
54 // The "inline" keyword of this method matters. Without it, we have observed | |
55 // significant perf regression with some tests on Mac. crbug.com/631415 | |
56 if (input.size() != size) { | 46 if (input.size() != size) { |
| 47 // This is a hack to make compilers for Mac and Android happy. They |
| 48 // currently don't allow resizing types like |
| 49 // std::vector<std::vector<MoveOnlyType>>. |
| 50 // Because the deserialization code doesn't care about the original |
| 51 // contents of |input|, we discard them directly. |
57 std::vector<T> temp(size); | 52 std::vector<T> temp(size); |
58 input.swap(temp); | 53 input.swap(temp); |
59 } | 54 } |
60 #else | |
61 input.resize(size); | |
62 #endif | |
63 | 55 |
64 return true; | 56 return true; |
65 } | 57 } |
66 }; | 58 }; |
67 | 59 |
68 } // namespace mojo | 60 } // namespace mojo |
69 | 61 |
70 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ | 62 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_TRAITS_STL_H_ |
OLD | NEW |