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

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

Issue 2112093002: Mojo C++ bindings: Merge EncodePointers/DecodePointers into Serialize/Deserialize, respectively. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@61_array_fix
Patch Set: . Created 4 years, 5 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <stdint.h> 9 #include <stdint.h>
10 10
11 #include <queue> 11 #include <queue>
12 12
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "mojo/public/cpp/bindings/associated_interface_ptr_info.h"
16 #include "mojo/public/cpp/bindings/interface_ptr.h"
17 #include "mojo/public/cpp/bindings/lib/bindings_internal.h" 15 #include "mojo/public/cpp/bindings/lib/bindings_internal.h"
18 #include "mojo/public/cpp/bindings/lib/serialization_context.h" 16 #include "mojo/public/cpp/bindings/lib/serialization_context.h"
19 #include "mojo/public/cpp/system/handle.h"
20 17
21 namespace mojo { 18 namespace mojo {
22
23 class AssociatedGroupController;
24
25 namespace internal { 19 namespace internal {
26 20
27 size_t Align(size_t size);
28 char* AlignPointer(char* ptr);
29
30 bool IsAligned(const void* ptr);
31
32 // Pointers are encoded as relative offsets. The offsets are relative to the
33 // address of where the offset value is stored, such that the pointer may be
34 // recovered with the expression:
35 //
36 // ptr = reinterpret_cast<char*>(offset) + *offset
37 //
38 // A null pointer is encoded as an offset value of 0.
39 //
40 void EncodePointer(const void* ptr, uint64_t* offset);
41 // Note: This function doesn't validate the encoded pointer value.
42 const void* DecodePointerRaw(const uint64_t* offset);
43
44 // Note: This function doesn't validate the encoded pointer value.
45 template <typename T>
46 inline void DecodePointer(const uint64_t* offset, T** ptr) {
47 *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset)));
48 }
49
50 // The following 2 functions are used to encode/decode all objects (structs and
51 // arrays) in a consistent manner.
52
53 template <typename T>
54 inline void Encode(T* obj) {
55 if (obj->ptr)
56 obj->ptr->EncodePointers();
57 EncodePointer(obj->ptr, &obj->offset);
58 }
59
60 // Note: This function doesn't validate the encoded pointer and handle values.
61 template <typename T>
62 inline void Decode(T* obj) {
63 DecodePointer(&obj->offset, &obj->ptr);
64 if (obj->ptr)
65 obj->ptr->DecodePointers();
66 }
67
68 template <typename T>
69 inline void AssociatedInterfacePtrInfoToData(
70 AssociatedInterfacePtrInfo<T> input,
71 AssociatedInterface_Data* output) {
72 output->version = input.version();
73 output->interface_id = input.PassHandle().release();
74 }
75
76 template <typename T>
77 inline void AssociatedInterfaceDataToPtrInfo(
78 AssociatedInterface_Data* input,
79 AssociatedInterfacePtrInfo<T>* output,
80 AssociatedGroupController* group_controller) {
81 output->set_handle(group_controller->CreateLocalEndpointHandle(
82 FetchAndReset(&input->interface_id)));
83 output->set_version(input->version);
84 }
85
86 template <typename T>
87 inline void InterfacePointerToData(InterfacePtr<T> input,
88 Interface_Data* output,
89 SerializationContext* context) {
90 InterfacePtrInfo<T> info = input.PassInterface();
91 output->handle = context->handles.AddHandle(info.PassHandle().release());
92 output->version = info.version();
93 }
94
95 template <typename T>
96 inline void InterfaceDataToPointer(Interface_Data* input,
97 InterfacePtr<T>* output,
98 SerializationContext* context) {
99 output->Bind(InterfacePtrInfo<T>(
100 context->handles.TakeHandleAs<mojo::MessagePipeHandle>(input->handle),
101 input->version));
102 }
103
104 template <typename T> 21 template <typename T>
105 struct HasIsNullMethod { 22 struct HasIsNullMethod {
106 template <typename U> 23 template <typename U>
107 static char Test(decltype(U::IsNull)*); 24 static char Test(decltype(U::IsNull)*);
108 template <typename U> 25 template <typename U>
109 static int Test(...); 26 static int Test(...);
110 static const bool value = sizeof(Test<T>(0)) == sizeof(char); 27 static const bool value = sizeof(Test<T>(0)) == sizeof(char);
111 28
112 private: 29 private:
113 EnsureTypeIsComplete<T> check_t_; 30 EnsureTypeIsComplete<T> check_t_;
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 typename std::enable_if< 204 typename std::enable_if<
288 !HasGetDataMethod<Traits, MaybeConstUserType>::value>::type* = nullptr> 205 !HasGetDataMethod<Traits, MaybeConstUserType>::value>::type* = nullptr>
289 void* CallGetDataIfExists(MaybeConstUserType& input) { 206 void* CallGetDataIfExists(MaybeConstUserType& input) {
290 return nullptr; 207 return nullptr;
291 } 208 }
292 209
293 } // namespace internal 210 } // namespace internal
294 } // namespace mojo 211 } // namespace mojo
295 212
296 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_ 213 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_SERIALIZATION_UTIL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/serialization.h ('k') | mojo/public/cpp/bindings/lib/serialization_util.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698