datasketches-cpp
Loading...
Searching...
No Matches
serde.hpp
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef DATASKETCHES_SERDE_HPP_
21#define DATASKETCHES_SERDE_HPP_
22
23#include <cstdint>
24#include <cstring>
25#include <iostream>
26#include <memory>
27#include <string>
28#include <exception>
29
30#include "memory_operations.hpp"
31
32namespace datasketches {
33
35template<typename T, typename Enable = void> struct serde {
42 void serialize(std::ostream& os, const T* items, unsigned num) const;
43
50 void deserialize(std::istream& is, T* items, unsigned num) const;
51
59 size_t serialize(void* ptr, size_t capacity, const T* items, unsigned num) const;
60
68 size_t deserialize(const void* ptr, size_t capacity, T* items, unsigned num) const;
69
75 size_t size_of_item(const T& item) const;
76};
77
81template<typename T>
82struct serde<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> {
84 void serialize(std::ostream& os, const T* items, unsigned num) const {
85 bool failure = false;
86 try {
87 os.write(reinterpret_cast<const char*>(items), sizeof(T) * num);
88 } catch (std::ostream::failure&) {
89 failure = true;
90 }
91 if (failure || !os.good()) {
92 throw std::runtime_error("error writing to std::ostream with " + std::to_string(num) + " items");
93 }
94 }
95
96 void deserialize(std::istream& is, T* items, unsigned num) const {
97 bool failure = false;
98 try {
99 is.read((char*)items, sizeof(T) * num);
100 } catch (std::istream::failure&) {
101 failure = true;
102 }
103 if (failure || !is.good()) {
104 throw std::runtime_error("error reading from std::istream with " + std::to_string(num) + " items");
105 }
106 }
107
109 size_t serialize(void* ptr, size_t capacity, const T* items, unsigned num) const {
110 const size_t bytes_written = sizeof(T) * num;
111 check_memory_size(bytes_written, capacity);
112 memcpy(ptr, items, bytes_written);
113 return bytes_written;
114 }
115
117 size_t deserialize(const void* ptr, size_t capacity, T* items, unsigned num) const {
118 const size_t bytes_read = sizeof(T) * num;
119 check_memory_size(bytes_read, capacity);
120 memcpy(items, ptr, bytes_read);
121 return bytes_read;
122 }
123
125 size_t size_of_item(const T& item) const {
126 unused(item);
127 return sizeof(T);
128 }
129};
130
141template<>
142struct serde<std::string> {
144 void serialize(std::ostream& os, const std::string* items, unsigned num) const {
145 unsigned i = 0;
146 bool failure = false;
147 try {
148 for (; i < num && os.good(); i++) {
149 uint32_t length = static_cast<uint32_t>(items[i].size());
150 os.write((char*)&length, sizeof(length));
151 os.write(items[i].c_str(), length);
152 }
153 } catch (std::ostream::failure&) {
154 failure = true;
155 }
156 if (failure || !os.good()) {
157 throw std::runtime_error("error writing to std::ostream at item " + std::to_string(i));
158 }
159 }
160
162 void deserialize(std::istream& is, std::string* items, unsigned num) const {
163 unsigned i = 0;
164 try {
165 for (; i < num; i++) {
166 uint32_t length;
167 is.read((char*)&length, sizeof(length));
168 if (!is.good()) { break; }
169 std::string str;
170 str.reserve(length);
171 for (uint32_t j = 0; j < length; j++) {
172 str.push_back(static_cast<char>(is.get()));
173 }
174 if (!is.good()) { break; }
175 new (&items[i]) std::string(std::move(str));
176 }
177 } catch (std::istream::failure&) {
178 // the stream sets its state before throwing, so this is reported below
179 } catch (...) {
180 for (unsigned j = 0; j < i; ++j) {
181 items[j].~basic_string();
182 }
183 throw;
184 }
185 if (!is.good()) {
186 for (unsigned j = 0; j < i; ++j) {
187 items[j].~basic_string();
188 }
189 throw std::runtime_error("error reading from std::istream at item " + std::to_string(i));
190 }
191 }
192
194 size_t serialize(void* ptr, size_t capacity, const std::string* items, unsigned num) const {
195 size_t bytes_written = 0;
196 for (unsigned i = 0; i < num; ++i) {
197 const uint32_t length = static_cast<uint32_t>(items[i].size());
198 const size_t new_bytes = length + sizeof(length);
199 check_memory_size(bytes_written + new_bytes, capacity);
200 memcpy(ptr, &length, sizeof(length));
201 ptr = static_cast<char*>(ptr) + sizeof(uint32_t);
202 memcpy(ptr, items[i].c_str(), length);
203 ptr = static_cast<char*>(ptr) + length;
204 bytes_written += new_bytes;
205 }
206 return bytes_written;
207 }
208
210 size_t deserialize(const void* ptr, size_t capacity, std::string* items, unsigned num) const {
211 size_t bytes_read = 0;
212 unsigned i = 0;
213 bool failure = false;
214 try {
215 for (; i < num && !failure; ++i) {
216 uint32_t length;
217 if (bytes_read + sizeof(length) > capacity) {
218 bytes_read += sizeof(length); // we'll use this to report the error
219 failure = true;
220 break;
221 }
222 memcpy(&length, ptr, sizeof(length));
223 ptr = static_cast<const char*>(ptr) + sizeof(uint32_t);
224 bytes_read += sizeof(length);
225
226 if (bytes_read + length > capacity) {
227 bytes_read += length; // we'll use this to report the error
228 failure = true;
229 break;
230 }
231 new (&items[i]) std::string(static_cast<const char*>(ptr), length);
232 ptr = static_cast<const char*>(ptr) + length;
233 bytes_read += length;
234 }
235 } catch (...) {
236 for (unsigned j = 0; j < i; ++j) {
237 items[j].~basic_string();
238 }
239 throw;
240 }
241
242 if (failure) {
243 // clean up what we've already allocated
244 for (unsigned j = 0; j < i; ++j)
245 items[j].~basic_string();
246 // using this for a consistent error message
247 check_memory_size(bytes_read, capacity);
248 }
249
250 return bytes_read;
251 }
252
254 size_t size_of_item(const std::string& item) const {
255 return sizeof(uint32_t) + item.size();
256 }
257};
258
259} /* namespace datasketches */
260
261# endif
DataSketches namespace.
Definition binomial_bounds.hpp:38
size_t deserialize(const void *ptr, size_t capacity, T *items, unsigned num) const
Definition serde.hpp:117
void serialize(std::ostream &os, const T *items, unsigned num) const
Definition serde.hpp:84
size_t serialize(void *ptr, size_t capacity, const T *items, unsigned num) const
Definition serde.hpp:109
void serialize(std::ostream &os, const std::string *items, unsigned num) const
Stream serialization.
Definition serde.hpp:144
size_t serialize(void *ptr, size_t capacity, const std::string *items, unsigned num) const
Raw bytes serialization.
Definition serde.hpp:194
size_t size_of_item(const std::string &item) const
Size of the given item.
Definition serde.hpp:254
size_t deserialize(const void *ptr, size_t capacity, std::string *items, unsigned num) const
Raw bytes deserialization.
Definition serde.hpp:210
void deserialize(std::istream &is, std::string *items, unsigned num) const
Stream deserialization.
Definition serde.hpp:162
Interface for serializing and deserializing items.
Definition serde.hpp:35
size_t size_of_item(const T &item) const
Size of the given item.
size_t deserialize(const void *ptr, size_t capacity, T *items, unsigned num) const
Raw bytes deserialization.
void deserialize(std::istream &is, T *items, unsigned num) const
Stream deserialization.
void serialize(std::ostream &os, const T *items, unsigned num) const
Stream serialization.
size_t serialize(void *ptr, size_t capacity, const T *items, unsigned num) const
Raw bytes serialization.