ad_map_access
ISerializer.hpp
Go to the documentation of this file.
1 // ----------------- BEGIN LICENSE BLOCK ---------------------------------
2 //
3 // Copyright (C) 2018-2021 Intel Corporation
4 //
5 // SPDX-License-Identifier: MIT
6 //
7 // ----------------- END LICENSE BLOCK -----------------------------------
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 
23 
25 namespace ad {
27 namespace map {
29 namespace serialize {
30 
35 {
36 public: // Constructor/Destructor
37  explicit ISerializer(bool store)
38  : mIsStoring(store)
39  , mUseMagic(true)
40  , mUseEmbeddedPoints(true)
41  {
42  }
43 
44  virtual ~ISerializer() = default;
45 
46  bool isStoring() const
47  {
48  return mIsStoring;
49  }
50 
51 public: // Operations
52  template <typename T> bool serialize(T &x)
53  {
54  if (mIsStoring)
55  {
56  return write(x);
57  }
58  else
59  {
60  return read(x);
61  }
62  }
63 
64  bool serialize(SerializeableMagic const &magic)
65  {
66  if (mUseMagic)
67  {
68  if (mIsStoring)
69  {
70  return write(static_cast<uint16_t>(magic));
71  }
72  else
73  {
74  uint16_t magicRead;
75  auto const ok = read(magicRead) && (static_cast<uint16_t>(magic) == magicRead);
76  return ok;
77  }
78  }
79  else
80  {
81  return true;
82  }
83  }
84 
85  template <typename T, typename SerializeBaseType, SerializeableMagic magic> bool serializeGeneratedType(T &x)
86  {
87  if (serialize(magic))
88  {
89  if (mIsStoring)
90  {
91  return write(static_cast<SerializeBaseType>(x));
92  }
93  else
94  {
95  SerializeBaseType xD;
96  if (read(xD))
97  {
98  x = T(xD);
99  return true;
100  }
101  else
102  {
103  return false;
104  }
105  }
106  }
107  return false;
108  }
109 
110  template <typename T> bool serializeVector(std::vector<T> &x)
111  {
112  if (mIsStoring)
113  {
114  return writeVector(x);
115  }
116  else
117  {
118  return readVector(x);
119  }
120  }
121 
122  template <typename T>
123  bool serializeObjectVector(std::vector<T> &x, SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType)
124  {
125  if (mIsStoring)
126  {
127  return writeObjectVector(x, magic);
128  }
129  else
130  {
131  return readObjectVector(x, magic);
132  }
133  }
134 
135  template <typename T, typename U, typename Comp> bool serializeObjectMap(std::map<T, U, Comp> &x)
136  {
137  if (mIsStoring)
138  {
139  return writeObjectMap(x);
140  }
141  else
142  {
143  return readObjectMap(x);
144  }
145  }
146 
147  template <typename T, typename U, typename Comp> bool serializeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
148  {
149  if (mIsStoring)
150  {
151  return writeObjectPtrMap(x);
152  }
153  else
154  {
155  return readObjectPtrMap(x);
156  }
157  }
158 
159  template <typename T, typename U, typename Comp> bool serializeObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
160  {
161  if (mIsStoring)
162  {
163  return writeObjectVecMap(x);
164  }
165  else
166  {
167  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  bool readObjectVector(std::vector<T> &x, SerializeableMagic const &magic = SerializeableMagic::ObjectVectorType);
184  template <typename T>
185  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 
211  bool setUseMagic(bool use_magic)
212  {
213  bool old_magic = mUseMagic;
214  mUseMagic = use_magic;
215  return old_magic;
216  }
217 
229  {
230  bool oldUseEmbeddedPoints = mUseEmbeddedPoints;
231  mUseEmbeddedPoints = useEmbeddedPoints;
232  return oldUseEmbeddedPoints;
233  }
234 
238  bool useEmbeddedPoints() const
239  {
240  return mUseEmbeddedPoints;
241  }
242 
247  {
248  if (!serialize(SerializeableMagic::ObjectVectorMapType))
249  {
250  return false;
251  }
252 
253  size_t n = 0;
254  if (mIsStoring)
255  {
256  if (!write(n))
257  {
258  return false;
259  }
260  }
261  else
262  {
263  if (!read(n))
264  {
265  return false;
266  }
267  }
268  return true;
269  }
270 
271 private: // Data Members
272  bool mIsStoring;
273  bool mUseMagic;
274  bool mUseEmbeddedPoints;
275 private: // Special types
276  typedef uint8_t EnumSerializationType;
277 };
278 
280 // Implementation
281 
282 template <> inline bool ISerializer::write<bool>(bool const &x)
283 {
284  uint8_t b = static_cast<uint8_t>(x ? 1 : 0);
285  return write(b);
286 }
287 
288 template <> inline bool ISerializer::read<bool>(bool &x)
289 {
290  uint8_t b;
291  if (read(b))
292  {
293  x = b != 0;
294  return true;
295  }
296  else
297  {
298  return false;
299  }
300 }
301 
302 template <> inline bool ISerializer::write<std::string>(std::string const &value)
303 {
304  uint32_t n = static_cast<uint32_t>(value.size());
305  return serialize(n) && write(value.c_str(), n);
306 }
307 
308 template <> inline bool ISerializer::read<std::string>(std::string &value)
309 {
310  uint32_t n = 0;
311  if (serialize(n))
312  {
313  char *buffer = new char[n + 1];
314  if (buffer != nullptr)
315  {
316  if (read(buffer, n))
317  {
318  buffer[n] = '\0';
319  value = buffer;
320  delete[] buffer;
321  return true;
322  }
323  delete[] buffer;
324  }
325  }
326  return false;
327 }
328 
329 inline bool doSerialize(ISerializer &serializer, std::string &value)
330 {
331  return serializer.serialize(SerializeableMagic::String) && serializer.serialize(value);
332 }
333 
334 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  if (std::is_enum<T>())
340  {
341  EnumSerializationType v = static_cast<EnumSerializationType>(x);
342  return write(&v, sizeof(v));
343  }
344  else
345  {
346  return write(&x, sizeof(x));
347  }
348 }
349 
350 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  if (std::is_enum<T>())
356  {
357  EnumSerializationType v;
358  if (read(&v, sizeof(v)))
359  {
360  x = static_cast<T>(v);
361  return true;
362  }
363  else
364  {
365  return false;
366  }
367  }
368  else
369  {
370  return read(&x, sizeof(x));
371  }
372 }
373 
374 template <typename T> inline bool ISerializer::writeVector(std::vector<T> &x)
375 {
376  if (serialize(SerializeableMagic::VectorType))
377  {
378  size_t n = x.size();
379  if (write(n))
380  {
381  for (size_t i = 0; i < n; i++)
382  {
383  if (!write(x[i]))
384  {
385  return false;
386  }
387  }
388  return true;
389  }
390  }
391  return false;
392 }
393 
394 template <typename T> inline bool ISerializer::readVector(std::vector<T> &x)
395 {
396  if (serialize(SerializeableMagic::VectorType))
397  {
398  size_t n;
399  if (read(n))
400  {
401  for (size_t i = 0; i < n; i++)
402  {
403  T xi;
404  if (!read(xi))
405  {
406  return false;
407  }
408  x.push_back(xi);
409  }
410  return true;
411  }
412  }
413  return false;
414 }
415 
416 template <typename T> inline bool ISerializer::writeObjectVector(std::vector<T> &x, SerializeableMagic const &magic)
417 {
418  if (serialize(magic))
419  {
420  size_t n = x.size();
421  if (write(n))
422  {
423  for (size_t i = 0; i < n; i++)
424  {
425  if (!doSerialize(*this, x[i]))
426  {
427  return false;
428  }
429  }
430  return true;
431  }
432  }
433  return false;
434 }
435 
436 template <typename T> inline bool ISerializer::readObjectVector(std::vector<T> &x, SerializeableMagic const &magic)
437 {
438  if (serialize(magic))
439  {
440  size_t n;
441  if (read(n))
442  {
443  for (size_t i = 0; i < n; i++)
444  {
445  T xi;
446  if (doSerialize(*this, xi))
447  {
448  x.push_back(xi);
449  }
450  else
451  {
452  return false;
453  }
454  }
455  return true;
456  }
457  }
458  return false;
459 }
460 
461 template <typename T, typename U, typename Comp> inline bool ISerializer::writeObjectMap(std::map<T, U, Comp> &x)
462 {
463  if (serialize(SerializeableMagic::ObjectMapType))
464  {
465  size_t n = x.size();
466  if (write(n))
467  {
468  typedef std::map<T, U> Map;
469  for (typename Map::iterator it = x.begin(); it != x.end(); ++it)
470  {
471  T key = it->first;
472  if (!doSerialize(*this, key) || !doSerialize(*this, it->second))
473  {
474  return false;
475  }
476  }
477  return true;
478  }
479  }
480  return false;
481 }
482 
483 template <typename T, typename U, typename Comp> inline bool ISerializer::readObjectMap(std::map<T, U, Comp> &x)
484 {
485  if (serialize(SerializeableMagic::ObjectMapType))
486  {
487  size_t n;
488  if (read(n))
489  {
490  for (size_t i = 0; i < n; i++)
491  {
492  T first;
493  if (doSerialize(*this, first))
494  {
495  U second;
496  if (doSerialize(*this, second))
497  {
498  auto insertResult = x.insert({first, second});
499  if (!insertResult.second)
500  {
501  return false;
502  }
503  }
504  else
505  {
506  return false;
507  }
508  }
509  else
510  {
511  return false;
512  }
513  }
514  return true;
515  }
516  }
517  return false;
518 }
519 
520 template <typename T, typename U, typename Comp>
521 inline bool ISerializer::writeObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
522 {
523  if (serialize(SerializeableMagic::ObjectPtrMapType))
524  {
525  size_t n = x.size();
526  if (write(n))
527  {
528  for (auto it = x.begin(); it != x.end(); it++)
529  {
530  T key = it->first;
531  if (!doSerialize(*this, key) || !doSerialize(*this, *it->second))
532  {
533  return false;
534  }
535  }
536  return true;
537  }
538  }
539  return false;
540 }
541 
542 template <typename T, typename U, typename Comp>
543 inline bool ISerializer::readObjectPtrMap(std::map<T, std::shared_ptr<U>, Comp> &x)
544 {
545  if (serialize(SerializeableMagic::ObjectPtrMapType))
546  {
547  size_t n;
548  if (read(n))
549  {
550  for (size_t i = 0; i < n; i++)
551  {
552  T first;
553  if (doSerialize(*this, first))
554  {
555  std::shared_ptr<U> second = std::make_shared<U>();
556  if (doSerialize(*this, *second))
557  {
558  auto insertResult = x.insert({first, second});
559  if (!insertResult.second)
560  {
561  return false;
562  }
563  }
564  else
565  {
566  return false;
567  }
568  }
569  else
570  {
571  return false;
572  }
573  }
574  return true;
575  }
576  }
577  return false;
578 }
579 
580 template <typename T, typename U, typename Comp>
581 inline bool ISerializer::writeObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
582 {
583  if (serialize(SerializeableMagic::ObjectVectorMapType))
584  {
585  size_t n = x.size();
586  if (write(n))
587  {
588  for (auto it = x.begin(); it != x.end(); it++)
589  {
590  T key = it->first;
591  if (!doSerialize(*this, key) || !writeObjectVector(it->second))
592  {
593  return false;
594  }
595  }
596  return true;
597  }
598  }
599  return false;
600 }
601 
602 template <typename T, typename U, typename Comp>
603 inline bool ISerializer::readObjectVecMap(std::map<T, std::vector<U>, Comp> &x)
604 {
605  if (serialize(SerializeableMagic::ObjectVectorMapType))
606  {
607  size_t n;
608  if (read(n))
609  {
610  for (size_t i = 0; i < n; i++)
611  {
612  T first;
613  if (doSerialize(*this, first))
614  {
615  auto insertResult = x.insert({first, std::vector<U>()});
616  if (!insertResult.second)
617  {
618  return false;
619  }
620  if (!readObjectVector(insertResult.first->second))
621  {
622  return false;
623  }
624  }
625  else
626  {
627  return false;
628  }
629  }
630  return true;
631  }
632  }
633  return false;
634 }
635 
636 } // namespace serialize
637 } // namespace map
638 } // namespace ad
ad
namespace ad
Definition: GeometryStoreItem.hpp:28
ad::map::serialize::ISerializer::useEmbeddedPoints
bool useEmbeddedPoints() const
retunrs the setting if embedded points should be used or not on serialization
Definition: ISerializer.hpp:238
ad::map::serialize::ISerializer
Interface for Serializer.
Definition: ISerializer.hpp:34
ad::map::serialize::ISerializer::setUseEmbeddedPoints
bool setUseEmbeddedPoints(bool useEmbeddedPoints)
Specifies if the geometry points should be embedded within the objects or handled separately.
Definition: ISerializer.hpp:228
ad::map::serialize::SerializeableMagic
SerializeableMagic
Declaration of class-specific Magic Numbers.
Definition: SerializeableMagic.hpp:26
ad::map::serialize::ISerializer::setUseMagic
bool setUseMagic(bool use_magic)
Specifies if the every serialized block will be/is prefixed with object-specific magic number....
Definition: ISerializer.hpp:211
ad::map::serialize::ISerializer::serializeEmptyObjectVec
bool serializeEmptyObjectVec()
Definition: ISerializer.hpp:246
SerializeableMagic.hpp