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

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

Issue 23913008: C++ bindings (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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_BINDINGS_H_
6 #define MOJO_PUBLIC_BINDINGS_BINDINGS_H_
7
8 #include <assert.h>
9 #include <stddef.h>
10 #include <stdint.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #include <new>
15 #include <vector>
16
17 #include "mojo/public/system/core.h"
18
19 namespace mojo {
20
21 template <typename T> class Array;
22 class String;
23
24 namespace internal {
25
26 struct MessageHeader {
27 uint32_t num_bytes;
28 uint32_t name;
29 };
30
31 struct StructHeader {
32 uint32_t num_bytes;
33 uint32_t num_fields;
34 };
35
36 struct ArrayHeader {
37 uint32_t num_bytes;
38 uint32_t num_elements;
39 };
40
41 template <typename T>
42 union StructPointer {
43 uint64_t offset;
44 T* ptr;
45 };
46
47 template <typename T>
48 union ArrayPointer {
49 uint64_t offset;
50 Array<T>* ptr;
51 };
52
53 union StringPointer {
54 uint64_t offset;
55 String* ptr;
56 };
57
58 template <typename T>
59 struct ArrayTraits {
60 typedef T StorageType;
61
62 static T& ToRef(StorageType& e) { return e; }
63 static T const& ToConstRef(const StorageType& e) { return e; }
64 };
65
66 template <typename P>
67 struct ArrayTraits<P*> {
68 typedef StructPointer<P> StorageType;
69
70 static P*& ToRef(StorageType& e) { return e.ptr; }
71 static P* const& ToConstRef(const StorageType& e) { return e.ptr; }
72 };
73
74 template <typename T> class ObjectTraits {};
75
76 } // namespace internal
77
78 //-----------------------------------------------------------------------------
79
80 template <typename T>
81 class Array {
82 public:
83 explicit Array(size_t num_elements) {
84 // TODO: bools should get packed to a single bit?
85 header_.num_bytes =
86 sizeof(*this) +
87 sizeof(typename internal::ArrayTraits<T>::StorageType) *
88 (num_elements - 1);
89 header_.num_elements = num_elements;
90 }
91
92 size_t size() const { return header_.num_elements; }
93
94 T* data() { return elements_; }
95 const T* data() const { return elements_; }
96
97 T& at(size_t offset) {
98 return internal::ArrayTraits<T>::ToRef(elements_[offset]);
99 }
100
101 const T& at(size_t offset) const {
102 return internal::ArrayTraits<T>::ToConstRef(elements_[offset]);
103 }
104
105 T& operator[](size_t offset) {
106 return at(offset);
107 }
108
109 const T& operator[](size_t offset) const {
110 return at(offset);
111 }
112
113 private:
114 friend class internal::ObjectTraits<Array<T> >;
115 friend class internal::ObjectTraits<String>;
116
117 internal::ArrayHeader header_;
118
119 // Extra elements follow.
120 typename internal::ArrayTraits<T>::StorageType elements_[1];
121 };
122
123 //-----------------------------------------------------------------------------
124
125 // UTF-8 encoded
126 class String : public Array<char> {
127 public:
128 explicit String(size_t num_chars)
129 : Array<char>(num_chars) {
130 }
131
132 char& operator[](size_t offset) {
133 return at(offset);
134 }
135
136 const char& operator[](size_t offset) const {
137 return at(offset);
138 }
139 };
140
141 //-----------------------------------------------------------------------------
142
143 struct Message {
144 uint32_t name;
145 uint8_t* data_start;
146 uint8_t* data_end;
147 //XXX MessageBuffer data;
148 std::vector<Handle> handles;
149 };
150
151 class MessageSender {
152 public:
153 virtual void Send(const Message& message) = 0;
154 };
155
156 //-----------------------------------------------------------------------------
157
158 // The following is a cheezy arena allocator.
159 class MessageBuffer {
160 public:
161 MessageBuffer() : ptr_(NULL), size_(0) {
162 }
163 ~MessageBuffer() { free(ptr_); }
164
165 const uint8_t* data() const { return ptr_; }
166 uint8_t* data() { return ptr_; }
167
168 size_t size() const { return size_; }
169
170 template <typename T>
171 T* New() {
172 size_t size = sizeof(T);
173 return new (Grow(size)) T();
174 }
175
176 template <typename T>
177 Array<T>* NewArray(size_t num_elements) {
178 // (count - 1) because Array<T> has reserved space for the first element.
179 size_t size = sizeof(Array<T>) + sizeof(T) * (num_elements - 1);
180 return new (Grow(size)) Array<T>(num_elements);
181 }
182
183 String* NewString(size_t num_chars) {
184 // (num_chars - 1) because String has reserved space for the first element.
185 size_t size = sizeof(String) + (num_chars - 1);
186 return new (Grow(size)) String(num_chars);
187 }
188
189 private:
190 uint8_t* Grow(size_t delta) {
191 // TODO: Align allocations
192 size_t old_size = size_;
193 size_t new_size = old_size + delta;
194 ptr_ = static_cast<uint8_t*>(realloc(ptr_, old_size + delta));
195 size_ = new_size;
196 uint8_t* result = ptr_ + old_size;
197 memset(result, 0, delta);
198 return result;
199 }
200
201 uint8_t* ptr_;
202 size_t size_;
203
204 // NOT IMPLEMENTED
205 MessageBuffer(const MessageBuffer&);
206 void operator=(const MessageBuffer&);
207 };
208
209 } // namespace mojo
210
211 #endif // MOJO_PUBLIC_BINDINGS_BINDINGS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698