Scanner C++ API
serialize.h
1 /* Copyright 2016 Carnegie Mellon University
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include "scanner/types.pb.h"
19 #include "scanner/util/memory.h"
20 
21 #include <cstddef>
22 #include <vector>
23 
24 namespace scanner {
25 namespace {
26 template <typename T>
27 inline T deser(const u8*& buffer, size_t& size_left) {
28  assert(size_left >= sizeof(T));
29  T e = *((T*)buffer);
30  buffer += sizeof(T);
31  size_left -= sizeof(T);
32  return e;
33 }
34 }
35 
36 template <typename 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);
40  size = element_size;
41  element.SerializeToArray(buffer, element_size);
42 }
43 
44 template <typename T>
45 T deserialize_proto(const u8* buffer, size_t size) {
46  T e;
47  e.ParseFromArray(buffer, size);
48  return e;
49 }
50 
51 template <typename T>
52 void serialize_proto_vector(const std::vector<T>& elements, u8*& buffer,
53  size_t& size) {
54  size = sizeof(size_t);
55  for (auto& e : elements) {
56  size += e.ByteSizeLong() + sizeof(size_t);
57  }
58  buffer = new_buffer(CPU_DEVICE, size);
59 
60  u8* buf = buffer;
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);
69  buf += element_size;
70  }
71 }
72 
73 template <typename T>
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);
81  }
82  }
83  buffer = new_buffer(CPU_DEVICE, size);
84 
85  u8* buf = buffer;
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);
98  buf += element_size;
99  }
100  }
101 }
102 
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);
111  T e;
112  e.ParseFromArray(buf, element_size);
113  size -= element_size;
114  buf += element_size;
115  elements.push_back(e);
116  }
117  return elements;
118 }
119 
120 inline void serialize_bbox_vector(const std::vector<BoundingBox>& bboxes,
121  u8*& buffer, size_t& size) {
122  serialize_proto_vector(bboxes, buffer, size);
123 }
124 
125 inline std::vector<BoundingBox> deserialize_bbox_vector(const u8* buffer,
126  size_t size) {
127  return deserialize_proto_vector<BoundingBox>(buffer, size);
128 }
129 
130 // inline void serialize_decode_args(const DecodeArgs& args, u8*& buffer,
131 // size_t& size) {
132 // size = args.ByteSize();
133 // buffer = new_buffer(CPU_DEVICE, size);
134 // args.SerializeToArray(buffer, size);
135 // }
136 
137 // inline DecodeArgs deserialize_decode_args(const u8* buffer, size_t size) {
138 // DecodeArgs args;
139 // args.ParseFromArray(buffer, size);
140 // return args;
141 // }
142 
143 // inline void serialize_image_decode_args(const ImageDecodeArgs& args,
144 // u8*& buffer, size_t& size) {
145 // size = args.ByteSize();
146 // buffer = new_buffer(CPU_DEVICE, size);
147 // args.SerializeToArray(buffer, size);
148 // }
149 
150 // inline ImageDecodeArgs deserialize_image_decode_args(const u8* buffer,
151 // size_t size) {
152 // ImageDecodeArgs args;
153 // args.ParseFromArray(buffer, size);
154 // return args;
155 // }
156 }
Definition: database.cpp:36