libpressio 0.93.0
Loading...
Searching...
No Matches
pressio.h
Go to the documentation of this file.
1
5#ifndef LIBPRESSIO_PRESSIO_IMPL_H
6#define LIBPRESSIO_PRESSIO_IMPL_H
7#include <map>
8#include <memory>
9#include <functional>
10#include "compressor.h"
11#include "metrics.h"
12#include "io.h"
13#include "std_compat/language.h"
14
18template <class T>
26 T build(std::string const& name) const {
27 auto factory = factories.find(name);
28 if ( factory != factories.end()) {
29 return factory->second();
30 } else {
31 return nullptr;
32 }
33 }
34
41 template <class Name, class Factory>
42 void regsiter_factory(Name&& name, Factory&& factory) {
43 factories.emplace(std::forward<Name>(name), std::forward<Factory>(factory));
44 }
45
46 private:
47 std::map<std::string, std::function<T()>> factories;
48
49 public:
51 using value_type = T;
53 using reference = T&;
55 using const_reference = T const;
59 auto begin() const -> decltype(factories.begin()) { return std::begin(factories); }
63 auto end() const -> decltype(factories.end()) { return std::end(factories); }
64
71 bool contains(std::string const& key) const {
72 return factories.find(key) != factories.end();
73 }
74
81 auto find(std::string const& key) const -> decltype(factories.find(key)) {
82 return factories.find(key);
83 }
84
91 auto find(std::string const& key) -> decltype(factories.find(key)) {
92 return factories.find(key);
93 }
94
95 size_t size() const {
96 return factories.size();
97 }
98
99};
100
106 public:
114 template <class RegistryType, class NameType, class Factory>
115 pressio_register(pressio_registry<RegistryType>& registry, NameType&& name, Factory&& factory) {
116 registry.regsiter_factory(name, factory);
117 }
118};
119
128
133
137struct pressio {
138 public:
139
145 void set_error(int code, std::string const& msg) {
146 error.code = code;
147 error.msg = msg;
148 }
149
153 int err_code() const { return error.code; }
154
158 std::string const& err_msg() const { return error.msg; }
159
165 std::shared_ptr<libpressio_compressor_plugin> get_compressor(std::string const& compressor_id) {
166 auto compressor = compressor_plugins().build(compressor_id);
167 if (compressor) return compressor;
168 else {
169 set_error(1, std::string("invalid compressor id ") + compressor_id);
170 return nullptr;
171 }
172 }
173
179 std::shared_ptr<libpressio_io_plugin> get_io(std::string const& io_module_id)
180 {
181 auto io_module = io_plugins().build(io_module_id);
182 if (io_module)
183 return RVO_MOVE(io_module);
184 else {
185 set_error(1, std::string("invalid io_plugin id ") + io_module_id);
186 return nullptr;
187 }
188 }
189
195 template <class Str>
196 std::unique_ptr<libpressio_metrics_plugin> get_metric(Str id) {
197 auto ret = metrics_plugins().build(id);
198 if(!ret) {
199 set_error(2, std::string("failed to construct metrics plugin: ") + id);
200 return nullptr;
201 }
202 return ret;
203 }
204
211 template <class ForwardIt>
212 std::unique_ptr<libpressio_metrics_plugin> get_metrics(ForwardIt first, ForwardIt last) {
213 std::vector<pressio_metrics> plugins;
214
215 for (auto metric = first; metric != last; ++metric) {
216 plugins.emplace_back(metrics_plugins().build(*metric));
217 if(not plugins.back()) {
218 set_error(2, std::string("failed to construct metrics plugin: ") + *metric);
219 return nullptr;
220 }
221 }
222
223 auto metrics = make_m_composite(std::move(plugins));
224
225 if(metrics) return metrics;
226 else {
227 set_error(3, "failed to construct composite metric");
228 return nullptr;
229 };
230 }
231
237 static const char* version();
238
244 static const char* features();
245
251 static const char* supported_compressors();
252
258 static const char* supported_metrics();
259
265 static const char* supported_io();
266
270 static unsigned int major_version();
271
275 static unsigned int minor_version();
276
280 static unsigned int patch_version();
281
282 private:
283 struct {
284 int code;
285 std::string msg;
286 } error;
287};
288#endif
Definition: pressio.h:105
pressio_register(pressio_registry< RegistryType > &registry, NameType &&name, Factory &&factory)
Definition: pressio.h:115
an extension header for adding compressor plugins to libpressio
C++ interface to read and write files.
pressio_registry< std::unique_ptr< libpressio_metrics_plugin > > & metrics_plugins()
pressio_registry< std::unique_ptr< libpressio_io_plugin > > & io_plugins()
pressio_registry< std::shared_ptr< libpressio_compressor_plugin > > & compressor_plugins()
an extension header for adding metrics plugins to libpressio
std::unique_ptr< libpressio_metrics_plugin > make_m_composite(std::vector< pressio_metrics > &&plugins)
Definition: pressio.h:19
void regsiter_factory(Name &&name, Factory &&factory)
Definition: pressio.h:42
T value_type
Definition: pressio.h:51
auto find(std::string const &key) const -> decltype(factories.find(key))
Definition: pressio.h:81
auto find(std::string const &key) -> decltype(factories.find(key))
Definition: pressio.h:91
T build(std::string const &name) const
Definition: pressio.h:26
T const const_reference
Definition: pressio.h:55
bool contains(std::string const &key) const
Definition: pressio.h:71
auto end() const -> decltype(factories.end())
Definition: pressio.h:63
auto begin() const -> decltype(factories.begin())
Definition: pressio.h:59
T & reference
Definition: pressio.h:53
Definition: pressio.h:137
std::shared_ptr< libpressio_compressor_plugin > get_compressor(std::string const &compressor_id)
Definition: pressio.h:165
static const char * features()
static const char * supported_metrics()
std::unique_ptr< libpressio_metrics_plugin > get_metrics(ForwardIt first, ForwardIt last)
Definition: pressio.h:212
static const char * supported_compressors()
static unsigned int major_version()
static const char * supported_io()
static const char * version()
static unsigned int minor_version()
int err_code() const
Definition: pressio.h:153
std::string const & err_msg() const
Definition: pressio.h:158
std::unique_ptr< libpressio_metrics_plugin > get_metric(Str id)
Definition: pressio.h:196
void set_error(int code, std::string const &msg)
Definition: pressio.h:145
static unsigned int patch_version()
std::shared_ptr< libpressio_io_plugin > get_io(std::string const &io_module_id)
Definition: pressio.h:179