OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | |
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | |
7 | |
8 #include <string.h> | |
9 | |
10 #include <algorithm> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "mojo/public/cpp/bindings/lib/array_internal.h" | |
15 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" | |
16 #include "mojo/public/cpp/bindings/lib/template_util.h" | |
17 #include "mojo/public/cpp/bindings/type_converter.h" | |
18 | |
19 namespace mojo { | |
20 | |
21 template <typename T> | |
22 class Array { | |
23 MOJO_MOVE_ONLY_TYPE(Array) | |
24 public: | |
25 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value> Traits; | |
26 typedef typename Traits::ConstRefType ConstRefType; | |
27 typedef typename Traits::RefType RefType; | |
28 typedef typename Traits::StorageType StorageType; | |
29 typedef typename Traits::ForwardType ForwardType; | |
30 | |
31 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType> | |
32 Data_; | |
33 | |
34 Array() : is_null_(true) {} | |
35 explicit Array(size_t size) : vec_(size), is_null_(false) { | |
36 Traits::Initialize(&vec_); | |
37 } | |
38 ~Array() { Traits::Finalize(&vec_); } | |
39 | |
40 Array(Array&& other) : is_null_(true) { Take(&other); } | |
41 Array& operator=(Array&& other) { | |
42 Take(&other); | |
43 return *this; | |
44 } | |
45 | |
46 static Array New(size_t size) { return Array(size).Pass(); } | |
47 | |
48 template <typename U> | |
49 static Array From(const U& other) { | |
50 return TypeConverter<Array, U>::Convert(other); | |
51 } | |
52 | |
53 template <typename U> | |
54 U To() const { | |
55 return TypeConverter<U, Array>::Convert(*this); | |
56 } | |
57 | |
58 void reset() { | |
59 if (!vec_.empty()) { | |
60 Traits::Finalize(&vec_); | |
61 vec_.clear(); | |
62 } | |
63 is_null_ = true; | |
64 } | |
65 | |
66 bool is_null() const { return is_null_; } | |
67 | |
68 ConstRefType front() const { return vec_.front(); } | |
69 RefType front() { return vec_.front(); } | |
70 | |
71 size_t size() const { return vec_.size(); } | |
72 | |
73 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } | |
74 ConstRefType operator[](size_t offset) const { return at(offset); } | |
75 | |
76 RefType at(size_t offset) { return Traits::at(&vec_, offset); } | |
77 RefType operator[](size_t offset) { return at(offset); } | |
78 | |
79 void push_back(ForwardType value) { | |
80 is_null_ = false; | |
81 Traits::PushBack(&vec_, value); | |
82 } | |
83 | |
84 void resize(size_t size) { | |
85 is_null_ = false; | |
86 Traits::Resize(&vec_, size); | |
87 } | |
88 | |
89 const std::vector<StorageType>& storage() const { return vec_; } | |
90 operator const std::vector<StorageType>&() const { return vec_; } | |
91 | |
92 void Swap(Array* other) { | |
93 std::swap(is_null_, other->is_null_); | |
94 vec_.swap(other->vec_); | |
95 } | |
96 void Swap(std::vector<StorageType>* other) { | |
97 is_null_ = false; | |
98 vec_.swap(*other); | |
99 } | |
100 | |
101 // Please note that calling this method will fail compilation if the element | |
102 // type cannot be cloned (which usually means that it is a Mojo handle type or | |
103 // a type contains Mojo handles). | |
104 Array Clone() const { | |
105 Array result; | |
106 result.is_null_ = is_null_; | |
107 Traits::Clone(vec_, &result.vec_); | |
108 return result.Pass(); | |
109 } | |
110 | |
111 bool Equals(const Array& other) const { | |
112 if (is_null() != other.is_null()) | |
113 return false; | |
114 if (size() != other.size()) | |
115 return false; | |
116 for (size_t i = 0; i < size(); ++i) { | |
117 if (!internal::ValueTraits<T>::Equals(at(i), other.at(i))) | |
118 return false; | |
119 } | |
120 return true; | |
121 } | |
122 | |
123 private: | |
124 typedef std::vector<StorageType> Array::*Testable; | |
125 | |
126 public: | |
127 operator Testable() const { return is_null_ ? 0 : &Array::vec_; } | |
128 | |
129 private: | |
130 void Take(Array* other) { | |
131 reset(); | |
132 Swap(other); | |
133 } | |
134 | |
135 std::vector<StorageType> vec_; | |
136 bool is_null_; | |
137 }; | |
138 | |
139 template <typename T, typename E> | |
140 struct TypeConverter<Array<T>, std::vector<E>> { | |
141 static Array<T> Convert(const std::vector<E>& input) { | |
142 Array<T> result(input.size()); | |
143 for (size_t i = 0; i < input.size(); ++i) | |
144 result[i] = TypeConverter<T, E>::Convert(input[i]); | |
145 return result.Pass(); | |
146 } | |
147 }; | |
148 | |
149 template <typename E, typename T> | |
150 struct TypeConverter<std::vector<E>, Array<T>> { | |
151 static std::vector<E> Convert(const Array<T>& input) { | |
152 std::vector<E> result; | |
153 if (!input.is_null()) { | |
154 result.resize(input.size()); | |
155 for (size_t i = 0; i < input.size(); ++i) | |
156 result[i] = TypeConverter<E, T>::Convert(input[i]); | |
157 } | |
158 return result; | |
159 } | |
160 }; | |
161 | |
162 } // namespace mojo | |
163 | |
164 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_ | |
OLD | NEW |