Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_BINDINGS_LIB_BINDINGS_H_ | |
| 6 #define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 #include <string.h> | |
| 10 | |
| 11 #include <new> | |
| 12 | |
| 13 #include "mojo/public/bindings/lib/bindings_internal.h" | |
| 14 #include "mojo/public/bindings/lib/buffer.h" | |
| 15 #include "mojo/public/system/core.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 template <typename T> | |
| 20 class Array { | |
| 21 public: | |
| 22 static Array<T>* New(Buffer* buf, size_t num_elements) { | |
| 23 size_t num_bytes = sizeof(Array<T>) + sizeof(StorageType) * num_elements; | |
|
dmichael (off chromium)
2013/10/14 22:41:19
Do you want to require here that num_bytes is a mu
| |
| 24 return new (buf->Allocate(num_bytes)) Array<T>(num_bytes, num_elements); | |
| 25 } | |
| 26 | |
| 27 template <typename U> | |
| 28 static Array<T>* NewCopyOf(Buffer* buf, const U& u) { | |
| 29 Array<T>* result = Array<T>::New(buf, u.size()); | |
| 30 memcpy(result->storage(), u.data(), u.size() * sizeof(T)); | |
| 31 return result; | |
| 32 } | |
| 33 | |
| 34 size_t size() const { return header_.num_elements; } | |
| 35 | |
| 36 T& at(size_t offset) { | |
| 37 return internal::ArrayTraits<T>::ToRef(storage()[offset]); | |
| 38 } | |
| 39 | |
| 40 const T& at(size_t offset) const { | |
| 41 return internal::ArrayTraits<T>::ToConstRef(storage()[offset]); | |
| 42 } | |
| 43 | |
| 44 T& operator[](size_t offset) { | |
| 45 return at(offset); | |
| 46 } | |
| 47 | |
| 48 const T& operator[](size_t offset) const { | |
| 49 return at(offset); | |
| 50 } | |
| 51 | |
| 52 template <typename U> | |
| 53 U To() const { | |
| 54 return U(storage(), storage() + size()); | |
| 55 } | |
| 56 | |
| 57 private: | |
| 58 friend class internal::ObjectTraits<Array<T> >; | |
| 59 | |
| 60 typedef typename internal::ArrayTraits<T>::StorageType StorageType; | |
| 61 | |
| 62 StorageType* storage() { | |
| 63 return reinterpret_cast<StorageType*>(this + 1); | |
|
dmichael (off chromium)
2013/10/14 22:41:19
Maybe you've already thought about this, but my un
| |
| 64 } | |
| 65 const StorageType* storage() const { | |
| 66 return reinterpret_cast<const StorageType*>(this + 1); | |
| 67 } | |
| 68 | |
| 69 Array(size_t num_bytes, size_t num_elements) { | |
| 70 header_.num_bytes = static_cast<uint32_t>(num_bytes); | |
| 71 header_.num_elements = static_cast<uint32_t>(num_elements); | |
| 72 } | |
| 73 ~Array() {} | |
| 74 | |
| 75 internal::ArrayHeader header_; | |
| 76 | |
| 77 // Elements of type internal::ArrayTraits<T>::StorageType follow. | |
| 78 }; | |
| 79 | |
| 80 // UTF-8 encoded | |
| 81 typedef Array<char> String; | |
| 82 | |
| 83 } // namespace mojo | |
| 84 | |
| 85 #endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_ | |
| OLD | NEW |