lib45d
45Drives C++ Library API Documentation
|
23 #include <45d/Quota.hpp>
24 #include <45d/config/ConfigNode.hpp>
25 #include <45d/config/Exceptions.hpp>
28 #include <unordered_map>
49 T get(
const std::string &key,
50 const std::unordered_map<std::string, ffd::ConfigNode> *config_map) {
52 std::stringstream ss(node.
value_);
53 ss.exceptions(std::ios::failbit | std::ios::badbit);
105 T
get(
const std::string &key)
const {
106 return ffd_internal::get<T>(key, config_map_ptr_);
120 T
get(
const std::string &key,
const T &fallback)
const noexcept {
123 }
catch (
const std::out_of_range &) {
125 }
catch (
const std::ios_base::failure &) {
127 report_error(
"Invalid configuration entry format: " + key +
" = "
128 + config_map_.at(key).value_);
129 }
catch (
const std::out_of_range &) {
130 report_error(
"Invalid configuration entry format for " + key);
133 report_error(e.
what());
134 }
catch (
const std::exception &e) {
135 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
137 report_error(
"Unexplained exception caught while getting " + key);
155 T
get(
const std::string &key,
bool *fail_flag)
const noexcept {
158 }
catch (
const std::ios_base::failure &) {
160 report_error(
"Invalid configuration entry format: " + key +
" = "
161 + config_map_.at(key).value_);
162 }
catch (
const std::out_of_range &) {
163 report_error(
"Invalid configuration entry format for " + key);
165 }
catch (
const std::out_of_range &) {
166 report_error(
"Option not in config: " + key);
168 report_error(e.
what());
169 }
catch (
const std::exception &e) {
170 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
172 report_error(
"Unexplained exception caught while getting " + key);
175 if (std::is_fundamental<T>::value)
190 T
get_from(
const std::string §ion,
const std::string &key) {
193 "Cannot call get_from while ConfigSubsectionGuard is in scope"));
194 set_subsection(section);
195 T result = get<T>(key);
213 T
get_from(
const std::string §ion,
const std::string &key,
const T &fallback) noexcept {
215 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
220 set_subsection(section);
221 }
catch (
const std::out_of_range &) {
225 T result = get<T>(key, fallback);
240 T
get_from(
const std::string §ion,
const std::string &key,
bool *fail_flag) noexcept {
242 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
245 if (std::is_fundamental<T>::value)
251 set_subsection(section);
252 }
catch (
const std::out_of_range &) {
253 std::cerr <<
"Section not in config: " << section << std::endl;
256 if (std::is_fundamental<T>::value)
261 T result = get<T>(key, fail_flag);
282 return Quota(max, get<std::string>(key));
296 }
catch (
const std::out_of_range &) {
298 }
catch (
const std::ios_base::failure &) {
300 report_error(
"Invalid configuration entry format: " + key +
" = "
301 + config_map_.at(key).value_);
302 }
catch (
const std::out_of_range &) {
303 report_error(
"Invalid configuration entry format for " + key);
306 report_error(e.
what());
307 }
catch (
const std::exception &e) {
308 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
310 report_error(
"Unexplained exception caught while getting " + key);
327 }
catch (
const std::ios_base::failure &) {
329 report_error(
"Invalid configuration entry format: " + key +
" = "
330 + config_map_.at(key).value_);
331 }
catch (
const std::out_of_range &) {
332 report_error(
"Invalid configuration entry format for " + key);
334 }
catch (
const std::out_of_range &) {
335 report_error(
"Option not in config: " + key);
337 report_error(e.
what());
338 }
catch (
const std::exception &e) {
339 report_error(
"Unexpected std::exception while getting " + key +
": " + e.what());
341 report_error(
"Unexplained exception caught while getting " + key);
358 "Cannot call get_from while ConfigSubsectionGuard is in scope"));
359 set_subsection(section);
378 const std::string &key,
380 const Quota &fallback) noexcept {
382 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
387 set_subsection(section);
388 }
catch (
const std::out_of_range &) {
407 const std::string &key,
409 bool *fail_flag) noexcept {
411 std::cerr <<
"Cannot call get_from while ConfigSubsectionGuard is in scope"
417 set_subsection(section);
418 }
catch (
const std::out_of_range &) {
419 std::cerr <<
"Section not in config." << std::endl;
438 std::unordered_map<std::string, ConfigNode>
441 std::unordered_map<std::string, ConfigNode>
456 void parse(std::ifstream &file);
462 void parse_entry(
const std::string &line);
473 void parse_heading(
const std::string &line);
479 void set_subsection(
const std::string §ion) {
481 config_map_ptr_ = config_map_.at(section).sub_map_;
482 }
catch (
const std::out_of_range &) {
483 throw std::out_of_range(
"No subconfig with name `" + section +
"`");
485 if (config_map_ptr_ ==
nullptr)
486 throw std::out_of_range(
"`" + section +
"` is not a subsection.");
493 void reset_subsection(
void) noexcept {
494 config_map_ptr_ = &config_map_;
503 void report_error(
const std::string &message)
const noexcept {
504 if (config_map_ptr_ != &config_map_)
506 std::cerr << message << std::endl;
ConfigParser(std::string path)
Construct a new Config Parser object.
Definition: config_parser.cpp:25
Use this class for byte-formatted values. e.g.: "123 KiB".
Definition: Bytes.hpp:32
T get_from(const std::string §ion, const std::string &key)
Adapter for ffd::get(). Sets config_map_ptr_ to address of sub config with name section....
Definition: ConfigParser.hpp:190
Quota get_quota(const std::string &key, const Bytes &max, const Quota &fallback) const noexcept
Try to get Quota from config, default to fallback if fails. Guaranteed no-throw.
Definition: ConfigParser.hpp:293
This class extends ffd::Bytes to specify percents of an amount of bytes.
Definition: Quota.hpp:30
T get(const std::string &key, bool *fail_flag) const noexcept
Try to get value from config. If ffd::get fails, return T() or 0 and set fail_flag....
Definition: ConfigParser.hpp:155
std::string dump_str(void) const
Dump config to stdout as a string.
Definition: config_parser.cpp:79
Use this to switch to a certain config subsection to get a group of values.
Definition: ConfigSubsectionGuard.hpp:49
T get_from(const std::string §ion, const std::string &key, bool *fail_flag) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,bool*) const noexcept,...
Definition: ConfigParser.hpp:240
T get(const std::string &key, const T &fallback) const noexcept
Try to get value from config, default to fallback if fails. Guaranteed no-throw.
Definition: ConfigParser.hpp:120
T get(const std::string &key) const
Get value from config map using ffd::get(). This can throw. Use this in a try...catch block.
Definition: ConfigParser.hpp:105
45Drives namespace
Definition: Bytes.hpp:27
Quota get_quota(const std::string &key, const Bytes &max, bool *fail_flag) const noexcept
Try to get Quota from config. If ffd::get fails, return Quota(void) and set fail_flag....
Definition: ConfigParser.hpp:324
Quota get_quota_from(const std::string §ion, const std::string &key, const Bytes &max, bool *fail_flag) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,bool*) const noexcept,...
Definition: ConfigParser.hpp:406
const char * what(void) const noexcept
Return string containing explanation message.
Definition: Exceptions.hpp:44
Quota get_quota(const std::string &key, const Bytes &max) const
Get quota from config map using ffd::get(). This can throw. Use this in a try...catch block.
Definition: ConfigParser.hpp:281
General exception for all Config* related issues.
Definition: Exceptions.hpp:30
Throw this exception when a ConfigGuard is constructed or get_from() is called when the config is alr...
Definition: Exceptions.hpp:58
std::string current_section_
Name of current section, set by set_subsection()
Definition: ConfigParser.hpp:430
T get_from(const std::string §ion, const std::string &key, const T &fallback) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,const T&) const noexcept,...
Definition: ConfigParser.hpp:213
std::string value_
string from config file after '='
Definition: ConfigNode.hpp:33
std::vector< ConfigNode * > sub_confs_
Vector of config subsections.
Definition: ConfigParser.hpp:429
Quota get_quota_from(const std::string §ion, const std::string &key, const Bytes &max, const Quota &fallback) noexcept
Get value from config subsection using ConfigParser::get(const std::string&,const T&) const noexcept,...
Definition: ConfigParser.hpp:377
Quota get_quota_from(const std::string §ion, const std::string &key, const Bytes &max)
Adapter for ffd::get(). Sets config_map_ptr_ to address of sub config with name section....
Definition: ConfigParser.hpp:355
Main configuration parser class to inherit from in your code.
Definition: ConfigParser.hpp:68
Class for config_map_ entries.
Definition: ConfigNode.hpp:31
Namespace for internal use, not to be exposed to ffd.
Definition: ConfigParser.hpp:37