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 <stdint.h> | |
| 10 #include <string.h> | |
| 11 | |
| 12 #include <new> | |
| 13 | |
| 14 #include "mojo/public/bindings/lib/buffer.h" | |
| 15 #include "mojo/public/system/core.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 //----------------------------------------------------------------------------- | |
| 20 | |
| 21 template <typename T> class Array; | |
| 22 | |
| 23 // UTF-8 encoded | |
| 24 typedef Array<char> String; | |
| 25 | |
| 26 namespace internal { | |
| 27 | |
| 28 struct StructHeader { | |
| 29 uint32_t num_bytes; | |
| 30 uint32_t num_fields; | |
| 31 }; | |
| 32 | |
| 33 struct ArrayHeader { | |
| 34 uint32_t num_bytes; | |
| 35 uint32_t num_elements; | |
| 36 }; | |
| 37 | |
| 38 template <typename T> | |
| 39 union StructPointer { | |
| 40 uint64_t offset; | |
| 41 T* ptr; | |
| 42 }; | |
| 43 | |
| 44 template <typename T> | |
| 45 union ArrayPointer { | |
| 46 uint64_t offset; | |
| 47 Array<T>* ptr; | |
| 48 }; | |
| 49 | |
| 50 union StringPointer { | |
| 51 uint64_t offset; | |
| 52 String* ptr; | |
| 53 }; | |
| 54 | |
| 55 template <typename T> | |
| 56 struct ArrayTraits { | |
| 57 typedef T StorageType; | |
| 58 | |
| 59 static T& ToRef(StorageType& e) { return e; } | |
| 60 static T const& ToConstRef(const StorageType& e) { return e; } | |
| 61 }; | |
| 62 | |
| 63 template <typename P> | |
| 64 struct ArrayTraits<P*> { | |
| 65 typedef StructPointer<P> StorageType; | |
| 66 | |
| 67 static P*& ToRef(StorageType& e) { return e.ptr; } | |
| 68 static P* const& ToConstRef(const StorageType& e) { return e.ptr; } | |
| 69 }; | |
| 70 | |
| 71 template <typename T> class ObjectTraits {}; | |
| 72 | |
| 73 } // namespace internal | |
| 74 | |
| 75 //----------------------------------------------------------------------------- | |
| 76 | |
| 77 template <typename T> | |
| 78 class Array { | |
| 79 public: | |
| 80 static Array<T>* New(Buffer* buf, size_t num_elements) { | |
| 81 // The first array element is included in sizeof(*this). | |
| 82 size_t num_bytes = | |
| 83 sizeof(Array<T>) + | |
| 84 sizeof(typename internal::ArrayTraits<T>::StorageType) * | |
| 85 (num_elements - 1); | |
|
viettrungluu
2013/10/09 03:44:06
Can num_elements be 0?
| |
| 86 return new (buf->Allocate(num_bytes)) Array<T>(num_bytes, num_elements); | |
| 87 } | |
| 88 | |
| 89 template <typename U> | |
| 90 static Array<T>* NewCopyOf(Buffer* buf, const U& u) { | |
| 91 Array<T>* result = Array<T>::New(buf, u.size()); | |
| 92 memcpy(result->data(), u.data(), u.size() * sizeof(T)); | |
| 93 return result; | |
| 94 } | |
| 95 | |
| 96 size_t size() const { return header_.num_elements; } | |
| 97 | |
| 98 T* data() { return elements_; } | |
| 99 const T* data() const { return elements_; } | |
| 100 | |
| 101 T& at(size_t offset) { | |
| 102 return internal::ArrayTraits<T>::ToRef(elements_[offset]); | |
| 103 } | |
| 104 | |
| 105 const T& at(size_t offset) const { | |
| 106 return internal::ArrayTraits<T>::ToConstRef(elements_[offset]); | |
| 107 } | |
| 108 | |
| 109 T& operator[](size_t offset) { | |
| 110 return at(offset); | |
| 111 } | |
| 112 | |
| 113 const T& operator[](size_t offset) const { | |
| 114 return at(offset); | |
| 115 } | |
| 116 | |
| 117 template <typename U> | |
| 118 U To() const { | |
| 119 return U(data(), data() + size()); | |
| 120 } | |
| 121 | |
| 122 protected: | |
| 123 Array(size_t num_bytes, size_t num_elements) { | |
| 124 header_.num_bytes = static_cast<uint32_t>(num_bytes); | |
| 125 header_.num_elements = static_cast<uint32_t>(num_elements); | |
| 126 } | |
| 127 ~Array() { | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 friend class internal::ObjectTraits<Array<T> >; | |
| 132 | |
| 133 internal::ArrayHeader header_; | |
| 134 | |
| 135 // Extra elements follow. | |
| 136 // TODO: bools should get packed to a single bit? | |
| 137 typename internal::ArrayTraits<T>::StorageType elements_[1]; | |
| 138 }; | |
| 139 | |
| 140 } // namespace mojo | |
| 141 | |
| 142 #endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_ | |
| OLD | NEW |