libpressio 0.93.0
Loading...
Searching...
No Matches
configurable.h
Go to the documentation of this file.
1#ifndef LIBPRESSIO_CONFIGURABLE_H
2#define LIBPRESSIO_CONFIGURABLE_H
3#include <string>
4#include <utility>
5#include "options.h"
7#include "errorable.h"
8
18 public:
19 virtual ~pressio_configurable()=default;
20 pressio_configurable()=default;
21
23 virtual const char* prefix() const=0;
24
28 std::string const& get_name() const {
29 return name;
30 }
31
36 virtual void set_name(std::string const& new_name) {
37 this->set_name_impl(new_name);
38 this->name = new_name;
39 }
40
46 virtual void set_name_impl(std::string const& new_name) {
47 (void)new_name;
48 }
49
54 virtual struct pressio_options get_configuration() const;
55
60 virtual struct pressio_options get_documentation() const;
61
66 virtual int check_options(struct pressio_options const&);
67
80 virtual struct pressio_options get_options() const;
81
86 virtual int set_options(struct pressio_options const& options);
87
88 protected:
96 template <class StringType>
97 void set(pressio_options& options, StringType const& key, pressio_option const& value) const {
98 if(name.empty()) options.set(key, value);
99 else options.set(name, key, value);
100 }
101
102
110 template <class StringType>
111 void set_type(pressio_options& options, StringType const& key, pressio_option_type type) const {
112 if(name.empty()) options.set_type(key, type);
113 else options.set_type(name, key, type);
114 }
115
116
125 template <class StringType, class PointerType>
127 get(pressio_options const& options, StringType && key, PointerType value) const {
128 if(name.empty()) return options.get(std::forward<StringType>(key), value);
129 return options.get(name, std::forward<StringType>(key), value);
130 }
131
141 template<class StringType, class Wrapper, class... Args>
142 void
143 set_meta(pressio_options& options, StringType&& key, std::string const& current_id, Wrapper const& current_value, Args&&... args) const {
144 set(options, std::forward<StringType>(key), current_id);
145 options.copy_from(current_value->get_options(std::forward<Args>(args)...));
146 }
147
157 template<class StringType, class Wrapper, class... Args>
158 void
159 set_meta_docs(pressio_options& options, StringType&& key, std::string const& docstring, Wrapper const& current_value, Args&&... args) const {
160 set(options, std::forward<StringType>(key), docstring);
161 options.copy_from(current_value->get_documentation(std::forward<Args>(args)...));
162 }
163
173 template<class StringType, class Wrapper, class Registry, class... Args>
174 void
175 set_meta_configuration(pressio_options& options, StringType&& key, Registry const& registry, Wrapper const& current_value) const {
176 std::vector<std::string> plugins;
177 plugins.reserve(registry.size());
178 for (auto const& i : registry) {
179 plugins.emplace_back(i.first);
180 }
181 set(options, std::forward<StringType>(key), plugins);
182 options.copy_from(current_value->get_configuration());
183 }
184
194 template<class StringType, class Wrapper, class Registry, class... Args>
195 void
196 set_meta_many_configuration(pressio_options& options, StringType&& key, Registry const& registry, std::vector<Wrapper> const& current_values) const {
197 std::vector<std::string> plugins;
198 plugins.reserve(registry.size());
199 for (auto const& i : registry) {
200 plugins.emplace_back(i.first);
201 }
202 set(options, std::forward<StringType>(key), plugins);
203 for (auto const& current_value : current_values) {
204 options.copy_from(current_value->get_configuration());
205 }
206 }
207
208
218 template<class StringType, class Wrapper, class... Args>
219 void
220 set_meta_many_docs(pressio_options& options, StringType&& key, std::string const& docstring, std::vector<Wrapper> const& current_values, Args&&... args) const {
221 set(options, std::forward<StringType>(key), docstring);
222 for (auto const& wrapper : current_values) {
223 options.copy_from(wrapper->get_documentation(std::forward<Args>(args)...));
224 }
225 }
226
236 template<class StringType, class Wrapper, class... Args>
237 void
238 set_meta_many(pressio_options& options, StringType&& key, std::vector<std::string> const& current_ids, std::vector<Wrapper> const& current_values, Args&&... args) const {
239 set(options, std::forward<StringType>(key), current_ids);
240 for (auto const& wrapper : current_values) {
241 options.copy_from(wrapper->get_options(std::forward<Args>(args)...));
242 }
243 }
244
255 template <class StringType, class Registry, class Wrapper>
257 get_meta(pressio_options const& options,
258 StringType && key,
259 Registry const& registry,
260 std::string& current_id,
261 Wrapper& current_value) {
262 std::string new_id;
264 if(get(options, std::forward<StringType>(key), &new_id) == pressio_options_key_set) {
265 if (new_id != current_id) {
266 auto new_value = registry.build(new_id);
267 if(new_value) {
268 current_id = std::move(new_id);
269 current_value = std::move(new_value);
271 } else {
273 }
274 if(not get_name().empty()) {
276 }
277 }
278 }
279 current_value->set_options(options);
280 return ret;
281 }
282
293 template <class StringType, class Registry, class Wrapper>
296 StringType && key,
297 Registry const& registry,
298 std::vector<std::string>& current_ids,
299 std::vector<Wrapper>& current_values) {
300 std::vector<std::string> new_ids;
302 if(get(options, std::forward<StringType>(key), &new_ids) == pressio_options_key_set) {
303 if (new_ids != current_ids) {
304 std::vector<Wrapper> new_values;
305 bool all_built = true;
306 for (auto const& new_id : new_ids) {
307 new_values.emplace_back(registry.build(new_id));
308 all_built &= new_values.back();
309 if(not all_built) break;
310 }
311 if(all_built) {
312 current_ids = std::move(new_ids);
313 current_values = std::move(new_values);
315 } else {
317 }
318 if(not get_name().empty()) {
320 }
321 }
322 }
323 for (auto& current_value : current_values) {
324 current_value->set_options(options);
325 }
326 return ret;
327 }
328
335 template <class T>
336 void set_names_many(std::string const& name, std::vector<T>& plugins, std::vector<std::string> const& names) const {
337 if(name.empty()) {
338 for (auto & plugin : plugins) {
339 plugin->set_name(name);
340 }
341 } else {
342 if(not names.empty()) {
343 for (size_t i = 0; i < std::min(plugins.size(), names.size()); ++i) {
344 plugins[i]->set_name(name + "/" + names[i]);
345 }
346 } else {
347 for (auto & plugin : plugins) {
348 plugin->set_name(name + "/" + plugin->prefix());
349 }
350 }
351 }
352 }
353
354 public:
355 int cast_options(pressio_options const& early_config, pressio_options const& config) {
356 set_options(early_config);
357 auto types = get_options();
358 pressio_options casted;
359
360 for (auto const& i : config) {
361 auto status = types.key_status(i.first);
362 switch(status) {
364 {
365 return set_error(1, "option " + i.first + " does not exist");
366 }
367 default:
368 {
369 casted.set(i.first, types.get(i.first));
370 auto cast_status = casted.cast_set(i.first, i.second, pressio_conversion_special);
371 if(cast_status != pressio_options_key_set) {
372 return set_error(1, "option " + i.first + " could not be converted");
373
374 }
375 }
376 }
377 }
378 return set_options(casted);
379 }
380
381
382
383 protected:
384
391 c.get_configuration().get("pressio:thread_safe", &thread_safe);
392 return pressio_thread_safety(thread_safe);
393 }
394
399 std::string get_metrics_key_name() const {
400 return std::string(prefix()) + ":metric";
401 }
402
404 std::string name;
405};
406
407#endif /* end of include guard: LIBPRESSIO_CONFIGURABLE_H */
Definition: configurable.h:17
void set_meta_many_docs(pressio_options &options, StringType &&key, std::string const &docstring, std::vector< Wrapper > const &current_values, Args &&... args) const
Definition: configurable.h:220
void set_names_many(std::string const &name, std::vector< T > &plugins, std::vector< std::string > const &names) const
Definition: configurable.h:336
void set_meta_many(pressio_options &options, StringType &&key, std::vector< std::string > const &current_ids, std::vector< Wrapper > const &current_values, Args &&... args) const
Definition: configurable.h:238
virtual void set_name(std::string const &new_name)
Definition: configurable.h:36
virtual struct pressio_options get_documentation() const
void set_meta_configuration(pressio_options &options, StringType &&key, Registry const &registry, Wrapper const &current_value) const
Definition: configurable.h:175
void set_meta(pressio_options &options, StringType &&key, std::string const &current_id, Wrapper const &current_value, Args &&... args) const
Definition: configurable.h:143
static pressio_thread_safety get_threadsafe(pressio_configurable const &c)
Definition: configurable.h:389
virtual int check_options(struct pressio_options const &)
virtual const char * prefix() const =0
std::string get_metrics_key_name() const
Definition: configurable.h:399
std::string name
Definition: configurable.h:404
virtual struct pressio_options get_configuration() const
virtual void set_name_impl(std::string const &new_name)
Definition: configurable.h:46
enum pressio_options_key_status get(pressio_options const &options, StringType &&key, PointerType value) const
Definition: configurable.h:127
pressio_options_key_status get_meta(pressio_options const &options, StringType &&key, Registry const &registry, std::string &current_id, Wrapper &current_value)
Definition: configurable.h:257
void set(pressio_options &options, StringType const &key, pressio_option const &value) const
Definition: configurable.h:97
void set_meta_many_configuration(pressio_options &options, StringType &&key, Registry const &registry, std::vector< Wrapper > const &current_values) const
Definition: configurable.h:196
pressio_options_key_status get_meta_many(pressio_options const &options, StringType &&key, Registry const &registry, std::vector< std::string > &current_ids, std::vector< Wrapper > &current_values)
Definition: configurable.h:295
void set_type(pressio_options &options, StringType const &key, pressio_option_type type) const
Definition: configurable.h:111
std::string const & get_name() const
Definition: configurable.h:28
void set_meta_docs(pressio_options &options, StringType &&key, std::string const &docstring, Wrapper const &current_value, Args &&... args) const
Definition: configurable.h:159
virtual struct pressio_options get_options() const
virtual int set_options(struct pressio_options const &options)
Definition: errorable.h:13
int set_error(int code, std::string const &msg)
interface for types which can report and have errors
C++ pressio_options and pressio_option interfaces.
Compress, decompress, and configure pressio and lossless compressors.
pressio_thread_safety
Definition: pressio_compressor.h:268
@ pressio_thread_safety_single
Definition: pressio_compressor.h:270
pressio_option_type
Definition: pressio_options.h:57
@ pressio_conversion_special
Definition: pressio_options.h:53
pressio_options_key_status
Definition: pressio_options.h:27
@ pressio_options_key_does_not_exist
Definition: pressio_options.h:35
@ pressio_options_key_set
Definition: pressio_options.h:29
@ pressio_options_key_exists
Definition: pressio_options.h:32
Definition: options.h:80
Definition: options.h:352
void set_type(StringType &&key, pressio_option_type type)
Definition: options.h:476
void set(StringType &&key, pressio_option const &value)
Definition: options.h:423
void copy_from(pressio_options const &o, bool ignore_empty=false)
Definition: options.h:664
enum pressio_options_key_status cast_set(StringType &&key, pressio_option const &value, enum pressio_conversion_safety safety=pressio_conversion_implicit)
Definition: options.h:446
pressio_option const & get(StringType const &key) const
Definition: options.h:496
pressio_options_key_status key_status(std::string const &key) const
Definition: options.h:395