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/type_converter.h"
19 // Provides read-only access to array data.
23 typedef internal::ArrayTraits
<T
,
24 internal::TypeTraits
<T
>::kIsObject
,
25 internal::TypeTraits
<T
>::kIsHandle
> Traits_
;
26 typedef typename
Traits_::DataType Data
;
27 typedef typename
Traits_::ConstRef ConstRef
;
29 Array() : data_(NULL
) {
33 Array(const U
& u
, Buffer
* buf
= Buffer::current()) {
34 TypeConverter
<Array
<T
>,U
>::AssertAllowImplicitTypeConversion();
35 *this = TypeConverter
<Array
<T
>,U
>::ConvertFrom(u
, buf
);
39 Array
& operator=(const U
& u
) {
40 TypeConverter
<Array
<T
>,U
>::AssertAllowImplicitTypeConversion();
41 *this = TypeConverter
<Array
<T
>,U
>::ConvertFrom(u
, Buffer::current());
47 TypeConverter
<Array
<T
>,U
>::AssertAllowImplicitTypeConversion();
53 return TypeConverter
<Array
<T
>,U
>::ConvertTo(*this);
57 static Array
From(const U
& u
, Buffer
* buf
= Buffer::current()) {
58 return TypeConverter
<Array
<T
>,U
>::ConvertFrom(u
, buf
);
61 bool is_null() const { return !data_
; }
63 size_t size() const { return data_
->size(); }
65 ConstRef
at(size_t offset
) const {
66 return Traits_::ToConstRef(data_
->at(offset
));
68 ConstRef
operator[](size_t offset
) const { return at(offset
); }
70 // Provides a way to initialize an array element-by-element.
73 typedef typename Array
<T
>::Data Data
;
74 typedef typename Array
<T
>::Traits_ Traits_
;
75 typedef typename
Traits_::Ref Ref
;
77 explicit Builder(size_t num_elements
, Buffer
* buf
= mojo::Buffer::current())
78 : data_(Data::New(num_elements
, buf
, Traits_::GetDestructor())) {
81 size_t size() const { return data_
->size(); }
83 Ref
at(size_t offset
) {
84 return Traits_::ToRef(data_
->at(offset
));
86 Ref
operator[](size_t offset
) { return at(offset
); }
90 std::swap(data
, data_
);
91 return internal::Wrap(data
);
96 MOJO_DISALLOW_COPY_AND_ASSIGN(Builder
);
100 friend class internal::WrapperHelper
<Array
<T
> >;
103 Array(Wrap
, const Data
* data
) : data_(data
) {}
109 typedef Array
<char> String
;
112 class TypeConverter
<String
, std::string
> {
114 static String
ConvertFrom(const std::string
& input
, Buffer
* buf
);
115 static std::string
ConvertTo(const String
& input
);
117 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION();
121 class TypeConverter
<String
, char[N
]> {
123 static String
ConvertFrom(const char input
[N
], Buffer
* buf
) {
124 String::Builder
result(N
- 1, buf
);
125 memcpy(&result
[0], input
, N
- 1);
126 return result
.Finish();
129 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION();
134 class TypeConverter
<String
, const char[N
]> {
136 static String
ConvertFrom(const char input
[N
], Buffer
* buf
) {
137 return TypeConverter
<String
, char[N
]>::ConvertFrom(input
, buf
);
140 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION();
144 class TypeConverter
<String
, const char*> {
146 static String
ConvertFrom(const char* input
, Buffer
* buf
);
147 // NOTE: |ConvertTo| explicitly not implemented since String is not null
148 // terminated (and may have embedded null bytes).
150 MOJO_ALLOW_IMPLICIT_TYPE_CONVERSION();
153 template <typename T
, typename E
>
154 class TypeConverter
<Array
<T
>, std::vector
<E
> > {
156 static Array
<T
> ConvertFrom(const std::vector
<E
>& input
, Buffer
* buf
) {
157 typename Array
<T
>::Builder
result(input
.size(), buf
);
158 for (size_t i
= 0; i
< input
.size(); ++i
)
159 result
[i
] = TypeConverter
<T
, E
>::ConvertFrom(input
[i
], buf
);
160 return result
.Finish();
162 static std::vector
<E
> ConvertTo(const Array
<T
>& input
) {
163 std::vector
<E
> result
;
164 if (!input
.is_null()) {
165 result
.resize(input
.size());
166 for (size_t i
= 0; i
< input
.size(); ++i
)
167 result
[i
] = TypeConverter
<T
, E
>::ConvertTo(input
[i
]);
172 MOJO_INHERIT_IMPLICIT_TYPE_CONVERSION(T
, E
);
177 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_