serialize.h 3.9 KB
Newer Older
N
nhzlx 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// 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 <cassert>
#include <cstring>
#include <type_traits>
#include <vector>

template <typename T>
N
nhzlx 已提交
23
inline void SerializeValue(void** buffer, T const& value);
N
nhzlx 已提交
24 25

template <typename T>
N
nhzlx 已提交
26 27
inline void DeserializeValue(void const** buffer, size_t* buffer_size,
                             T* value);
N
nhzlx 已提交
28 29 30 31 32 33 34 35 36 37

namespace {

template <typename T, class Enable = void>
struct Serializer {};

template <typename T>
struct Serializer<T, typename std::enable_if<std::is_arithmetic<T>::value ||
                                             std::is_enum<T>::value ||
                                             std::is_pod<T>::value>::type> {
N
nhzlx 已提交
38 39 40
  static size_t SerializedSize(T const& value) { return sizeof(T); }
  static void Serialize(void** buffer, T const& value) {
    std::memcpy(*buffer, &value, sizeof(T));
N
nhzlx 已提交
41 42
    reinterpret_cast<char*&>(*buffer) += sizeof(T);
  }
N
nhzlx 已提交
43
  static void Deserialize(void const** buffer, size_t* buffer_size, T* value) {
N
nhzlx 已提交
44
    assert(*buffer_size >= sizeof(T));
N
nhzlx 已提交
45
    std::memcpy(value, *buffer, sizeof(T));
N
nhzlx 已提交
46 47 48 49 50 51 52
    reinterpret_cast<char const*&>(*buffer) += sizeof(T);
    *buffer_size -= sizeof(T);
  }
};

template <>
struct Serializer<const char*> {
N
nhzlx 已提交
53 54 55
  static size_t SerializedSize(const char* value) { return strlen(value) + 1; }
  static void Serialize(void** buffer, const char* value) {
    std::strcpy(static_cast<char*>(*buffer), value);
N
nhzlx 已提交
56 57
    reinterpret_cast<char*&>(*buffer) += strlen(value) + 1;
  }
N
nhzlx 已提交
58
  static void Deserialize(void const** buffer, size_t* buffer_size,
N
nhzlx 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72
                          const char** value) {
    *value = static_cast<char const*>(*buffer);
    size_t data_size = strnlen(*value, *buffer_size) + 1;
    assert(*buffer_size >= data_size);
    reinterpret_cast<char const*&>(*buffer) += data_size;
    *buffer_size -= data_size;
  }
};

template <typename T>
struct Serializer<std::vector<T>,
                  typename std::enable_if<std::is_arithmetic<T>::value ||
                                          std::is_enum<T>::value ||
                                          std::is_pod<T>::value>::type> {
N
nhzlx 已提交
73
  static size_t SerializedSize(std::vector<T> const& value) {
N
nhzlx 已提交
74 75
    return sizeof(value.size()) + value.size() * sizeof(T);
  }
N
nhzlx 已提交
76 77
  static void Serialize(void** buffer, std::vector<T> const& value) {
    SerializeValue(buffer, value.size());
N
nhzlx 已提交
78
    size_t nbyte = value.size() * sizeof(T);
N
nhzlx 已提交
79
    std::memcpy(*buffer, value.data(), nbyte);
N
nhzlx 已提交
80 81
    reinterpret_cast<char*&>(*buffer) += nbyte;
  }
N
nhzlx 已提交
82
  static void Deserialize(void const** buffer, size_t* buffer_size,
N
nhzlx 已提交
83 84
                          std::vector<T>* value) {
    size_t size;
N
nhzlx 已提交
85
    DeserializeValue(buffer, buffer_size, &size);
N
nhzlx 已提交
86 87 88
    value->resize(size);
    size_t nbyte = value->size() * sizeof(T);
    assert(*buffer_size >= nbyte);
N
nhzlx 已提交
89
    std::memcpy(value->data(), *buffer, nbyte);
N
nhzlx 已提交
90 91 92 93 94 95 96 97
    reinterpret_cast<char const*&>(*buffer) += nbyte;
    *buffer_size -= nbyte;
  }
};

}  // namespace

template <typename T>
N
nhzlx 已提交
98 99
inline size_t SerializedSize(T const& value) {
  return Serializer<T>::SerializedSize(value);
N
nhzlx 已提交
100 101 102
}

template <typename T>
N
nhzlx 已提交
103 104
inline void SerializeValue(void** buffer, T const& value) {
  return Serializer<T>::Serialize(buffer, value);
N
nhzlx 已提交
105 106 107
}

template <typename T>
N
nhzlx 已提交
108 109 110
inline void DeserializeValue(void const** buffer, size_t* buffer_size,
                             T* value) {
  return Serializer<T>::Deserialize(buffer, buffer_size, value);
N
nhzlx 已提交
111
}