Branch data Line data Source code
1 : : // ----------------- BEGIN LICENSE BLOCK ---------------------------------
2 : : //
3 : : // Copyright (C) 2018-2021 Intel Corporation
4 : : //
5 : : // SPDX-License-Identifier: MIT
6 : : //
7 : : // ----------------- END LICENSE BLOCK -----------------------------------
8 : : /**
9 : : * @file
10 : : */
11 : :
12 : : #pragma once
13 : :
14 : : #include <cstdint>
15 : : #include <limits>
16 : : #include <map>
17 : : #include <memory>
18 : : #include <type_traits>
19 : : #include <utility>
20 : : #include <vector>
21 : :
22 : : #include "ad/map/serialize/SerializeableMagic.hpp"
23 : :
24 : : /** @brief namespace ad */
25 : : namespace ad {
26 : : /** @brief namespace map */
27 : : namespace map {
28 : : /** @brief namespace serialize */
29 : : namespace serialize {
30 : :
31 : : /**
32 : : * @brief Interface for Serializer
33 : : */
34 : : class ISerializer
35 : : {
36 : : public: // Constructor/Destructor
37 : 169 : explicit ISerializer(bool store)
38 : 169 : : mIsStoring(store)
39 : : , mUseMagic(true)
40 : 169 : , mUseEmbeddedPoints(true)
41 : : {
42 : 169 : }
43 : :
44 : 169 : virtual ~ISerializer() = default;
45 : :
46 : 167664 : bool isStoring() const
47 : : {
48 : 167664 : return mIsStoring;
49 : : }
50 : :
51 : : public: // Operations
52 : 656064 : template <typename T> bool serialize(T &x)
53 : : {
54 [ + + ]: 656064 : if (mIsStoring)
55 : : {
56 : 19465 : return write(x);
57 : : }
58 : : else
59 : : {
60 : 636599 : return read(x);
61 : : }
62 : : }
63 : :
64 : 5310480 : bool serialize(SerializeableMagic const &magic)
65 : : {
66 [ + + ]: 5310480 : if (mUseMagic)
67 : : {
68 [ + + ]: 5161650 : if (mIsStoring)
69 : : {
70 [ + - ]: 49723 : return write(static_cast<uint16_t>(magic));
71 : : }
72 : : else
73 : : {
74 : : uint16_t magicRead;
75 [ + - + - : 5111920 : auto const ok = read(magicRead) && (static_cast<uint16_t>(magic) == magicRead);
+ + ]
76 : 5111920 : return ok;
77 : : }
78 : : }
79 : : else
80 : : {
81 : 148836 : return true;
82 : : }
83 : : }
84 : :
85 : 3301832 : template <typename T, typename SerializeBaseType, SerializeableMagic magic> bool serializeGeneratedType(T &x)
86 : : {
87 [ + - + + ]: 3301832 : if (serialize(magic))
88 : : {
89 [ + + ]: 3301832 : if (mIsStoring)
90 : : {
91 [ + - ]: 62894 : return write(static_cast<SerializeBaseType>(x));
92 : : }
93 : : else
94 : : {
95 : : SerializeBaseType xD;
96 [ + - + - ]: 3238931 : if (read(xD))
97 : : {
98 : 3238931 : x = T(xD);
99 : 3238931 : return true;
100 : : }
101 : : else
102 : : {
103 : 0 : return false;
104 : : }
105 : : }
106 : : }
107 : 1 : return false;
108 : : }
109 : :
110 : 248086 : template <typename T> bool serializeVector(std::vector<T> &x)
111 : : {
112 [ + + ]: 248086 : if (mIsStoring)
113 : : {
114 : 9286 : return writeVector(x);
115 : : }
116 : : else
117 : : {
118 : 238800 : return readVector(x);
119 : : }
120 : : }
121 : :
122 : : template <typename T>
123 [ + - + - : 717191 : bool serializeObjectVector(std::vector<T> &x, SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType)
+ - + + -
- - - -
- ]
124 : : {
125 [ + + ]: 529362 : if (mIsStoring)
126 : : {
127 : 16928 : return writeObjectVector(x, magic);
128 : : }
129 : : else
130 : : {
131 : 512434 : return readObjectVector(x, magic);
132 : : }
133 : : }
134 : :
135 : 4 : template <typename T, typename U, typename Comp> bool serializeObjectMap(std::map<T, U, Comp> &x)
136 : : {
137 [ + + ]: 4 : if (mIsStoring)
138 : : {
139 : 2 : return writeObjectMap(x);
140 : : }
141 : : else
142 : : {
143 : 2 : return readObjectMap(x);
144 : : }
145 : : }
146 : :
147 : 333 : template <typename T, typename U, typename Comp> bool serializeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
148 : : {
149 [ + + ]: 333 : if (mIsStoring)
150 : : {
151 : 22 : return writeObjectPtrMap(x);
152 : : }
153 : : else
154 : : {
155 : 311 : return readObjectPtrMap(x);
156 : : }
157 : : }
158 : :
159 : 332 : template <typename T, typename U, typename Comp> bool serializeObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
160 : : {
161 [ + + ]: 332 : if (mIsStoring)
162 : : {
163 : 22 : return writeObjectVecMap(x);
164 : : }
165 : : else
166 : : {
167 : 310 : return readObjectVecMap(x);
168 : : }
169 : : }
170 : :
171 : : private: // Operations
172 : : template <typename T> bool read(T &x);
173 : : template <typename T> bool read(T const &x);
174 : : template <typename T> bool write(T const &x);
175 : :
176 : : template <typename T> bool readGeneratedDouble(T &x);
177 : : template <typename T> bool writeGeneratedDouble(T const &x);
178 : :
179 : : template <typename T> bool readVector(std::vector<T> &x);
180 : : template <typename T> bool writeVector(std::vector<T> &x);
181 : :
182 : : template <typename T>
183 : 0 : bool readObjectVector(std::vector<T> &x, SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType);
184 : : template <typename T>
185 : 0 : bool writeObjectVector(std::vector<T> &x, SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType);
186 : :
187 : : template <typename T, typename U, typename Comp> bool readObjectMap(std::map<T, U, Comp> &x);
188 : : template <typename T, typename U, typename Comp> bool writeObjectMap(std::map<T, U, Comp> &x);
189 : :
190 : : template <typename T, typename U, typename Comp> bool readObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x);
191 : : template <typename T, typename U, typename Comp> bool writeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x);
192 : :
193 : : template <typename T, typename U, typename Comp> bool readObjectVecMap(std::map<T, std::vector<U>, Comp> &x);
194 : : template <typename T, typename U, typename Comp> bool writeObjectVecMap(std::map<T, std::vector<U>, Comp> &x);
195 : :
196 : : public: // Aux Methods
197 : : virtual bool write(const void *x, size_t bytes) = 0;
198 : : virtual bool read(void *x, size_t bytes) = 0;
199 : :
200 : : /**
201 : : * @brief Specifies if the every serialized block will be/is prefixed with object-specific
202 : : * magic number. This is application-wide setting.
203 : : *
204 : : * \warning Deserialization setting must use same value that is applied to serialization!
205 : : *
206 : : * @param[in] use_magic Set to true if every serialized block will be/is prefixed with
207 : : * object-specific magic number.
208 : : *
209 : : * @return Old value of the flag.
210 : : */
211 : 501 : bool setUseMagic(bool use_magic)
212 : : {
213 : 501 : bool old_magic = mUseMagic;
214 : 501 : mUseMagic = use_magic;
215 : 501 : return old_magic;
216 : : }
217 : :
218 : : /**
219 : : * @brief Specifies if the geometry points should be embedded within the objects or handled separately.
220 : : *
221 : : * \warning Deserialization setting must use same value that is applied to serialization!
222 : : *
223 : : * @param[in] useEmbeddedPoints Set to true if every geometry object should embed the geometry points.
224 : : * If false, geometry points have to be handled separately.
225 : : *
226 : : * @return Old value of the flag.
227 : : */
228 : 501 : bool setUseEmbeddedPoints(bool useEmbeddedPoints)
229 : : {
230 : 501 : bool oldUseEmbeddedPoints = mUseEmbeddedPoints;
231 : 501 : mUseEmbeddedPoints = useEmbeddedPoints;
232 : 501 : return oldUseEmbeddedPoints;
233 : : }
234 : :
235 : : /**
236 : : * @brief retunrs the setting if embedded points should be used or not on serialization
237 : : */
238 : 2008 : bool useEmbeddedPoints() const
239 : : {
240 : 2008 : return mUseEmbeddedPoints;
241 : : }
242 : :
243 : : /**
244 : : * @Todo will delete this after preparing new map without connector
245 : : */
246 : 332 : bool serializeEmptyObjectVec()
247 : : {
248 [ + - - + ]: 332 : if (!serialize(SerializeableMagic::ObjectVectorMapType))
249 : : {
250 : 0 : return false;
251 : : }
252 : :
253 : 332 : size_t n = 0;
254 [ + + ]: 332 : if (mIsStoring)
255 : : {
256 [ + - - + ]: 22 : if (!write(n))
257 : : {
258 : 0 : return false;
259 : : }
260 : : }
261 : : else
262 : : {
263 [ + - - + ]: 310 : if (!read(n))
264 : : {
265 : 0 : return false;
266 : : }
267 : : }
268 : 332 : return true;
269 : : }
270 : :
271 : : private: // Data Members
272 : : bool mIsStoring; ///< If true, this is Serialization process, false means De-Serialization.
273 : : bool mUseMagic; ///< If true, the SerializeableMagic is used within serialization
274 : : bool mUseEmbeddedPoints; ///< If true, the geometry points are saved together with objects
275 : : private: // Special types
276 : : typedef uint8_t EnumSerializationType; ///< Use 8 bits for enum serializations.
277 : : };
278 : :
279 : : /////////////////
280 : : // Implementation
281 : :
282 : 8163 : template <> inline bool ISerializer::write<bool>(bool const &x)
283 : : {
284 [ + + ]: 8163 : uint8_t b = static_cast<uint8_t>(x ? 1 : 0);
285 [ + - ]: 16326 : return write(b);
286 : : }
287 : :
288 : 249849 : template <> inline bool ISerializer::read<bool>(bool &x)
289 : : {
290 : : uint8_t b;
291 [ + - + - ]: 249849 : if (read(b))
292 : : {
293 : 249849 : x = b != 0;
294 : 249849 : return true;
295 : : }
296 : : else
297 : : {
298 : 0 : return false;
299 : : }
300 : : }
301 : :
302 : 188 : template <> inline bool ISerializer::write<std::string>(std::string const &value)
303 : : {
304 : 188 : uint32_t n = static_cast<uint32_t>(value.size());
305 [ + - + - : 188 : return serialize(n) && write(value.c_str(), n);
+ - + - ]
306 : : }
307 : :
308 : 31131 : template <> inline bool ISerializer::read<std::string>(std::string &value)
309 : : {
310 : 31131 : uint32_t n = 0;
311 [ + - + - ]: 31131 : if (serialize(n))
312 : : {
313 [ + - ]: 31131 : char *buffer = new char[n + 1];
314 [ + - ]: 31131 : if (buffer != nullptr)
315 : : {
316 [ + - + - ]: 31131 : if (read(buffer, n))
317 : : {
318 : 31131 : buffer[n] = '\0';
319 [ + - ]: 31131 : value = buffer;
320 [ + - ]: 31131 : delete[] buffer;
321 : 31131 : return true;
322 : : }
323 [ # # ]: 0 : delete[] buffer;
324 : : }
325 : : }
326 : 0 : return false;
327 : : }
328 : :
329 : 31319 : inline bool doSerialize(ISerializer &serializer, std::string &value)
330 : : {
331 [ + - + - : 31319 : return serializer.serialize(SerializeableMagic::String) && serializer.serialize(value);
+ - + - ]
332 : : }
333 : :
334 : 175677 : template <typename T> inline bool ISerializer::write(T const &x)
335 : : {
336 : : #ifndef __GNUC__
337 : : static_assert(std::is_trivially_copyable<T>::value, "Type needs non-trivial serialization!");
338 : : #endif
339 [ + + ]: 175677 : if (std::is_enum<T>())
340 : : {
341 : 25064 : EnumSerializationType v = static_cast<EnumSerializationType>(x);
342 [ + - ]: 25064 : return write(&v, sizeof(v));
343 : : }
344 : : else
345 : : {
346 : 150613 : return write(&x, sizeof(x));
347 : : }
348 : : }
349 : :
350 : 10120068 : template <typename T> inline bool ISerializer::read(T &x)
351 : : {
352 : : #ifndef __GNUC__
353 : : static_assert(std::is_trivially_copyable<T>::value, "Type needs non-trivial serialization!");
354 : : #endif
355 [ + + ]: 10120068 : if (std::is_enum<T>())
356 : : {
357 : : EnumSerializationType v;
358 [ + - + - ]: 707756 : if (read(&v, sizeof(v)))
359 : : {
360 : 707756 : x = static_cast<T>(v);
361 : 707756 : return true;
362 : : }
363 : : else
364 : : {
365 : 0 : return false;
366 : : }
367 : : }
368 : : else
369 : : {
370 : 9412312 : return read(&x, sizeof(x));
371 : : }
372 : : }
373 : :
374 : 9286 : template <typename T> inline bool ISerializer::writeVector(std::vector<T> &x)
375 : : {
376 [ + - + - ]: 9286 : if (serialize(SerializeableMagic::VectorType))
377 : : {
378 : 9286 : size_t n = x.size();
379 [ + - + - ]: 9286 : if (write(n))
380 : : {
381 [ + + ]: 26770 : for (size_t i = 0; i < n; i++)
382 : : {
383 [ + - - + ]: 17484 : if (!write(x[i]))
384 : : {
385 : 9286 : return false;
386 : : }
387 : : }
388 : 9286 : return true;
389 : : }
390 : : }
391 : 0 : return false;
392 : : }
393 : :
394 : 238800 : template <typename T> inline bool ISerializer::readVector(std::vector<T> &x)
395 : : {
396 [ + - + - ]: 238800 : if (serialize(SerializeableMagic::VectorType))
397 : : {
398 : : size_t n;
399 [ + - + - ]: 238800 : if (read(n))
400 : : {
401 [ + + ]: 650118 : for (size_t i = 0; i < n; i++)
402 : : {
403 : : T xi;
404 [ + - - + ]: 411318 : if (!read(xi))
405 : : {
406 : 0 : return false;
407 : : }
408 [ + - ]: 411318 : x.push_back(xi);
409 : : }
410 : 238800 : return true;
411 : : }
412 : : }
413 : 0 : return false;
414 : : }
415 : :
416 : 16945 : template <typename T> inline bool ISerializer::writeObjectVector(std::vector<T> &x, SerializeableMagic const &magic)
417 : : {
418 [ + - ]: 16945 : if (serialize(magic))
419 : : {
420 : 16945 : size_t n = x.size();
421 [ + - + - ]: 16945 : if (write(n))
422 : : {
423 [ + + ]: 38139 : for (size_t i = 0; i < n; i++)
424 : : {
425 [ + - - + ]: 21194 : if (!doSerialize(*this, x[i]))
426 : : {
427 : 16945 : return false;
428 : : }
429 : : }
430 : 16945 : return true;
431 : : }
432 : : }
433 : 0 : return false;
434 : : }
435 : :
436 : 512698 : template <typename T> inline bool ISerializer::readObjectVector(std::vector<T> &x, SerializeableMagic const &magic)
437 : : {
438 [ + - ]: 512698 : if (serialize(magic))
439 : : {
440 : : size_t n;
441 [ + - + - ]: 512698 : if (read(n))
442 : : {
443 [ + + ]: 2124600 : for (size_t i = 0; i < n; i++)
444 : : {
445 [ + - ]: 1611903 : T xi;
446 [ + - + + ]: 1611903 : if (doSerialize(*this, xi))
447 : : {
448 [ + - ]: 1611902 : x.push_back(xi);
449 : : }
450 : : else
451 : : {
452 : 1 : return false;
453 : : }
454 : : }
455 : 512697 : return true;
456 : : }
457 : : }
458 : 0 : return false;
459 : : }
460 : :
461 : 2 : template <typename T, typename U, typename Comp> inline bool ISerializer::writeObjectMap(std::map<T, U, Comp> &x)
462 : : {
463 [ + - + - ]: 2 : if (serialize(SerializeableMagic::ObjectMapType))
464 : : {
465 : 2 : size_t n = x.size();
466 [ + - + - ]: 2 : if (write(n))
467 : : {
468 : : typedef std::map<T, U> Map;
469 [ + + ]: 602 : for (typename Map::iterator it = x.begin(); it != x.end(); ++it)
470 : : {
471 : 600 : T key = it->first;
472 [ + - + - : 600 : if (!doSerialize(*this, key) || !doSerialize(*this, it->second))
+ - - + -
+ ]
473 : : {
474 : 0 : return false;
475 : : }
476 : : }
477 : 2 : return true;
478 : : }
479 : : }
480 : 0 : return false;
481 : : }
482 : :
483 : 2 : template <typename T, typename U, typename Comp> inline bool ISerializer::readObjectMap(std::map<T, U, Comp> &x)
484 : : {
485 [ + - + - ]: 2 : if (serialize(SerializeableMagic::ObjectMapType))
486 : : {
487 : : size_t n;
488 [ + - + - ]: 2 : if (read(n))
489 : : {
490 [ + + ]: 602 : for (size_t i = 0; i < n; i++)
491 : : {
492 : 600 : T first;
493 [ + - + - ]: 600 : if (doSerialize(*this, first))
494 : : {
495 : 600 : U second;
496 [ + - + - ]: 600 : if (doSerialize(*this, second))
497 : : {
498 [ + - ]: 600 : auto insertResult = x.insert({first, second});
499 [ - + ]: 600 : if (!insertResult.second)
500 : : {
501 : 0 : return false;
502 : : }
503 : : }
504 : : else
505 : : {
506 : 0 : return false;
507 : : }
508 : : }
509 : : else
510 : : {
511 : 0 : return false;
512 : : }
513 : : }
514 : 2 : return true;
515 : : }
516 : : }
517 : 0 : return false;
518 : : }
519 : :
520 : : template <typename T, typename U, typename Comp>
521 : 22 : inline bool ISerializer::writeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
522 : : {
523 [ + - + - ]: 22 : if (serialize(SerializeableMagic::ObjectPtrMapType))
524 : : {
525 : 22 : size_t n = x.size();
526 [ + - + - ]: 22 : if (write(n))
527 : : {
528 [ + + ]: 1120 : for (auto it = x.begin(); it != x.end(); it++)
529 : : {
530 : 1098 : T key = it->first;
531 [ + - + - : 1098 : if (!doSerialize(*this, key) || !doSerialize(*this, *it->second))
+ - - + -
+ ]
532 : : {
533 : 0 : return false;
534 : : }
535 : : }
536 : 22 : return true;
537 : : }
538 : : }
539 : 0 : return false;
540 : : }
541 : :
542 : : template <typename T, typename U, typename Comp>
543 : 311 : inline bool ISerializer::readObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
544 : : {
545 [ + - + - ]: 311 : if (serialize(SerializeableMagic::ObjectPtrMapType))
546 : : {
547 : : size_t n;
548 [ + - + - ]: 311 : if (read(n))
549 : : {
550 [ + + ]: 56616 : for (size_t i = 0; i < n; i++)
551 : : {
552 : 56306 : T first;
553 [ + - + - ]: 56306 : if (doSerialize(*this, first))
554 : : {
555 [ + - ]: 56306 : std::shared_ptr<U> second = std::make_shared<U>();
556 [ + - + + ]: 56306 : if (doSerialize(*this, *second))
557 : : {
558 [ + - ]: 56305 : auto insertResult = x.insert({first, second});
559 [ - + ]: 56305 : if (!insertResult.second)
560 : : {
561 : 0 : return false;
562 : : }
563 : : }
564 : : else
565 : : {
566 : 1 : return false;
567 : : }
568 : : }
569 : : else
570 : : {
571 : 0 : return false;
572 : : }
573 : : }
574 : 310 : return true;
575 : : }
576 : : }
577 : 0 : return false;
578 : : }
579 : :
580 : : template <typename T, typename U, typename Comp>
581 : 22 : inline bool ISerializer::writeObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
582 : : {
583 [ + - + - ]: 22 : if (serialize(SerializeableMagic::ObjectVectorMapType))
584 : : {
585 : 22 : size_t n = x.size();
586 [ + - + - ]: 22 : if (write(n))
587 : : {
588 [ + + ]: 39 : for (auto it = x.begin(); it != x.end(); it++)
589 : : {
590 : 17 : T key = it->first;
591 [ + - + - : 17 : if (!doSerialize(*this, key) || !writeObjectVector(it->second))
+ - - + -
+ ]
592 : : {
593 : 0 : return false;
594 : : }
595 : : }
596 : 22 : return true;
597 : : }
598 : : }
599 : 0 : return false;
600 : : }
601 : :
602 : : template <typename T, typename U, typename Comp>
603 : 310 : inline bool ISerializer::readObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
604 : : {
605 [ + - + - ]: 310 : if (serialize(SerializeableMagic::ObjectVectorMapType))
606 : : {
607 : : size_t n;
608 [ + - + - ]: 310 : if (read(n))
609 : : {
610 [ + + ]: 574 : for (size_t i = 0; i < n; i++)
611 : : {
612 : 264 : T first;
613 [ + - + - ]: 264 : if (doSerialize(*this, first))
614 : : {
615 [ + - ]: 264 : auto insertResult = x.insert({first, std::vector<U>()});
616 [ - + ]: 264 : if (!insertResult.second)
617 : : {
618 : 0 : return false;
619 : : }
620 [ + - - + ]: 264 : if (!readObjectVector(insertResult.first->second))
621 : : {
622 : 0 : return false;
623 : : }
624 : : }
625 : : else
626 : : {
627 : 0 : return false;
628 : : }
629 : : }
630 : 310 : return true;
631 : : }
632 : : }
633 : 0 : return false;
634 : : }
635 : :
636 : : } // namespace serialize
637 : : } // namespace map
638 : : } // namespace ad
|