// 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 #include #include #include #include "mojo/public/cpp/bindings/lib/array_internal.h" #include "mojo/public/cpp/bindings/lib/template_util.h" #include "mojo/public/cpp/bindings/type_converter.h" namespace mojo { // Provides read-only access to array data. template class Array { MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(Array, RValue) public: typedef internal::ArrayTraits::value> Traits; typedef typename Traits::ConstRefType ConstRefType; typedef typename Traits::RefType RefType; typedef typename Traits::StorageType StorageType; typedef typename Traits::ForwardType ForwardType; typedef internal::Array_Data::DataType> Data_; Array() : is_null_(true) {} explicit Array(size_t size) : vec_(size), is_null_(false) { Traits::Initialize(&vec_); } ~Array() { Traits::Finalize(&vec_); } Array(RValue other) : is_null_(true) { Take(other.object); } Array& operator=(RValue other) { Take(other.object); return *this; } static Array New(size_t size) { return Array(size).Pass(); } template static Array From(const U& other) { return TypeConverter::ConvertFrom(other); } template U To() const { return TypeConverter::ConvertTo(*this); } void reset() { if (!vec_.empty()) { Traits::Finalize(&vec_); vec_.clear(); } is_null_ = true; } bool is_null() const { return is_null_; } size_t size() const { return vec_.size(); } ConstRefType at(size_t offset) const { return Traits::at(&vec_, offset); } ConstRefType operator[](size_t offset) const { return at(offset); } RefType at(size_t offset) { return Traits::at(&vec_, offset); } RefType operator[](size_t offset) { return at(offset); } void push_back(ForwardType value) { is_null_ = false; Traits::PushBack(&vec_, value); } void resize(size_t size) { is_null_ = false; Traits::Resize(&vec_, size); } const std::vector& storage() const { return vec_; } operator const std::vector&() const { return vec_; } void Swap(Array* other) { std::swap(is_null_, other->is_null_); vec_.swap(other->vec_); } void Swap(std::vector* other) { is_null_ = false; vec_.swap(*other); } private: typedef std::vector Array::*Testable; public: operator Testable() const { return is_null_ ? 0 : &Array::vec_; } private: void Take(Array* other) { reset(); Swap(other); } std::vector vec_; bool is_null_; }; template class TypeConverter, std::vector > { public: static Array ConvertFrom(const std::vector& input) { Array result(input.size()); for (size_t i = 0; i < input.size(); ++i) result[i] = TypeConverter::ConvertFrom(input[i]); return result.Pass(); } static std::vector ConvertTo(const Array& input) { std::vector result; if (!input.is_null()) { result.resize(input.size()); for (size_t i = 0; i < input.size(); ++i) result[i] = TypeConverter::ConvertTo(input[i]); } return result; } }; } // namespace mojo #endif // MOJO_PUBLIC_CPP_BINDINGS_ARRAY_H_