Temporarily re-enabling SizeAfterPrefChange test with traces.
[chromium-blink-merge.git] / mojo / public / cpp / bindings / array.h
blobbf971764526a926b29f4c8a32587ef8637bad90f
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_
8 #include <string.h>
10 #include <algorithm>
11 #include <string>
12 #include <vector>
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"
18 namespace mojo {
20 // Provides read-only access to array data.
21 template <typename T>
22 class Array {
23 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(Array, RValue)
24 public:
25 typedef internal::ArrayTraits<T, internal::IsMoveOnlyType<T>::value>
26 Traits;
27 typedef typename Traits::ConstRefType ConstRefType;
28 typedef typename Traits::RefType RefType;
29 typedef typename Traits::StorageType StorageType;
31 typedef internal::Array_Data<typename internal::WrapperTraits<T>::DataType>
32 Data_;
34 Array() : is_null_(true) {}
35 explicit Array(size_t size) : vec_(size), is_null_(false) {
36 Traits::Initialize(&vec_);
38 ~Array() { Traits::Finalize(&vec_); }
40 Array(RValue other) : is_null_(true) { Take(other.object); }
41 Array& operator=(RValue other) {
42 Take(other.object);
43 return *this;
46 static Array New(size_t size) {
47 return Array(size).Pass();
50 template <typename U>
51 static Array From(const U& other) {
52 return TypeConverter<Array, U>::ConvertFrom(other);
55 template <typename U>
56 U To() const {
57 return TypeConverter<Array, U>::ConvertTo(*this);
60 void reset() {
61 if (!vec_.empty()) {
62 Traits::Finalize(&vec_);
63 vec_.clear();
65 is_null_ = true;
68 bool is_null() const { return is_null_; }
70 size_t size() const { return vec_.size(); }
72 ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); }
73 ConstRefType operator[](size_t offset) const { return at(offset); }
75 RefType at(size_t offset) { return Traits::at(&vec_, offset); }
76 RefType operator[](size_t offset) { return at(offset); }
78 const std::vector<StorageType>& storage() const {
79 return vec_;
81 operator const std::vector<StorageType>&() const {
82 return vec_;
85 void Swap(Array* other) {
86 std::swap(is_null_, other->is_null_);
87 vec_.swap(other->vec_);
89 void Swap(std::vector<StorageType>* other) {
90 is_null_ = false;
91 vec_.swap(*other);
94 private:
95 typedef std::vector<StorageType> Array::*Testable;
97 public:
98 operator Testable() const { return is_null_ ? 0 : &Array::vec_; }
100 private:
101 void Take(Array* other) {
102 reset();
103 Swap(other);
106 std::vector<StorageType> vec_;
107 bool is_null_;
110 template <typename T, typename E>
111 class TypeConverter<Array<T>, std::vector<E> > {
112 public:
113 static Array<T> ConvertFrom(const std::vector<E>& input) {
114 Array<T> result(input.size());
115 for (size_t i = 0; i < input.size(); ++i)
116 result[i] = TypeConverter<T, E>::ConvertFrom(input[i]);
117 return result.Pass();
119 static std::vector<E> ConvertTo(const Array<T>& input) {
120 std::vector<E> result;
121 if (!input.is_null()) {
122 result.resize(input.size());
123 for (size_t i = 0; i < input.size(); ++i)
124 result[i] = TypeConverter<T, E>::ConvertTo(input[i]);
126 return result;
130 } // namespace mojo
132 #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_