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

Side by Side Diff: mojo/public/cpp/bindings/lib/bindings_internal.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_BINDINGS_INTERNAL_H_ 5 #ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
7 7
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include "mojo/public/cpp/bindings/interface_id.h" 10 #include "mojo/public/cpp/bindings/interface_id.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 const uint32_t kUnionDataSize = 16; 50 const uint32_t kUnionDataSize = 16;
51 51
52 template <typename T> 52 template <typename T>
53 class Array_Data; 53 class Array_Data;
54 54
55 template <typename K, typename V> 55 template <typename K, typename V>
56 class Map_Data; 56 class Map_Data;
57 57
58 using String_Data = Array_Data<char>; 58 using String_Data = Array_Data<char>;
59 59
60 size_t Align(size_t size);
61 char* AlignPointer(char* ptr);
62
63 bool IsAligned(const void* ptr);
64
65 // Pointers are encoded as relative offsets. The offsets are relative to the
66 // address of where the offset value is stored, such that the pointer may be
67 // recovered with the expression:
68 //
69 // ptr = reinterpret_cast<char*>(offset) + *offset
70 //
71 // A null pointer is encoded as an offset value of 0.
72 //
73 void EncodePointer(const void* ptr, uint64_t* offset);
74 // Note: This function doesn't validate the encoded pointer value.
75 inline const void* DecodePointer(const uint64_t* offset) {
76 if (!*offset)
77 return nullptr;
78 return reinterpret_cast<const char*>(offset) + *offset;
79 }
80
60 #pragma pack(push, 1) 81 #pragma pack(push, 1)
61 82
62 struct StructHeader { 83 struct StructHeader {
63 uint32_t num_bytes; 84 uint32_t num_bytes;
64 uint32_t version; 85 uint32_t version;
65 }; 86 };
66 static_assert(sizeof(StructHeader) == 8, "Bad sizeof(StructHeader)"); 87 static_assert(sizeof(StructHeader) == 8, "Bad sizeof(StructHeader)");
67 88
68 struct ArrayHeader { 89 struct ArrayHeader {
69 uint32_t num_bytes; 90 uint32_t num_bytes;
70 uint32_t num_elements; 91 uint32_t num_elements;
71 }; 92 };
72 static_assert(sizeof(ArrayHeader) == 8, "Bad_sizeof(ArrayHeader)"); 93 static_assert(sizeof(ArrayHeader) == 8, "Bad_sizeof(ArrayHeader)");
73 94
74 template <typename T> 95 template <typename T>
75 union Pointer { 96 struct Pointer {
97 using BaseType = T;
98
99 void Set(T* ptr) { EncodePointer(ptr, &offset); }
100 const T* Get() const { return static_cast<const T*>(DecodePointer(&offset)); }
101 T* Get() {
102 return static_cast<T*>(const_cast<void*>(DecodePointer(&offset)));
103 }
104
105 bool is_null() const { return offset == 0; }
106
76 uint64_t offset; 107 uint64_t offset;
77 T* ptr;
78 }; 108 };
79 static_assert(sizeof(Pointer<char>) == 8, "Bad_sizeof(Pointer)"); 109 static_assert(sizeof(Pointer<char>) == 8, "Bad_sizeof(Pointer)");
80 110
81 struct Handle_Data { 111 struct Handle_Data {
82 Handle_Data() = default; 112 Handle_Data() = default;
83 explicit Handle_Data(uint32_t value) : value(value) {} 113 explicit Handle_Data(uint32_t value) : value(value) {}
84 114
85 bool is_valid() const { return value != kEncodedInvalidHandleValue; } 115 bool is_valid() const { return value != kEncodedInvalidHandleValue; }
86 116
87 uint32_t value; 117 uint32_t value;
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
165 struct MojomTypeTraits { 195 struct MojomTypeTraits {
166 using Data = T; 196 using Data = T;
167 using DataAsArrayElement = Data; 197 using DataAsArrayElement = Data;
168 198
169 static const MojomTypeCategory category = MojomTypeCategory::POD; 199 static const MojomTypeCategory category = MojomTypeCategory::POD;
170 }; 200 };
171 201
172 template <typename T> 202 template <typename T>
173 struct MojomTypeTraits<Array<T>, false> { 203 struct MojomTypeTraits<Array<T>, false> {
174 using Data = Array_Data<typename MojomTypeTraits<T>::DataAsArrayElement>; 204 using Data = Array_Data<typename MojomTypeTraits<T>::DataAsArrayElement>;
175 using DataAsArrayElement = Data*; 205 using DataAsArrayElement = Pointer<Data>;
176 206
177 static const MojomTypeCategory category = MojomTypeCategory::ARRAY; 207 static const MojomTypeCategory category = MojomTypeCategory::ARRAY;
178 }; 208 };
179 209
180 template <typename T> 210 template <typename T>
181 struct MojomTypeTraits<AssociatedInterfacePtrInfo<T>, false> { 211 struct MojomTypeTraits<AssociatedInterfacePtrInfo<T>, false> {
182 using Data = AssociatedInterface_Data; 212 using Data = AssociatedInterface_Data;
183 using DataAsArrayElement = Data; 213 using DataAsArrayElement = Data;
184 214
185 static const MojomTypeCategory category = 215 static const MojomTypeCategory category =
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 using DataAsArrayElement = Data; 263 using DataAsArrayElement = Data;
234 264
235 static const MojomTypeCategory category = 265 static const MojomTypeCategory category =
236 MojomTypeCategory::INTERFACE_REQUEST; 266 MojomTypeCategory::INTERFACE_REQUEST;
237 }; 267 };
238 268
239 template <typename K, typename V> 269 template <typename K, typename V>
240 struct MojomTypeTraits<Map<K, V>, false> { 270 struct MojomTypeTraits<Map<K, V>, false> {
241 using Data = Map_Data<typename MojomTypeTraits<K>::DataAsArrayElement, 271 using Data = Map_Data<typename MojomTypeTraits<K>::DataAsArrayElement,
242 typename MojomTypeTraits<V>::DataAsArrayElement>; 272 typename MojomTypeTraits<V>::DataAsArrayElement>;
243 using DataAsArrayElement = Data*; 273 using DataAsArrayElement = Pointer<Data>;
244 274
245 static const MojomTypeCategory category = MojomTypeCategory::MAP; 275 static const MojomTypeCategory category = MojomTypeCategory::MAP;
246 }; 276 };
247 277
248 template <> 278 template <>
249 struct MojomTypeTraits<String, false> { 279 struct MojomTypeTraits<String, false> {
250 using Data = String_Data; 280 using Data = String_Data;
251 using DataAsArrayElement = Data*; 281 using DataAsArrayElement = Pointer<Data>;
252 282
253 static const MojomTypeCategory category = MojomTypeCategory::STRING; 283 static const MojomTypeCategory category = MojomTypeCategory::STRING;
254 }; 284 };
255 285
256 template <typename T> 286 template <typename T>
257 struct MojomTypeTraits<StructPtr<T>, false> { 287 struct MojomTypeTraits<StructPtr<T>, false> {
258 using Data = typename T::Data_; 288 using Data = typename T::Data_;
259 using DataAsArrayElement = 289 using DataAsArrayElement =
260 typename std::conditional<IsUnionDataType<Data>::value, 290 typename std::conditional<IsUnionDataType<Data>::value,
261 Data, 291 Data,
262 Data*>::type; 292 Pointer<Data>>::type;
263 293
264 static const MojomTypeCategory category = IsUnionDataType<Data>::value 294 static const MojomTypeCategory category = IsUnionDataType<Data>::value
265 ? MojomTypeCategory::UNION 295 ? MojomTypeCategory::UNION
266 : MojomTypeCategory::STRUCT; 296 : MojomTypeCategory::STRUCT;
267 }; 297 };
268 298
269 template <typename T> 299 template <typename T>
270 struct MojomTypeTraits<InlinedStructPtr<T>, false> { 300 struct MojomTypeTraits<InlinedStructPtr<T>, false> {
271 using Data = typename T::Data_; 301 using Data = typename T::Data_;
272 using DataAsArrayElement = 302 using DataAsArrayElement =
273 typename std::conditional<IsUnionDataType<Data>::value, 303 typename std::conditional<IsUnionDataType<Data>::value,
274 Data, 304 Data,
275 Data*>::type; 305 Pointer<Data>>::type;
276 306
277 static const MojomTypeCategory category = IsUnionDataType<Data>::value 307 static const MojomTypeCategory category = IsUnionDataType<Data>::value
278 ? MojomTypeCategory::UNION 308 ? MojomTypeCategory::UNION
279 : MojomTypeCategory::STRUCT; 309 : MojomTypeCategory::STRUCT;
280 }; 310 };
281 311
282 template <typename T, MojomTypeCategory categories> 312 template <typename T, MojomTypeCategory categories>
283 struct BelongsTo { 313 struct BelongsTo {
284 static const bool value = 314 static const bool value =
285 static_cast<uint32_t>(MojomTypeTraits<T>::category & categories) != 0; 315 static_cast<uint32_t>(MojomTypeTraits<T>::category & categories) != 0;
286 }; 316 };
287 317
288 } // namespace internal 318 } // namespace internal
289 } // namespace mojo 319 } // namespace mojo
290 320
291 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_ 321 #endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_INTERNAL_H_
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/array_serialization.h ('k') | mojo/public/cpp/bindings/lib/bindings_internal.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698