#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);
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)));
}
bool ValidatePointer(const void* ptr, const Message& message);
void EncodeHandle(Handle* handle, std::vector<Handle>* handles);
bool DecodeHandle(Handle* handle, std::vector<Handle>* handles);
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;
}
}
}
#endif