Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(525)

Side by Side Diff: mojo/public/cpp/bindings/array.h

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/public/cpp/bindings/DEPS ('k') | mojo/public/cpp/bindings/binding.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/DEPS ('k') | mojo/public/cpp/bindings/binding.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698