// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #pragma once #include #include #include #include template inline void serialize_value(void** buffer, T const& value); template inline void deserialize_value(void const** buffer, size_t* buffer_size, T* value); namespace { template struct Serializer {}; template struct Serializer::value || std::is_enum::value || std::is_pod::value>::type> { static size_t serialized_size(T const& value) { return sizeof(T); } static void serialize(void** buffer, T const& value) { ::memcpy(*buffer, &value, sizeof(T)); reinterpret_cast(*buffer) += sizeof(T); } static void deserialize(void const** buffer, size_t* buffer_size, T* value) { assert(*buffer_size >= sizeof(T)); ::memcpy(value, *buffer, sizeof(T)); reinterpret_cast(*buffer) += sizeof(T); *buffer_size -= sizeof(T); } }; template <> struct Serializer { static size_t serialized_size(const char* value) { return strlen(value) + 1; } static void serialize(void** buffer, const char* value) { ::strcpy(static_cast(*buffer), value); reinterpret_cast(*buffer) += strlen(value) + 1; } static void deserialize(void const** buffer, size_t* buffer_size, const char** value) { *value = static_cast(*buffer); size_t data_size = strnlen(*value, *buffer_size) + 1; assert(*buffer_size >= data_size); reinterpret_cast(*buffer) += data_size; *buffer_size -= data_size; } }; template struct Serializer, typename std::enable_if::value || std::is_enum::value || std::is_pod::value>::type> { static size_t serialized_size(std::vector const& value) { return sizeof(value.size()) + value.size() * sizeof(T); } static void serialize(void** buffer, std::vector const& value) { serialize_value(buffer, value.size()); size_t nbyte = value.size() * sizeof(T); ::memcpy(*buffer, value.data(), nbyte); reinterpret_cast(*buffer) += nbyte; } static void deserialize(void const** buffer, size_t* buffer_size, std::vector* value) { size_t size; deserialize_value(buffer, buffer_size, &size); value->resize(size); size_t nbyte = value->size() * sizeof(T); assert(*buffer_size >= nbyte); ::memcpy(value->data(), *buffer, nbyte); reinterpret_cast(*buffer) += nbyte; *buffer_size -= nbyte; } }; } // namespace template inline size_t serialized_size(T const& value) { return Serializer::serialized_size(value); } template inline void serialize_value(void** buffer, T const& value) { return Serializer::serialize(buffer, value); } template inline void deserialize_value(void const** buffer, size_t* buffer_size, T* value) { return Serializer::deserialize(buffer, buffer_size, value); }