$darkmode
output_serializer.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
22 #pragma once
23 #ifndef CLOE_UTILITY_OUTPUT_SERIALIZER_HPP_
24 #define CLOE_UTILITY_OUTPUT_SERIALIZER_HPP_
25 
26 #include <fstream> // for ofstream
27 #include <string> // for string
28 #include <vector> // for vector<>
29 
30 #include <boost/algorithm/string/join.hpp> // for boost::algorithm::join
31 #include <boost/assign/list_of.hpp> // for list_of
32 #include <boost/bimap.hpp> // for bimap
33 #include <boost/iostreams/copy.hpp> // for copy
34 #include <boost/iostreams/filter/bzip2.hpp> // for bzip2_compressor
35 #include <boost/iostreams/filter/gzip.hpp> // for gzip_compressor
36 #include <boost/iostreams/filtering_streambuf.hpp> // for filtering_streambuf
37 #include <boost/range/adaptor/map.hpp> // for boost::adaptors::map
38 #include <boost/range/algorithm/copy.hpp> // for boost::algorithm::copy
39 
40 #include <cloe/core.hpp> // for Logger
41 
42 namespace cloe {
43 namespace utility {
44 
45 class OutputStream {
46  public:
47  using char_iterator = std::vector<char>::iterator;
48  using uint8_iterator = std::vector<uint8_t>::iterator;
49 
50  explicit OutputStream(Logger logger) : logger_(logger) {}
51  virtual ~OutputStream() = default;
52  virtual std::string make_default_filename(const std::string& default_filename) = 0;
53  virtual bool open_stream() = 0;
54  virtual void write(const char* s, std::streamsize count) = 0;
55  virtual void close_stream() = 0;
56 
57  protected:
58  Logger logger_;
59 };
60 
61 template <typename... TSerializerArgs>
62 class Serializer {
63  public:
64  Serializer(void (OutputStream::*write_function)(const char*, std::streamsize),
65  OutputStream* instance)
66  : write_function_(write_function), instance_(instance) {}
67  virtual ~Serializer() = default;
68  virtual std::string make_default_filename(const std::string& default_filename) = 0;
69  virtual void start_array() = 0;
70  virtual void serialize(TSerializerArgs... args) = 0;
71  virtual void end_array() = 0;
72 
73  protected:
74  void write(const std::string& str) { (instance_->*write_function_)(str.c_str(), str.length()); }
75  void write(const std::vector<uint8_t>& data) {
76  (instance_->*write_function_)((const char*)data.data(), data.size());
77  }
78  void (OutputStream::*write_function_)(const char*, std::streamsize);
79  OutputStream* instance_;
80 
81  template <typename TSerializer, typename TOutputStream>
82  friend class GndTruthSerializerImpl;
83 };
84 
86  public:
87  using OutputStream::OutputStream;
88  virtual ~BasicFileOutputStream() = default;
89  bool open_stream() final { return false; }
90  virtual bool open_file(const std::string& filename, const std::string& default_filename);
91  void write(const char* s, std::streamsize count) override { ofs_.write(s, count); }
92  void close_stream() override;
93 
94  protected:
95  std::ofstream ofs_; // output file stream
96 };
97 
99  public:
100  using BasicFileOutputStream::BasicFileOutputStream;
101  virtual ~FileOutputStream() = default;
102  std::string make_default_filename(const std::string& default_filename) override {
103  return default_filename;
104  }
105 };
106 
108  public:
109  explicit FilteringOutputStream(Logger logger)
110  : BasicFileOutputStream(logger), filter_(), out_(&filter_) {}
111  virtual ~FilteringOutputStream() = default;
112  bool open_file(const std::string& filename, const std::string& default_filename) override;
113  void write(const char* s, std::streamsize count) override { out_.write(s, count); }
114  void close_stream() override;
115 
116  protected:
117  virtual void configure_filter() = 0;
118 
119  protected:
120  boost::iostreams::filtering_streambuf<boost::iostreams::output> filter_;
121  std::ostream out_;
122 };
123 
125  public:
126  using FilteringOutputStream::FilteringOutputStream;
127  virtual ~ZlibOutputStream() = default;
128  std::string make_default_filename(const std::string& default_filename) override {
129  return default_filename + ".zip";
130  }
131 
132  protected:
133  void configure_filter() override {
134  filter_.push(boost::iostreams::gzip_compressor(
135  boost::iostreams::gzip_params(boost::iostreams::gzip::best_compression)));
136  }
137 };
138 
140  public:
141  using FilteringOutputStream::FilteringOutputStream;
142  virtual ~GzipOutputStream() = default;
143  std::string make_default_filename(const std::string& default_filename) override {
144  return default_filename + ".gz";
145  }
146 
147  protected:
148  void configure_filter() override {
149  filter_.push(boost::iostreams::gzip_compressor(
150  boost::iostreams::gzip_params(boost::iostreams::gzip::best_compression)));
151  }
152 };
153 
155  public:
156  using FilteringOutputStream::FilteringOutputStream;
157  virtual ~Bzip2OutputStream() = default;
158  std::string make_default_filename(const std::string& default_filename) override {
159  return default_filename + ".bz2";
160  }
161 
162  protected:
163  void configure_filter() override {
164  filter_.push(boost::iostreams::bzip2_compressor(
165  boost::iostreams::bzip2_params(boost::iostreams::bzip2::default_block_size)));
166  }
167 };
168 
170 template <typename TSerializer, typename TOutputStream, typename... TSerializerArgs>
172  public:
173  explicit FileSerializer(Logger logger)
174  : outputstream_(logger)
175  , serializer_((void (OutputStream::*)(const char*, std::streamsize)) & TOutputStream::write,
176  &outputstream_) {}
177  virtual ~FileSerializer() = default;
178  virtual void open_file(const std::string& filename, const std::string& default_filename) {
179  outputstream_.open_file(filename, default_filename);
180  }
181  virtual void serialize(TSerializerArgs... args) { serializer_.serialize(args...); }
182  virtual void close_file() { outputstream_.close_stream(); }
183 
184  protected:
185  TOutputStream outputstream_;
186  TSerializer serializer_;
187 };
188 
190 template <typename TSerializer, typename TOutputStream, typename... TSerializerArgs>
192  : public FileSerializer<TSerializer, TOutputStream, TSerializerArgs...> {
193  using base = FileSerializer<TSerializer, TOutputStream, TSerializerArgs...>;
194 
195  public:
196  using base::base;
197  void open_file(const std::string& filename, const std::string& default_filename) override {
198  base::open_file(filename, default_filename);
199  on_file_opened();
200  }
201  void close_file() override {
202  on_file_closing();
203  base::close_file();
204  }
205 
206  protected:
207  virtual void on_file_opened() = 0;
208  virtual void on_file_closing() = 0;
209 };
210 
211 } // namespace utility
212 } // namespace cloe
213 
214 #endif // CLOE_UTILITY_OUTPUT_SERIALIZER_HPP_
Definition: output_serializer.hpp:45
SequentialFileSerializer is a FileSerializer for sequences of objects of the same type...
Definition: output_serializer.hpp:191
FileSerializer composes a TSerializer with a TOutputStream.
Definition: output_serializer.hpp:171
Definition: output_serializer.hpp:107
Definition: coordinator.hpp:36
Definition: output_serializer.hpp:98
Definition: output_serializer.hpp:85
Definition: enum_test.cpp:29
Definition: output_serializer.hpp:124
Definition: output_serializer.hpp:139
Definition: output_serializer.hpp:154
Definition: output_serializer.hpp:62