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 <string_view>
27 #include <sstream>
28 
29 #include <boost/iostreams/filtering_streambuf.hpp>
30 #include <boost/iostreams/copy.hpp>
31 #include <boost/iostreams/filter/gzip.hpp>
32 
33 #include <Ice/OutputStream.h>
34 
35 //raw
36 namespace armarx
37 {
38  template<class T>
40  {
42  {
43  serialize(obj);
44  }
45  ObjectToIceBlobSerializer() = default;
46 
47  void serialize(const T& obj)
48  {
49  _out.clear();
50  _out.write(obj);
51  std::tie(_begin, _end) = _out.finished();
52  }
53 
54  void clear()
55  {
56  _out.clear();
57  _begin = nullptr;
58  _end = nullptr;
59  }
60 
61  const Ice::Byte* begin() const
62  {
63  return _begin;
64  }
65  const Ice::Byte* end() const
66  {
67  return _end;
68  }
69 
70  std::ptrdiff_t size() const
71  {
72  return _end - _begin;
73  }
74  private:
75  Ice::OutputStream _out;
76  const Ice::Byte* _begin{nullptr};
77  const Ice::Byte* _end{nullptr};
78  };
79 }
80 
81 //compressed
82 namespace armarx
83 {
84  template<class T>
86  {
88  {
89  serialize(obj);
90  }
92 
93  void serialize(const T& obj)
94  {
95  _ser.serialize(obj);
96  const std::string_view sv(reinterpret_cast<const char*>(_ser.begin()), _ser.size());
97 
98  std::stringstream istr;
99  istr << sv;
100  boost::iostreams::filtering_istreambuf in;
101  in.push(boost::iostreams::gzip_compressor());
102  in.push(istr);
103  std::stringstream ostr;
104  boost::iostreams::copy(in, ostr);
105 
106  _ser.clear();
107  _str = ostr.str();
108  }
109 
110  void clear()
111  {
112  _ser.clear();
113  _str.clear();
114  }
115 
116  const char* begin() const
117  {
118  return _str.data();
119  }
120 
121  std::size_t size() const
122  {
123  return _str.size();
124  }
125 
126  const char* end() const
127  {
128  return begin() + size();
129  }
130 
131  private:
133  std::string _str;
134  };
135 }
armarx::ObjectToIceBlobSerializer::begin
const Ice::Byte * begin() const
Definition: ObjectToIceBlob.h:61
armarx::ObjectToCompressedIceBlobSerializer
Definition: ObjectToIceBlob.h:85
armarx::ObjectToCompressedIceBlobSerializer::size
std::size_t size() const
Definition: ObjectToIceBlob.h:121
armarx::ObjectToCompressedIceBlobSerializer::serialize
void serialize(const T &obj)
Definition: ObjectToIceBlob.h:93
armarx::ObjectToCompressedIceBlobSerializer::clear
void clear()
Definition: ObjectToIceBlob.h:110
armarx::ObjectToCompressedIceBlobSerializer::begin
const char * begin() const
Definition: ObjectToIceBlob.h:116
armarx::ObjectToIceBlobSerializer::ObjectToIceBlobSerializer
ObjectToIceBlobSerializer(const T &obj)
Definition: ObjectToIceBlob.h:41
armarx::ObjectToIceBlobSerializer::serialize
void serialize(const T &obj)
Definition: ObjectToIceBlob.h:47
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::ObjectToCompressedIceBlobSerializer::ObjectToCompressedIceBlobSerializer
ObjectToCompressedIceBlobSerializer()=default
armarx::ObjectToIceBlobSerializer
Definition: ObjectToIceBlob.h:39
armarx::ObjectToCompressedIceBlobSerializer::end
const char * end() const
Definition: ObjectToIceBlob.h:126
armarx::ObjectToIceBlobSerializer::end
const Ice::Byte * end() const
Definition: ObjectToIceBlob.h:65
armarx::ObjectToIceBlobSerializer::size
std::ptrdiff_t size() const
Definition: ObjectToIceBlob.h:70
armarx::ObjectToCompressedIceBlobSerializer::ObjectToCompressedIceBlobSerializer
ObjectToCompressedIceBlobSerializer(const T &obj)
Definition: ObjectToIceBlob.h:87
armarx::ObjectToIceBlobSerializer::ObjectToIceBlobSerializer
ObjectToIceBlobSerializer()=default
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::ObjectToIceBlobSerializer::clear
void clear()
Definition: ObjectToIceBlob.h:54
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28