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

Side by Side Diff: mojo/public/bindings/bindings_internal.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_INTERNAL_H_
6 #define MOJO_PUBLIC_BINDINGS_BINDINGS_INTERNAL_H_
7
8 #include <string.h>
9
10 #include <vector>
11
12 #include "mojo/public/bindings/bindings.h"
13
14 namespace mojo {
15 namespace internal {
16
17 inline void EncodePointer(void* address, uint64_t* offset) {
18 if (!address) {
19 *offset = 0;
20 return;
21 }
22 uint8_t* p_obj = reinterpret_cast<uint8_t*>(address);
23 uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset);
24 assert(p_obj > p_slot);
25 *offset = p_obj - p_slot;
26 }
27
28 template <typename T>
29 inline void DecodePointer(uint64_t* offset, T** ptr) {
30 if (!*offset) {
31 *ptr = NULL;
32 return;
33 }
34 uint8_t* p_slot = reinterpret_cast<uint8_t*>(offset);
35 *ptr = reinterpret_cast<T*>(p_slot + *offset);
36 }
37
38 template <typename T>
39 inline T* Clone(const T* obj, MessageBuffer* buf) {
40 return Traits<T>::Clone(obj, buf);
41 }
42
43 template <typename T>
44 inline void EncodePointersAndHandles(T* obj,
45 std::vector<Handle>* handles) {
46 Traits<T>::EncodePointersAndHandles(obj, handles);
47 }
48
49 template <typename T>
50 inline bool DecodePointersAndHandles(T* obj, const Message& message) {
51 return Traits<T>::DecodePointersAndHandles(obj, message);
52 }
53
54 template <typename T>
55 inline void Encode(T* obj, std::vector<Handle>* handles) {
56 if (obj->ptr)
57 EncodePointersAndHandles(obj->ptr, handles);
58 EncodePointer(obj->ptr, &obj->offset);
59 }
60
61 template <typename T>
62 inline bool Decode(T* obj, const Message& message) {
63 DecodePointer(&obj->offset, &obj->ptr);
64 if (obj->ptr) {
65 if (!DecodePointersAndHandles(obj->ptr, message))
66 return false;
67 }
68 // TODO: Validate!
69 return true;
70 }
71
72 template <typename T>
73 struct ArrayHelper {
74 typedef T ElementType;
75
76 static void CloneElements(MessageBuffer* buf,
77 ArrayHeader* header,
78 ElementType* elements) {
79 }
80
81 static void EncodePointersAndHandles(ArrayHeader* header,
82 ElementType* elements,
83 std::vector<Handle>* handles) {
84 }
85 static bool DecodePointersAndHandles(ArrayHeader* header,
86 ElementType* elements,
87 const Message& message) {
88 return true;
89 }
90 };
91
92 template <typename P>
93 struct ArrayHelper<P*> {
94 typedef StructPointer<P> ElementType;
95
96 static void CloneElements(MessageBuffer* buf,
97 ArrayHeader* header,
98 ElementType* elements) {
99 for (uint32_t i = 0; i < header->num_elements; ++i) {
100 if (elements[i].ptr)
101 elements[i].ptr = Clone(elements[i].ptr, buf);
102 }
103 }
104
105 static void EncodePointersAndHandles(ArrayHeader* header,
106 ElementType* elements,
107 std::vector<Handle>* handles) {
108 for (uint32_t i = 0; i < header->num_elements; ++i)
109 Encode(&elements[i], handles);
110 }
111 static bool DecodePointersAndHandles(ArrayHeader* header,
112 ElementType* elements,
113 const Message& message) {
114 for (uint32_t i = 0; i < header->num_elements; ++i) {
115 if (!Decode(&elements[i], message))
116 return false;
117 }
118 return true;
119 }
120 };
121
122 template <typename T>
123 class Traits<Array<T> > {
124 public:
125 static Array<T>* Clone(const Array<T>* array, MessageBuffer* buf) {
126 Array<T>* clone = buf->AllocArray<T>(array->header_.num_elements);
127 memcpy(clone, array, array->header_.num_bytes);
128
129 ArrayHelper<T>::CloneElements(buf, &clone->header_, clone->elements_);
130 return clone;
131 }
132
133 static void EncodePointersAndHandles(Array<T>* array,
134 std::vector<Handle>* handles) {
135 ArrayHelper<T>::EncodePointersAndHandles(&array->header_, array->elements_,
136 handles);
137 }
138
139 static bool DecodePointersAndHandles(Array<T>* array,
140 const Message& message) {
141 return ArrayHelper<T>::DecodePointersAndHandles(&array->header_,
142 array->elements_,
143 message);
144 }
145 };
146
147 } // namespace internal
148 } // namespace mojo
149
150 #endif // MOJO_PUBLIC_BINDINGS_BINDINGS_INTERNAL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698