OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_H_ | 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
7 | 7 |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 Take(other.object); | 43 Take(other.object); |
44 return *this; | 44 return *this; |
45 } | 45 } |
46 | 46 |
47 static Array New(size_t size) { | 47 static Array New(size_t size) { |
48 return Array(size).Pass(); | 48 return Array(size).Pass(); |
49 } | 49 } |
50 | 50 |
51 template <typename U> | 51 template <typename U> |
52 static Array From(const U& other) { | 52 static Array From(const U& other) { |
53 return TypeConverter<Array, U>::ConvertFrom(other); | 53 return TypeConverter<Array, U>::Convert(other); |
54 } | 54 } |
55 | 55 |
56 template <typename U> | 56 template <typename U> |
57 U To() const { | 57 U To() const { |
58 return TypeConverter<Array, U>::ConvertTo(*this); | 58 return TypeConverter<U, Array>::Convert(*this); |
59 } | 59 } |
60 | 60 |
61 void reset() { | 61 void reset() { |
62 if (!vec_.empty()) { | 62 if (!vec_.empty()) { |
63 Traits::Finalize(&vec_); | 63 Traits::Finalize(&vec_); |
64 vec_.clear(); | 64 vec_.clear(); |
65 } | 65 } |
66 is_null_ = true; | 66 is_null_ = true; |
67 } | 67 } |
68 | 68 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 void Take(Array* other) { | 112 void Take(Array* other) { |
113 reset(); | 113 reset(); |
114 Swap(other); | 114 Swap(other); |
115 } | 115 } |
116 | 116 |
117 std::vector<StorageType> vec_; | 117 std::vector<StorageType> vec_; |
118 bool is_null_; | 118 bool is_null_; |
119 }; | 119 }; |
120 | 120 |
121 template <typename T, typename E> | 121 template <typename T, typename E> |
122 class TypeConverter<Array<T>, std::vector<E> > { | 122 struct TypeConverter<Array<T>, std::vector<E> > { |
123 public: | 123 static Array<T> Convert(const std::vector<E>& input) { |
124 static Array<T> ConvertFrom(const std::vector<E>& input) { | |
125 Array<T> result(input.size()); | 124 Array<T> result(input.size()); |
126 for (size_t i = 0; i < input.size(); ++i) | 125 for (size_t i = 0; i < input.size(); ++i) |
127 result[i] = TypeConverter<T, E>::ConvertFrom(input[i]); | 126 result[i] = TypeConverter<T, E>::Convert(input[i]); |
128 return result.Pass(); | 127 return result.Pass(); |
129 } | 128 } |
130 static std::vector<E> ConvertTo(const Array<T>& input) { | 129 }; |
| 130 |
| 131 template <typename E, typename T> |
| 132 struct TypeConverter<std::vector<E>, Array<T> > { |
| 133 static std::vector<E> Convert(const Array<T>& input) { |
131 std::vector<E> result; | 134 std::vector<E> result; |
132 if (!input.is_null()) { | 135 if (!input.is_null()) { |
133 result.resize(input.size()); | 136 result.resize(input.size()); |
134 for (size_t i = 0; i < input.size(); ++i) | 137 for (size_t i = 0; i < input.size(); ++i) |
135 result[i] = TypeConverter<T, E>::ConvertTo(input[i]); | 138 result[i] = TypeConverter<E, T>::Convert(input[i]); |
136 } | 139 } |
137 return result; | 140 return result; |
138 } | 141 } |
139 }; | 142 }; |
140 | 143 |
141 } // namespace mojo | 144 } // namespace mojo |
142 | 145 |
143 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | 146 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ |
OLD | NEW |