serialization.h
Go to the documentation of this file.
1 #ifndef __SERIALIZATION_H_INCLUDED
2 #define __SERIALIZATION_H_INCLUDED
3 
4 #include <memory>
5 #include <vector>
6 #include <stdint.h>
7 #include <iostream>
8 
9 #ifdef COMPRESSED_SERIALIZATION
10 #include "roslz4/lz4s.h"
11 #endif
12 
13 class Serializer {
14 public:
15  Serializer(const std::vector<char>& data) : data(data) {}
16  explicit Serializer(size_t reserved_size = 0) {
17  if (reserved_size > 0) data.reserve(reserved_size);
18  }
19 
20  void reserve(size_t size) {
21  data.reserve(size);
22  }
23 
24  std::vector<char> result() const {
25  return data;
26  }
27 
28  template<typename Type>
29  void add_value(Type value) {
30  size_t ptr = data.size();
31  data.resize(data.size() + sizeof(Type));
32  *reinterpret_cast<Type*>(data.data() + ptr) = value;
33  }
34 
35  void append(const std::vector<char>& new_data) {
36  data.reserve(data.size() + new_data.size());
37  data.insert(data.end(), new_data.begin(), new_data.end());
38  }
39 
40 #ifdef COMPRESSED_SERIALIZATION
41  std::vector<char> compressed() const {
42  std::vector<char> res;
43  char* output = new char[data.size()];
44  unsigned int output_size = data.size();
45  if (roslz4_buffToBuffCompress(const_cast<char*>(data.data()), data.size(), output, &output_size, 7) == ROSLZ4_OK) {
46  res.assign(output, output + output_size);
47  }
48  delete[] output;
49  return res;
50  }
51 #endif
52 
53 private:
54  std::vector<char> data;
55 };
56 
57 template<typename Type>
58 Serializer& operator<<(Serializer& s, Type value) {
59  s.add_value<Type>(value);
60  return s;
61 }
62 
63 template<>
64 inline Serializer& operator<<(Serializer& s, const std::vector<char>& data) {
65  s.append(data);
66  return s;
67 }
68 
69 class Deserializer {
70 public:
71  Deserializer(const std::vector<char>& data, size_t start_pos = 0) : ptr(start_pos), data(data) {}
72 
73  size_t pos() const {
74  return ptr;
75  }
76 
77  template<typename Type>
78  Type read_value() {
79  Type value = *reinterpret_cast<const Type*>(data.data() + ptr);
80  ptr += sizeof(Type);
81  return value;
82  }
83 
84 #ifdef COMPRESSED_SERIALIZATION
85  static std::vector<char> decompress(const char* data, size_t data_size, size_t expected_size) {
86  std::vector<char> res;
87  char *output = new char[expected_size];
88  unsigned int output_size = expected_size;
89  if (ROSLZ4_OK == roslz4_buffToBuffDecompress(const_cast<char*>(data), data_size, output, &output_size)) {
90  res.reserve(output_size);
91  res.assign(output, output + output_size);
92  }
93  delete[] output;
94  return res;
95  }
96 #endif
97 
98 private:
99  size_t ptr;
100  const std::vector<char>& data;
101 };
102 
103 template<typename Type>
105  value = d.read_value<Type>();
106  return d;
107 }
108 
109 #endif
Serializer & operator<<(Serializer &s, Type value)
Definition: serialization.h:58
void reserve(size_t size)
Definition: serialization.h:20
Serializer(size_t reserved_size=0)
Definition: serialization.h:16
size_t pos() const
Definition: serialization.h:73
Deserializer & operator>>(Deserializer &d, Type &value)
XmlRpcServer s
void append(const std::vector< char > &new_data)
Definition: serialization.h:35
const std::vector< char > & data
std::vector< char > data
Definition: serialization.h:54
int roslz4_buffToBuffCompress(char *input, unsigned int input_size, char *output, unsigned int *output_size, int block_size_id)
Deserializer(const std::vector< char > &data, size_t start_pos=0)
Definition: serialization.h:71
def decompress(data)
const int ROSLZ4_OK
void add_value(Type value)
Definition: serialization.h:29
Type read_value()
Definition: serialization.h:78
std::vector< char > result() const
Definition: serialization.h:24
Serializer(const std::vector< char > &data)
Definition: serialization.h:15
int roslz4_buffToBuffDecompress(char *input, unsigned int input_size, char *output, unsigned int *output_size)


slam_constructor
Author(s): JetBrains Research, OSLL team
autogenerated on Mon Jun 10 2019 15:08:25