ObjectToIceBlob.h
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarXCore
19 * @author Raphael Grimm ( raphael dot grimm at kit dot edu)
20 * @date 2020
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#pragma once
25
26#include <sstream>
27#include <string_view>
28
29#include <boost/iostreams/copy.hpp>
30#include <boost/iostreams/filter/gzip.hpp>
31#include <boost/iostreams/filtering_streambuf.hpp>
32
33#include <Ice/OutputStream.h>
34
35//raw
36namespace armarx
37{
38 template <class T>
40 {
42 {
43 serialize(obj);
44 }
45
47
48 void
49 serialize(const T& obj)
50 {
51 _out.clear();
52 _out.write(obj);
53 std::tie(_begin, _end) = _out.finished();
54 }
55
56 void
58 {
59 _out.clear();
60 _begin = nullptr;
61 _end = nullptr;
62 }
63
64 const Ice::Byte*
65 begin() const
66 {
67 return _begin;
68 }
69
70 const Ice::Byte*
71 end() const
72 {
73 return _end;
74 }
75
76 std::ptrdiff_t
77 size() const
78 {
79 return _end - _begin;
80 }
81
82 private:
83 Ice::OutputStream _out;
84 const Ice::Byte* _begin{nullptr};
85 const Ice::Byte* _end{nullptr};
86 };
87} // namespace armarx
88
89//compressed
90namespace armarx
91{
92 template <class T>
94 {
96 {
97 serialize(obj);
98 }
99
101
102 void
103 serialize(const T& obj)
104 {
105 _ser.serialize(obj);
106 const std::string_view sv(reinterpret_cast<const char*>(_ser.begin()), _ser.size());
107
108 std::stringstream istr;
109 istr << sv;
110 boost::iostreams::filtering_istreambuf in;
111 in.push(boost::iostreams::gzip_compressor());
112 in.push(istr);
113 std::stringstream ostr;
114 boost::iostreams::copy(in, ostr);
115
116 _ser.clear();
117 _str = ostr.str();
118 }
119
120 void
122 {
123 _ser.clear();
124 _str.clear();
125 }
126
127 const char*
128 begin() const
129 {
130 return _str.data();
131 }
132
133 std::size_t
134 size() const
135 {
136 return _str.size();
137 }
138
139 const char*
140 end() const
141 {
142 return begin() + size();
143 }
144
145 private:
147 std::string _str;
148 };
149} // namespace armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
const Ice::Byte * end() const
const Ice::Byte * begin() const