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 <cstring>
24#include <iostream>
25#include <memory>
26#include <string>
27#include <exception>
28
29#include "memory_operations.hpp"
30
31namespace datasketches {
32
34template<typename T, typename Enable = void> struct serde {
41 void serialize(std::ostream& os, const T* items, unsigned num) const;
42
49 void deserialize(std::istream& is, T* items, unsigned num) const;
50
58 size_t serialize(void* ptr, size_t capacity, const T* items, unsigned num) const;
59
67 size_t deserialize(const void* ptr, size_t capacity, T* items, unsigned num) const;
68
74 size_t size_of_item(const T& item) const;
75};
76
80template<typename T>
81struct serde<T, typename std::enable_if<std::is_arithmetic<T>::value>::type> {
83 void serialize(std::ostream& os, const T* items, unsigned num) const {
84 bool failure = false;
85 try {
86 os.write(reinterpret_cast<const char*>(items), sizeof(T) * num);
87 } catch (std::ostream::failure&) {
88 failure = true;
89 }
90 if (failure || !os.good()) {
91 throw std::runtime_error("error writing to std::ostream with " + std::to_string(num) + " items");
92 }
93 }
94
95 void deserialize(std::istream& is, T* items, unsigned num) const {
96 bool failure = false;
97 try {
98 is.read((char*)items, sizeof(T) * num);
99 } catch (std::istream::failure&) {
100 failure = true;
101 }
102 if (failure || !is.good()) {
103 throw std::runtime_error("error reading from std::istream with " + std::to_string(num) + " items");
104 }
105 }
106
108 size_t serialize(void* ptr, size_t capacity, const T* items, unsigned num) const {
109 const size_t bytes_written = sizeof(T) * num;
110 check_memory_size(bytes_written, capacity);
111 memcpy(ptr, items, bytes_written);
112 return bytes_written;
113 }
114
116 size_t deserialize(const void* ptr, size_t capacity, T* items, unsigned num) const {
117 const size_t bytes_read = sizeof(T) * num;
118 check_memory_size(bytes_read, capacity);
119 memcpy(items, ptr, bytes_read);
120 return bytes_read;
121 }
122
124 size_t size_of_item(const T& item) const {
125 unused(item);
126 return sizeof(T);
127 }
128};
129
140template<>
141struct serde<std::string> {
143 void serialize(std::ostream& os, const std::string* items, unsigned num) const {
144 unsigned i = 0;
145 bool failure = false;
146 try {
147 for (; i < num && os.good(); i++) {
148 uint32_t length = static_cast<uint32_t>(items[i].size());
149 os.write((char*)&length, sizeof(length));
150 os.write(items[i].c_str(), length);
151 }
152 } catch (std::ostream::failure&) {
153 failure = true;
154 }
155 if (failure || !os.good()) {
156 throw std::runtime_error("error writing to std::ostream at item " + std::to_string(i));
157 }
158 }
159
161 void deserialize(std::istream& is, std::string* items, unsigned num) const {
162 unsigned i = 0;
163 bool failure = false;
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 failure = true;
179 }
180 if (failure || !is.good()) {
181 // clean up what we've already allocated
182 for (unsigned j = 0; j < i; ++j) {
183 items[j].~basic_string();
184 }
185 throw std::runtime_error("error reading from std::istream at item " + std::to_string(i));
186 }
187 }
188
190 size_t serialize(void* ptr, size_t capacity, const std::string* items, unsigned num) const {
191 size_t bytes_written = 0;
192 for (unsigned i = 0; i < num; ++i) {
193 const uint32_t length = static_cast<uint32_t>(items[i].size());
194 const size_t new_bytes = length + sizeof(length);
195 check_memory_size(bytes_written + new_bytes, capacity);
196 memcpy(ptr, &length, sizeof(length));
197 ptr = static_cast<char*>(ptr) + sizeof(uint32_t);
198 memcpy(ptr, items[i].c_str(), length);
199 ptr = static_cast<char*>(ptr) + length;
200 bytes_written += new_bytes;
201 }
202 return bytes_written;
203 }
204
206 size_t deserialize(const void* ptr, size_t capacity, std::string* items, unsigned num) const {
207 size_t bytes_read = 0;
208 unsigned i = 0;
209 bool failure = false;
210 for (; i < num && !failure; ++i) {
211 uint32_t length;
212 if (bytes_read + sizeof(length) > capacity) {
213 bytes_read += sizeof(length); // we'll use this to report the error
214 failure = true;
215 break;
216 }
217 memcpy(&length, ptr, sizeof(length));
218 ptr = static_cast<const char*>(ptr) + sizeof(uint32_t);
219 bytes_read += sizeof(length);
220
221 if (bytes_read + length > capacity) {
222 bytes_read += length; // we'll use this to report the error
223 failure = true;
224 break;
225 }
226 new (&items[i]) std::string(static_cast<const char*>(ptr), length);
227 ptr = static_cast<const char*>(ptr) + length;
228 bytes_read += length;
229 }
230
231 if (failure) {
232 // clean up what we've already allocated
233 for (unsigned j = 0; j < i; ++j)
234 items[j].~basic_string();
235 // using this for a consistent error message
236 check_memory_size(bytes_read, capacity);
237 }
238
239 return bytes_read;
240 }
241
243 size_t size_of_item(const std::string& item) const {
244 return sizeof(uint32_t) + item.size();
245 }
246};
247
248} /* namespace datasketches */
249
250# endif
DataSketches namespace.
Definition binomial_bounds.hpp:38
size_t size_of_item(const T &item) const
Size of the given item.
Definition serde.hpp:124
size_t deserialize(const void *ptr, size_t capacity, T *items, unsigned num) const
Raw bytes deserialization.
Definition serde.hpp:116
void serialize(std::ostream &os, const T *items, unsigned num) const
Stream serialization.
Definition serde.hpp:83
size_t serialize(void *ptr, size_t capacity, const T *items, unsigned num) const
Raw bytes serialization.
Definition serde.hpp:108
void serialize(std::ostream &os, const std::string *items, unsigned num) const
Stream serialization.
Definition serde.hpp:143
size_t serialize(void *ptr, size_t capacity, const std::string *items, unsigned num) const
Raw bytes serialization.
Definition serde.hpp:190
size_t size_of_item(const std::string &item) const
Size of the given item.
Definition serde.hpp:243
size_t deserialize(const void *ptr, size_t capacity, std::string *items, unsigned num) const
Raw bytes deserialization.
Definition serde.hpp:206
void deserialize(std::istream &is, std::string *items, unsigned num) const
Stream deserialization.
Definition serde.hpp:161
Interface for serializing and deserializing items.
Definition serde.hpp:34
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.