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.
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
14 #include "mojo/public/cpp/bindings/lib/array_internal.h"
15 #include "mojo/public/cpp/bindings/lib/template_util.h"
16 #include "mojo/public/cpp/bindings/type_converter.h"
20 // Provides read-only access to array data.
23 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(Array
, RValue
)
25 typedef internal::ArrayTraits
<T
, internal::IsMoveOnlyType
<T
>::value
>
27 typedef typename
Traits::ConstRefType ConstRefType
;
28 typedef typename
Traits::RefType RefType
;
29 typedef typename
Traits::StorageType StorageType
;
30 typedef typename
Traits::ForwardType ForwardType
;
32 typedef internal::Array_Data
<typename
internal::WrapperTraits
<T
>::DataType
>
35 Array() : is_null_(true) {}
36 explicit Array(size_t size
) : vec_(size
), is_null_(false) {
37 Traits::Initialize(&vec_
);
39 ~Array() { Traits::Finalize(&vec_
); }
41 Array(RValue other
) : is_null_(true) { Take(other
.object
); }
42 Array
& operator=(RValue other
) {
47 static Array
New(size_t size
) {
48 return Array(size
).Pass();
52 static Array
From(const U
& other
) {
53 return TypeConverter
<Array
, U
>::ConvertFrom(other
);
58 return TypeConverter
<Array
, U
>::ConvertTo(*this);
63 Traits::Finalize(&vec_
);
69 bool is_null() const { return is_null_
; }
71 size_t size() const { return vec_
.size(); }
73 ConstRefType
at(size_t offset
) const { return Traits::at(&vec_
, offset
); }
74 ConstRefType
operator[](size_t offset
) const { return at(offset
); }
76 RefType
at(size_t offset
) { return Traits::at(&vec_
, offset
); }
77 RefType
operator[](size_t offset
) { return at(offset
); }
79 void push_back(ForwardType value
) {
81 Traits::PushBack(&vec_
, value
);
84 void resize(size_t size
) {
86 Traits::Resize(&vec_
, size
);
89 const std::vector
<StorageType
>& storage() const {
92 operator const std::vector
<StorageType
>&() const {
96 void Swap(Array
* other
) {
97 std::swap(is_null_
, other
->is_null_
);
98 vec_
.swap(other
->vec_
);
100 void Swap(std::vector
<StorageType
>* other
) {
106 typedef std::vector
<StorageType
> Array::*Testable
;
109 operator Testable() const { return is_null_
? 0 : &Array::vec_
; }
112 void Take(Array
* other
) {
117 std::vector
<StorageType
> vec_
;
121 template <typename T
, typename E
>
122 class TypeConverter
<Array
<T
>, std::vector
<E
> > {
124 static Array
<T
> ConvertFrom(const std::vector
<E
>& input
) {
125 Array
<T
> result(input
.size());
126 for (size_t i
= 0; i
< input
.size(); ++i
)
127 result
[i
] = TypeConverter
<T
, E
>::ConvertFrom(input
[i
]);
128 return result
.Pass();
130 static std::vector
<E
> ConvertTo(const Array
<T
>& input
) {
131 std::vector
<E
> result
;
132 if (!input
.is_null()) {
133 result
.resize(input
.size());
134 for (size_t i
= 0; i
< input
.size(); ++i
)
135 result
[i
] = TypeConverter
<T
, E
>::ConvertTo(input
[i
]);
143 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_