libpressio 0.93.0
Loading...
Searching...
No Matches
compressor.h
Go to the documentation of this file.
1#ifndef LIBPRESSIO_COMPRESSOR_IMPL_H
2#define LIBPRESSIO_COMPRESSOR_IMPL_H
3#include <string>
4#include <memory>
5#include "metrics.h"
6#include "configurable.h"
7#include "versionable.h"
8#include "errorable.h"
9#include <std_compat/span.h>
10
15struct pressio_data;
16struct pressio_options;
18
23 public:
24
32 metrics_plugin((plugin.metrics_plugin)?plugin.metrics_plugin->clone(): nullptr)
33 {}
39 {
40 pressio_configurable::operator=(plugin);
41 metrics_plugin = plugin.metrics_plugin->clone();
42 return *this;
43 }
50 metrics_plugin(std::move(plugin.metrics_plugin))
51 {}
57 {
58 pressio_configurable::operator=(plugin);
59 metrics_plugin = std::move(plugin.metrics_plugin);
60 return *this;
61 }
62
63
66
72
85 struct pressio_options get_options() const override final;
86
92 int check_options(struct pressio_options const& options) override final;
93
98 struct pressio_options get_configuration() const override final;
99
104 struct pressio_options get_documentation() const override final;
105
110 int set_options(struct pressio_options const& options) override final;
111
116 int set_metrics_options(struct pressio_options const& options);
117
121 int compress(struct pressio_data const*input, struct pressio_data* output);
127 int decompress(struct pressio_data const*input, struct pressio_data* output);
128
136 template <class InputRandomAccessIterator,
137 class InputRandomAccessIteratorEnd,
138 class OutputRandomAccessIterator,
139 class OutputRandomAccessIteratorEnd
140 >
141 int compress_many(InputRandomAccessIterator in_begin, InputRandomAccessIteratorEnd in_end,
142 OutputRandomAccessIterator out_begin, OutputRandomAccessIteratorEnd out_end) {
143 clear_error();
144 compat::span<const pressio_data* const> inputs(in_begin, in_end);
145 compat::span<pressio_data*> outputs(out_begin, out_end);
146 if(metrics_plugin) {
147 if(metrics_plugin->begin_compress_many(inputs, outputs) != 0 && metrics_errors_fatal) {
148 set_error(metrics_plugin->error_code(), metrics_plugin->error_msg());
149 return error_code();
150 }
151 }
152 auto ret = compress_many_impl(inputs, outputs);
153 if(metrics_plugin) {
154 if(metrics_plugin->end_compress_many(inputs, outputs, ret) != 0 && metrics_errors_fatal) {
155 set_error(metrics_plugin->error_code(), metrics_plugin->error_msg());
156 return error_code();
157 }
158 }
159 return ret;
160 }
168 template <class InputRandomAccessIterator,
169 class InputRandomAccessIteratorEnd,
170 class OutputRandomAccessIterator,
171 class OutputRandomAccessIteratorEnd
172 >
173 int decompress_many(InputRandomAccessIterator in_begin, InputRandomAccessIteratorEnd in_end,
174 OutputRandomAccessIterator out_begin, OutputRandomAccessIteratorEnd out_end) {
175 clear_error();
176 compat::span<const pressio_data* const> inputs(in_begin, in_end);
177 compat::span<pressio_data*> outputs(out_begin, out_end);
178 if(metrics_plugin) {
179 if(metrics_plugin->begin_decompress_many(inputs, outputs) != 0 && metrics_errors_fatal) {
180 set_error(metrics_plugin->error_code(), metrics_plugin->error_msg());
181 return error_code();
182 }
183 }
184 auto ret = decompress_many_impl(inputs, outputs);
185 if(metrics_plugin) {
186 if(metrics_plugin->end_decompress_many(inputs, outputs, ret) != 0 && metrics_errors_fatal) {
187 set_error(metrics_plugin->error_code(), metrics_plugin->error_msg());
188 return error_code();
189 }
190 }
191 return ret;
192 }
193
210 void set_metrics(struct pressio_metrics& plugin);
214 void set_metrics(struct pressio_metrics&& plugin);
215
216
223 virtual std::shared_ptr<libpressio_compressor_plugin> clone()=0;
224
225
229 void set_name(std::string const& new_name) override final;
230
231
232 protected:
245 virtual struct pressio_options get_options_impl() const=0;
250 virtual struct pressio_options get_documentation_impl() const=0;
255 virtual struct pressio_options get_configuration_impl() const=0;
260 virtual int set_options_impl(struct pressio_options const& options)=0;
264 virtual int compress_impl(const pressio_data *input, struct pressio_data* output)=0;
268 virtual int decompress_impl(const pressio_data *input, struct pressio_data* output)=0;
269
275 virtual int check_options_impl(struct pressio_options const&);
276
286
290 virtual int decompress_many_impl(compat::span<const pressio_data* const> const& inputs, compat::span<pressio_data* >& outputs);
291
295 virtual int compress_many_impl(compat::span<const pressio_data* const> const& inputs, compat::span<pressio_data*> & outputs);
296
297 private:
298 pressio_metrics metrics_plugin;
299 std::string metrics_id;
300 int32_t metrics_errors_fatal = 1;
301 int32_t metrics_copy_impl_results = 1;
302};
303
311 pressio_compressor(std::shared_ptr<libpressio_compressor_plugin>&& impl): plugin(std::forward<std::shared_ptr<libpressio_compressor_plugin>>(impl)) {}
320 plugin(compressor->clone()) {}
325 if(&compressor == this) return *this;
326 plugin = compressor->clone();
327 return *this;
328 }
332 pressio_compressor& operator=(pressio_compressor&& compressor) noexcept=default;
337
339 operator bool() const {
340 return bool(plugin);
341 }
342
345 return *plugin;
346 }
347
350 return plugin.get();
351 }
352
356 std::shared_ptr<libpressio_compressor_plugin> plugin;
357};
358
359#endif
Definition: compressor.h:22
libpressio_compressor_plugin(libpressio_compressor_plugin const &plugin)
Definition: compressor.h:30
virtual int set_options_impl(struct pressio_options const &options)=0
virtual int compress_impl(const pressio_data *input, struct pressio_data *output)=0
virtual int check_options_impl(struct pressio_options const &)
libpressio_compressor_plugin & operator=(libpressio_compressor_plugin const &plugin)
Definition: compressor.h:38
virtual int compress_many_impl(compat::span< const pressio_data *const > const &inputs, compat::span< pressio_data * > &outputs)
struct pressio_options get_metrics_results() const
int check_options(struct pressio_options const &options) override final
virtual int decompress_many_impl(compat::span< const pressio_data *const > const &inputs, compat::span< pressio_data * > &outputs)
struct pressio_options get_options() const override final
struct pressio_options get_metrics_options() const
libpressio_compressor_plugin & operator=(libpressio_compressor_plugin &&plugin) noexcept
Definition: compressor.h:56
virtual struct pressio_options get_documentation_impl() const =0
libpressio_compressor_plugin(libpressio_compressor_plugin &&plugin) noexcept
Definition: compressor.h:48
virtual struct pressio_options get_metrics_results_impl() const
struct pressio_options get_documentation() const override final
int set_options(struct pressio_options const &options) override final
int set_metrics_options(struct pressio_options const &options)
void set_metrics(struct pressio_metrics &plugin)
virtual struct pressio_options get_configuration_impl() const =0
int compress(struct pressio_data const *input, struct pressio_data *output)
struct pressio_options get_configuration() const override final
void set_metrics(struct pressio_metrics &&plugin)
struct pressio_metrics && get_metrics() &&
void set_name(std::string const &new_name) override final
virtual struct pressio_options get_options_impl() const =0
int compress_many(InputRandomAccessIterator in_begin, InputRandomAccessIteratorEnd in_end, OutputRandomAccessIterator out_begin, OutputRandomAccessIteratorEnd out_end)
Definition: compressor.h:141
int decompress(struct pressio_data const *input, struct pressio_data *output)
virtual int decompress_impl(const pressio_data *input, struct pressio_data *output)=0
virtual std::shared_ptr< libpressio_compressor_plugin > clone()=0
int decompress_many(InputRandomAccessIterator in_begin, InputRandomAccessIteratorEnd in_end, OutputRandomAccessIterator out_begin, OutputRandomAccessIteratorEnd out_end)
Definition: compressor.h:173
Definition: metrics.h:22
int end_decompress_many(compat::span< const pressio_data *const > const &inputs, compat::span< const pressio_data *const > const &outputs, int rc)
int end_compress_many(compat::span< const pressio_data *const > const &inputs, compat::span< const pressio_data *const > const &outputs, int rc)
virtual std::unique_ptr< libpressio_metrics_plugin > clone()=0
int begin_decompress_many(compat::span< const pressio_data *const > const &inputs, compat::span< const pressio_data *const > const &outputs)
int begin_compress_many(compat::span< const pressio_data *const > const &inputs, compat::span< const pressio_data *const > const &outputs)
Definition: configurable.h:17
const char * error_msg() const
int error_code() const
int set_error(int code, std::string const &msg)
Definition: versionable.h:11
interface for configurable types
interface for types which can report and have errors
an extension header for adding metrics plugins to libpressio
Definition: compressor.h:307
libpressio_compressor_plugin * operator->() const noexcept
Definition: compressor.h:349
pressio_compressor & operator=(pressio_compressor &&compressor) noexcept=default
std::shared_ptr< libpressio_compressor_plugin > plugin
Definition: compressor.h:356
pressio_compressor(pressio_compressor &&compressor)=default
pressio_compressor()=default
pressio_compressor(pressio_compressor const &compressor)
Definition: compressor.h:319
pressio_compressor(std::shared_ptr< libpressio_compressor_plugin > &&impl)
Definition: compressor.h:311
pressio_compressor & operator=(pressio_compressor const &compressor)
Definition: compressor.h:324
libpressio_compressor_plugin & operator*() const noexcept
Definition: compressor.h:344
Definition: data.h:52
Definition: metrics.h:270
Definition: options.h:352
interface for versionable types