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

Side by Side Diff: mojo/public/bindings/lib/bindings.h

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix use of size_t Created 7 years, 2 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 | Annotate | Revision Log
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_BINDINGS_LIB_BINDINGS_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_
7
DaveMoore 2013/10/09 17:14:11 I find the naming of this header confusing, at lea
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 size_t num_bytes = sizeof(Array<T>) + sizeof(StorageType) * num_elements;
82 return new (buf->Allocate(num_bytes)) Array<T>(num_bytes, num_elements);
83 }
84
85 template <typename U>
86 static Array<T>* NewCopyOf(Buffer* buf, const U& u) {
87 Array<T>* result = Array<T>::New(buf, u.size());
88 memcpy(result->storage(), u.data(), u.size() * sizeof(T));
89 return result;
90 }
91
92 size_t size() const { return header_.num_elements; }
93
94 T& at(size_t offset) {
95 return internal::ArrayTraits<T>::ToRef(storage()[offset]);
96 }
97
98 const T& at(size_t offset) const {
99 return internal::ArrayTraits<T>::ToConstRef(storage()[offset]);
100 }
101
102 T& operator[](size_t offset) {
103 return at(offset);
104 }
105
106 const T& operator[](size_t offset) const {
107 return at(offset);
108 }
109
110 template <typename U>
111 U To() const {
112 return U(storage(), storage() + size());
113 }
114
115 private:
116 friend class internal::ObjectTraits<Array<T> >;
117
118 typedef typename internal::ArrayTraits<T>::StorageType StorageType;
119
120 StorageType* storage() {
121 return reinterpret_cast<StorageType*>(this + 1);
122 }
123 const StorageType* storage() const {
124 return reinterpret_cast<const StorageType*>(this + 1);
125 }
126
127 Array(size_t num_bytes, size_t num_elements) {
128 header_.num_bytes = static_cast<uint32_t>(num_bytes);
129 header_.num_elements = static_cast<uint32_t>(num_elements);
130 }
131 ~Array() {}
132
133 internal::ArrayHeader header_;
134
135 // Elements of type internal::ArrayTraits<T>::StorageType follow.
136 };
137
138 } // namespace mojo
139
140 #endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698