|
ampsci
High-precision calculations for one- and two-valence atomic systems
|
Holds a named list of key=value options and nested InputBlocks.
Parses and stores structured input in the format:
Can be constructed from a literal option list, a string, or a file stream. Block and option name comparison is case-insensitive.
#include <InputBlock.hpp>
Public Member Functions | |
| InputBlock () | |
| Default constructor; name will be blank. | |
| InputBlock (std::string_view name, std::initializer_list< Option > options={}) | |
| Constructs from a literal list of Option structs. | |
| InputBlock (std::string_view name, const std::string &string_input) | |
Constructs by parsing string_input in Block{option=value;} format. | |
| InputBlock (std::string_view name, const std::istream &file) | |
| Constructs by reading and parsing a plain-text file stream. | |
| void | add (InputBlock block, bool merge=false) |
| Appends or merges a child InputBlock. | |
| void | merge (InputBlock block) |
Merges block into an existing block of the same name, or appends it. | |
| void | add (Option option) |
| Appends a single Option to the option list. | |
| void | add (const std::vector< Option > &options) |
| Appends a list of Options to the option list. | |
| void | add (const std::string &string, bool merge=false) |
Parses string and adds its options and blocks. | |
| void | merge (const std::string &string) |
Parses string and merges any duplicate block names. | |
| std::string_view | name () const |
| Returns the name of this block. | |
| const std::vector< Option > & | options () const |
| Returns const reference to the list of options. | |
| const std::vector< InputBlock > & | blocks () const |
| Returns const reference to the list of child blocks. | |
| template<typename T > | |
| T | get (std::string_view key, T default_value) const |
Returns the value of key, or default_value if not found. | |
| template<typename T = std::string> | |
| std::optional< T > | get (std::string_view key) const |
Returns an optional value for key; empty if not found or unset. | |
| bool | has_option (std::string_view key) const |
Returns true if key is present in this block's option list, even if unset. | |
| bool | option_is_set (std::string_view key) const |
Returns true if key is present and has a non-default, non-empty value. | |
| template<typename T > | |
| T | get (std::initializer_list< std::string > blocks, std::string_view key, T default_value) const |
Returns value of key in a sequence of nested blocks, or default_value. | |
| template<typename T > | |
| std::optional< T > | get (std::initializer_list< std::string > blocks, std::string_view key) const |
Returns optional value of key in a sequence of nested blocks; empty if not found. | |
| std::optional< InputBlock > | getBlock (std::string_view name) const |
Returns an optional copy of the child block named name; empty if not found. | |
| std::optional< InputBlock > | getBlock (std::initializer_list< std::string > blocks, std::string_view name) const |
Returns an optional copy of a block found by traversing blocks then looking for name. | |
| InputBlock | get_block (std::string_view name) const |
Returns a copy of the child block named name, or an empty block if not found. | |
| bool | has_block (std::string_view name) const |
Returns true if a child block named name exists in this block. | |
| bool | has_block (std::initializer_list< std::string > blocks, std::string_view name) const |
Returns true if a block named name exists within the given nested blocks. | |
| std::optional< Option > | getOption (std::string_view key) const |
Returns the raw Option struct for key; rarely needed directly. | |
| void | print (std::ostream &os=std::cout, int indent_depth=0) const |
Prints the block contents to os in Block{option=value;} format. | |
| bool | check (std::initializer_list< std::string > blocks, const std::vector< std::pair< std::string, std::string > > &list, bool print=false) const |
| Validates options and sub-blocks against an allowed list. | |
| bool | check (const std::vector< std::pair< std::string, std::string > > &list, bool print=false) const |
Validates options in the current block against list. See check() overload for details. | |
Friends | |
| bool | operator== (InputBlock block, std::string_view name) |
| Equality/inequality compare by block name (case-insensitive). | |
| bool | operator== (std::string_view name, InputBlock block) |
| bool | operator!= (InputBlock block, std::string_view name) |
| bool | operator!= (std::string_view name, InputBlock block) |
|
inline |
Default constructor; name will be blank.
|
inline |
Constructs from a literal list of Option structs.
|
inline |
Constructs by parsing string_input in Block{option=value;} format.
|
inline |
Constructs by reading and parsing a plain-text file stream.
|
inline |
Appends or merges a child InputBlock.
If merge is false (default), always appends block. If merge is true and a block with the same name already exists, the options from block are merged into the existing block instead.
|
inline |
Merges block into an existing block of the same name, or appends it.
|
inline |
Appends a single Option to the option list.
|
inline |
Appends a list of Options to the option list.
|
inline |
Parses string and adds its options and blocks.
Comments, whitespace, and quote marks are stripped before parsing. If merge is true, duplicate block names are consolidated rather than appended.
|
inline |
Parses string and merges any duplicate block names.
|
inline |
Returns the name of this block.
|
inline |
Returns const reference to the list of options.
|
inline |
Returns const reference to the list of child blocks.
| T IO::InputBlock::get | ( | std::string_view | key, |
| T | default_value | ||
| ) | const |
Returns the value of key, or default_value if not found.
If the same key appears more than once, the later occurrence takes precedence. For bool, accepts "true"/"yes"/"y" (case-insensitive). For std::vector<T> or std::array<T,N>, parses a comma-separated list.
| std::optional< T > IO::InputBlock::get | ( | std::string_view | key | ) | const |
Returns an optional value for key; empty if not found or unset.
If the same key appears more than once, the later occurrence takes precedence. A value of "default" or empty string is treated as unset (returns nullopt). Supports T = std::vector<T> or std::array<T,N> for comma-separated list values.
|
inline |
Returns true if key is present in this block's option list, even if unset.
|
inline |
Returns true if key is present and has a non-default, non-empty value.
| T IO::InputBlock::get | ( | std::initializer_list< std::string > | blocks, |
| std::string_view | key, | ||
| T | default_value | ||
| ) | const |
Returns value of key in a sequence of nested blocks, or default_value.
| std::optional< T > IO::InputBlock::get | ( | std::initializer_list< std::string > | blocks, |
| std::string_view | key | ||
| ) | const |
Returns optional value of key in a sequence of nested blocks; empty if not found.
|
inline |
Returns an optional copy of the child block named name; empty if not found.
|
inline |
Returns an optional copy of a block found by traversing blocks then looking for name.
|
inline |
Returns a copy of the child block named name, or an empty block if not found.
|
inline |
Returns true if a child block named name exists in this block.
|
inline |
Returns true if a block named name exists within the given nested blocks.
|
inline |
Returns the raw Option struct for key; rarely needed directly.
|
inline |
Prints the block contents to os in Block{option=value;} format.
|
inline |
Validates options and sub-blocks against an allowed list.
Check one of the sub-blocks.
Checks each option and sub-block in the nested path blocks against list. If any are not found, a warning is printed along with the nearest spelling suggestion, and false is returned. If print is true, the full list of allowed options and descriptions is always printed.
The list entries are pairs of {option_name, description}. Blocks are identified by a trailing {} in the name, e.g., "SubBlock{}". Descriptions support self-documenting input files.
|
inline |
Validates options in the current block against list. See check() overload for details.
|
friend |
Equality/inequality compare by block name (case-insensitive).