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

Side by Side Diff: mojo/public/bindings/lib/bindings_internal.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_INTERNAL_H_
6 #define MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_INTERNAL_H_
7
8 #include <string.h>
9
10 #include <vector>
11
12 #include "mojo/public/bindings/lib/bindings.h"
13 #include "mojo/public/bindings/lib/message.h"
14
15 namespace mojo {
16 namespace internal {
17
18 // Pointers are encoded as relative offsets. The offsets are relative to the
19 // address of where the offset value is stored, such that the pointer may be
20 // recovered with the expression:
21 //
22 // ptr = (offset + *offset)
DaveMoore 2013/10/09 17:14:11 So these are array style offsets (numbytes / 8)? A
23 //
24 // A null pointer is encoded as an offset value of 0.
25 //
26 void EncodePointer(const void* ptr, uint64_t* offset);
27 const void* DecodePointerRaw(const uint64_t* offset);
28
29 template <typename T>
30 inline void DecodePointer(const uint64_t* offset, T** ptr) {
31 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset)));
32 }
33
34 // Check that the given pointer references memory contained within the message.
35 bool ValidatePointer(const void* ptr, const Message& message);
36
37 // Handles are encoded as indices into a vector of handles. These functions
38 // manipulate the value of |handle|, mapping it to and from an index.
39 void EncodeHandle(Handle* handle, std::vector<Handle>* handles);
40 bool DecodeHandle(Handle* handle, const std::vector<Handle>& handles);
41
42 // All objects (structs and arrays) can have 3 operations performed on them:
43 // - cloning
44 // - encoding pointers and handles
45 // - decoding pointers and handles
46 //
47 // The following 3 functions are used to select the proper ObjectTraits<>
48 // specialization.
49
50 template <typename T>
51 inline T* Clone(const T* obj, Buffer* buf) {
DaveMoore 2013/10/09 17:14:11 It might be better to make this an error (or not c
52 return obj ? ObjectTraits<T>::Clone(obj, buf) : NULL;
53 }
54
55 template <typename T>
56 inline void EncodePointersAndHandles(T* obj,
57 std::vector<Handle>* handles) {
58 ObjectTraits<T>::EncodePointersAndHandles(obj, handles);
59 }
60
61 template <typename T>
62 inline bool DecodePointersAndHandles(T* obj, const Message& message) {
63 return ObjectTraits<T>::DecodePointersAndHandles(obj, message);
64 }
65
66 // The following 2 functions are used to encode/decode all objects (structs and
67 // arrays) in a consistent manner.
68
69 template <typename T>
70 inline void Encode(T* obj, std::vector<Handle>* handles) {
71 if (obj->ptr)
72 EncodePointersAndHandles(obj->ptr, handles);
73 EncodePointer(obj->ptr, &obj->offset);
74 }
75
76 template <typename T>
77 inline bool Decode(T* obj, const Message& message) {
78 DecodePointer(&obj->offset, &obj->ptr);
79 if (obj->ptr) {
80 if (!ValidatePointer(obj->ptr, message))
81 return false;
82 if (!DecodePointersAndHandles(obj->ptr, message))
83 return false;
84 }
85 return true;
86 }
87
88 // What follows is code to support the ObjectTraits<> specialization of
89 // Array<T>. There are two interesting cases: arrays of primitives and arrays
90 // of objects. Arrays of objects are represented as arrays of pointers to
91 // objects.
92
93 template <typename T>
94 struct ArrayHelper {
95 typedef T ElementType;
96
97 static void CloneElements(Buffer* buf,
98 ArrayHeader* header,
99 ElementType* elements) {
100 }
101
102 static void EncodePointersAndHandles(ArrayHeader* header,
103 ElementType* elements,
104 std::vector<Handle>* handles) {
105 }
106 static bool DecodePointersAndHandles(ArrayHeader* header,
107 ElementType* elements,
108 const Message& message) {
109 return true;
110 }
111 };
112
113 template <>
114 struct ArrayHelper<Handle> {
115 typedef Handle ElementType;
116
117 static void CloneElements(Buffer* buf,
118 ArrayHeader* header,
119 ElementType* elements) {
120 }
121
122 static void EncodePointersAndHandles(ArrayHeader* header,
123 ElementType* elements,
124 std::vector<Handle>* handles);
125 static bool DecodePointersAndHandles(ArrayHeader* header,
126 ElementType* elements,
127 const Message& message);
128 };
129
130 template <typename P>
131 struct ArrayHelper<P*> {
132 typedef StructPointer<P> ElementType;
133
134 static void CloneElements(Buffer* buf,
135 ArrayHeader* header,
136 ElementType* elements) {
137 for (uint32_t i = 0; i < header->num_elements; ++i)
138 elements[i].ptr = Clone(elements[i].ptr, buf);
139 }
140
141 static void EncodePointersAndHandles(ArrayHeader* header,
142 ElementType* elements,
143 std::vector<Handle>* handles) {
144 for (uint32_t i = 0; i < header->num_elements; ++i)
145 Encode(&elements[i], handles);
146 }
147 static bool DecodePointersAndHandles(ArrayHeader* header,
148 ElementType* elements,
149 const Message& message) {
150 for (uint32_t i = 0; i < header->num_elements; ++i) {
151 if (!Decode(&elements[i], message))
152 return false;
153 }
154 return true;
155 }
156 };
157
158 template <typename T>
159 class ObjectTraits<Array<T> > {
160 public:
161 static Array<T>* Clone(const Array<T>* array, Buffer* buf) {
162 Array<T>* clone = Array<T>::New(buf, array->header_.num_elements);
163 memcpy(clone->storage(),
164 array->storage(),
165 array->header_.num_bytes - sizeof(Array<T>));
166
167 ArrayHelper<T>::CloneElements(buf, &clone->header_, clone->storage());
168 return clone;
169 }
170
171 static void EncodePointersAndHandles(Array<T>* array,
172 std::vector<Handle>* handles) {
173 ArrayHelper<T>::EncodePointersAndHandles(&array->header_, array->storage(),
174 handles);
175 }
176
177 static bool DecodePointersAndHandles(Array<T>* array,
178 const Message& message) {
179 return ArrayHelper<T>::DecodePointersAndHandles(&array->header_,
180 array->storage(),
181 message);
182 }
183 };
184
185 } // namespace internal
186 } // namespace mojo
187
188 #endif // MOJO_PUBLIC_BINDINGS_LIB_BINDINGS_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698