1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
#define MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
#include "mojo/public/cpp/bindings/lib/array_internal.h"
#include "mojo/public/cpp/bindings/type_converter.h"
namespace mojo {
// Provides read-only access to array data.
template <typename T>
class Array {
public:
typedef internal::ArrayTraits<T, internal::TypeTraits<T>::kIsObject> Traits_;
typedef typename Traits_::DataType Data;
typedef typename Traits_::ConstRef ConstRef;
Array() : data_(NULL) {
}
template <typename U>
Array(const U& u, Buffer* buf = Buffer::current()) {
*this = TypeConverter<Array<T>,U>::ConvertFrom(u, buf);
}
template <typename U>
Array& operator=(const U& u) {
*this = TypeConverter<Array<T>,U>::ConvertFrom(u, Buffer::current());
return *this;
}
template <typename U>
operator U() const {
return To<U>();
}
template <typename U>
U To() const {
return TypeConverter<Array<T>,U>::ConvertTo(*this);
}
bool is_null() const { return !data_; }
size_t size() const { return data_->size(); }
ConstRef at(size_t offset) const {
return Traits_::ToConstRef(data_->at(offset));
}
ConstRef operator[](size_t offset) const { return at(offset); }
// Provides a way to initialize an array element-by-element.
class Builder {
public:
typedef typename Array<T>::Data Data;
typedef typename Array<T>::Traits_ Traits_;
typedef typename Traits_::Ref Ref;
explicit Builder(size_t num_elements, Buffer* buf = mojo::Buffer::current())
: data_(Data::New(num_elements, buf)) {
}
size_t size() const { return data_->size(); }
Ref at(size_t offset) {
return Traits_::ToRef(data_->at(offset));
}
Ref operator[](size_t offset) { return at(offset); }
Array<T> Finish() {
Data* data = NULL;
std::swap(data, data_);
return internal::Wrap(data);
}
private:
Data* data_;
MOJO_DISALLOW_COPY_AND_ASSIGN(Builder);
};
protected:
friend class internal::WrapperHelper<Array<T> >;
struct Wrap {};
Array(Wrap, const Data* data) : data_(data) {}
const Data* data_;
};
// UTF-8 encoded
typedef Array<char> String;
template <>
class TypeConverter<String, std::string> {
public:
static String ConvertFrom(const std::string& input, Buffer* buf);
static std::string ConvertTo(const String& input);
};
template <size_t N>
class TypeConverter<String, char[N]> {
public:
static String ConvertFrom(const char input[N], Buffer* buf) {
String::Builder result(N - 1, buf);
memcpy(&result[0], input, N - 1);
return result.Finish();
}
};
// Appease MSVC.
template <size_t N>
class TypeConverter<String, const char[N]> {
public:
static String ConvertFrom(const char input[N], Buffer* buf) {
return TypeConverter<String, char[N]>::ConvertFrom(input, buf);
}
};
template <>
class TypeConverter<String, const char*> {
public:
static String ConvertFrom(const char* input, Buffer* buf);
// NOTE: |ConvertTo| explicitly not implemented since String is not null
// terminated (and may have embedded null bytes).
};
template <typename T, typename E>
class TypeConverter<Array<T>, std::vector<E> > {
public:
static Array<T> ConvertFrom(const std::vector<E>& input, Buffer* buf) {
typename Array<T>::Builder result(input.size(), buf);
for (size_t i = 0; i < input.size(); ++i)
result[i] = TypeConverter<T, E>::ConvertFrom(input[i], buf);
return result.Finish();
}
static std::vector<E> ConvertTo(const Array<T>& input) {
std::vector<E> result;
if (!input.is_null()) {
result.resize(input.size());
for (size_t i = 0; i < input.size(); ++i)
result[i] = TypeConverter<T, E>::ConvertTo(input[i]);
}
return result;
}
};
} // namespace mojo
#endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_
|