diff options
author | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-01 16:38:46 +0000 |
---|---|---|
committer | viettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-01 16:38:46 +0000 |
commit | 388ba91f3896021ae25dd902198e24f71a3c6404 (patch) | |
tree | 4d1974b54f241303801fee3908464eae2a3d00ae /mojo/public/cpp/bindings/lib/bindings_serialization.h | |
parent | d61f1cc72e7676f95418d0e9f20fa88a5d5391ec (diff) | |
download | chromium_src-388ba91f3896021ae25dd902198e24f71a3c6404.zip chromium_src-388ba91f3896021ae25dd902198e24f71a3c6404.tar.gz chromium_src-388ba91f3896021ae25dd902198e24f71a3c6404.tar.bz2 |
Mojo: Move mojo/public/bindings/lib to mojo/public/cpp/bindings/lib.
R=darin@chromium.org
Review URL: https://codereview.chromium.org/220243007
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@260886 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/public/cpp/bindings/lib/bindings_serialization.h')
-rw-r--r-- | mojo/public/cpp/bindings/lib/bindings_serialization.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/mojo/public/cpp/bindings/lib/bindings_serialization.h b/mojo/public/cpp/bindings/lib/bindings_serialization.h new file mode 100644 index 0000000..a860b14 --- /dev/null +++ b/mojo/public/cpp/bindings/lib/bindings_serialization.h @@ -0,0 +1,67 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ +#define MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ + +#include <vector> + +#include "mojo/public/cpp/bindings/buffer.h" +#include "mojo/public/cpp/bindings/message.h" + +namespace mojo { +namespace internal { + +size_t Align(size_t size); + +// Pointers are encoded as relative offsets. The offsets are relative to the +// address of where the offset value is stored, such that the pointer may be +// recovered with the expression: +// +// ptr = reinterpret_cast<char*>(offset) + *offset +// +// A null pointer is encoded as an offset value of 0. +// +void EncodePointer(const void* ptr, uint64_t* offset); +const void* DecodePointerRaw(const uint64_t* offset); + +template <typename T> +inline void DecodePointer(const uint64_t* offset, T** ptr) { + *ptr = reinterpret_cast<T*>(const_cast<void*>(DecodePointerRaw(offset))); +} + +// Check that the given pointer references memory contained within the message. +bool ValidatePointer(const void* ptr, const Message& message); + +// Handles are encoded as indices into a vector of handles. These functions +// manipulate the value of |handle|, mapping it to and from an index. +void EncodeHandle(Handle* handle, std::vector<Handle>* handles); +bool DecodeHandle(Handle* handle, std::vector<Handle>* handles); + +// The following 2 functions are used to encode/decode all objects (structs and +// arrays) in a consistent manner. + +template <typename T> +inline void Encode(T* obj, std::vector<Handle>* handles) { + if (obj->ptr) + obj->ptr->EncodePointersAndHandles(handles); + EncodePointer(obj->ptr, &obj->offset); +} + +template <typename T> +inline bool Decode(T* obj, Message* message) { + DecodePointer(&obj->offset, &obj->ptr); + if (obj->ptr) { + if (!ValidatePointer(obj->ptr, *message)) + return false; + if (!obj->ptr->DecodePointersAndHandles(message)) + return false; + } + return true; +} + +} // namespace internal +} // namespace mojo + +#endif // MOJO_PUBLIC_CPP_BINDINGS_LIB_BINDINGS_SERIALIZATION_H_ |