18 #include "scanner/types.pb.h" 19 #include "scanner/util/memory.h" 27 inline T deser(
const u8*& buffer,
size_t& size_left) {
28 assert(size_left >=
sizeof(T));
31 size_left -=
sizeof(T);
37 void serialize_proto(
const T& element, u8*& buffer,
size_t& size) {
38 size_t element_size = element.ByteSizeLong();
39 buffer = new_buffer(CPU_DEVICE, size);
41 element.SerializeToArray(buffer, element_size);
45 T deserialize_proto(
const u8* buffer,
size_t size) {
47 e.ParseFromArray(buffer, size);
52 void serialize_proto_vector(
const std::vector<T>& elements, u8*& buffer,
54 size =
sizeof(size_t);
55 for (
auto& e : elements) {
56 size += e.ByteSizeLong() +
sizeof(size_t);
58 buffer = new_buffer(CPU_DEVICE, size);
61 *((
size_t*)buf) = elements.size();
62 buf +=
sizeof(size_t);
63 for (
size_t i = 0; i < elements.size(); ++i) {
64 const T& e = elements[i];
65 size_t element_size = e.ByteSizeLong();
66 *((
size_t*)buf) = element_size;
67 buf +=
sizeof(size_t);
68 e.SerializeToArray(buf, element_size);
74 void serialize_proto_vector_of_vectors(
75 const std::vector<std::vector<T>>& elements, u8*& buffer,
size_t& size) {
76 size =
sizeof(size_t);
77 for (
auto& vec_of_e : elements) {
78 size +=
sizeof(size_t);
79 for (
auto& e : vec_of_e) {
80 size += e.ByteSizeLong() +
sizeof(size_t);
83 buffer = new_buffer(CPU_DEVICE, size);
86 *((
size_t*)buf) = elements.size();
87 buf +=
sizeof(size_t);
88 for (
size_t i = 0; i < elements.size(); ++i) {
89 const std::vector<T>& vec_of_e = elements[i];
90 *((
size_t*)buf) = vec_of_e.size();
91 buf +=
sizeof(size_t);
92 for (
size_t j = 0; j < vec_of_e.size(); ++j) {
93 const T& e = vec_of_e[j];
94 size_t element_size = e.ByteSizeLong();
95 *((
size_t*)buf) = element_size;
96 buf +=
sizeof(size_t);
97 e.SerializeToArray(buf, element_size);
103 template <
typename T>
104 std::vector<T> deserialize_proto_vector(
const u8* buffer,
size_t size) {
105 const u8* buf = buffer;
106 size_t num_elements = deser<size_t>(buf, size);
107 std::vector<T> elements;
108 for (
size_t i = 0; i < num_elements; ++i) {
109 size_t element_size = deser<size_t>(buf, size);
110 assert(size >= element_size);
112 e.ParseFromArray(buf, element_size);
113 size -= element_size;
115 elements.push_back(e);
120 inline void serialize_bbox_vector(
const std::vector<BoundingBox>& bboxes,
121 u8*& buffer,
size_t& size) {
122 serialize_proto_vector(bboxes, buffer, size);
125 inline std::vector<BoundingBox> deserialize_bbox_vector(
const u8* buffer,
127 return deserialize_proto_vector<BoundingBox>(buffer, size);
Definition: database.cpp:36