toml::parse_result class final

The result of a parsing operation.

A parse_result is effectively a discriminated union containing either a toml::table or a toml::parse_error. Most member functions assume a particular one of these two states, and calling them when in the wrong state will cause errors (e.g. attempting to access the error object when parsing was successful).

parse_result result = toml::parse_file("config.toml");
if (result)
    do_stuff_with_a_table(result); //implicitly converts to table&
else
    std::cerr << "Parse failed:\n"sv << result.error() << std::endl;
example output:

Parse failed:
Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
    (error occurred at line 1, column 13 of 'config.toml')

Getting node_views (operator[]) and using the iterator accessor functions (begin(), end() etc.) are unconditionally safe; when parsing fails these just return 'empty' values. A ranged-for loop on a failed parse_result is also safe since begin() and end() return the same iterator and will not lead to any dereferences and iterations.

Public types

using const_iterator = const_table_iterator
A BidirectionalIterator for iterating over const key-value pairs in a wrapped toml::table.
using iterator = table_iterator
A BidirectionalIterator for iterating over key-value pairs in a wrapped toml::table.

Constructors, destructors, conversion operators

parse_result(parse_result&& res) noexcept
Move constructor.
~parse_result() noexcept
Destructor.
operator bool() const explicit noexcept
Returns true if parsing succeeeded.
operator const parse_error&() const explicit noexcept
Returns the internal toml::parse_error (const lvalue overload).
operator const table&() const noexcept
Returns the internal toml::table (const lvalue overload).
operator parse_error&() explicit noexcept
Returns the internal toml::parse_error.
operator parse_error&&() explicit noexcept
Returns the internal toml::parse_error (rvalue overload).
operator table&() noexcept
Returns the internal toml::table.
operator table&&() noexcept
Returns the internal toml::table (rvalue overload).

Public functions

auto begin() const -> const_table_iterator noexcept
Returns an iterator to the first key-value pair in the wrapped table.
auto begin() -> table_iterator noexcept
Returns an iterator to the first key-value pair in the wrapped table.
auto cbegin() const -> const_table_iterator noexcept
Returns an iterator to the first key-value pair in the wrapped table.
auto cend() const -> const_table_iterator noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
auto end() const -> const_table_iterator noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
auto end() -> table_iterator noexcept
Returns an iterator to one-past-the-last key-value pair in the wrapped table.
auto error() && -> parse_error&& noexcept
Returns the internal toml::parse_error (rvalue overload).
auto error() & -> parse_error& noexcept
Returns the internal toml::parse_error.
auto error() const & -> const parse_error& noexcept
Returns the internal toml::parse_error (const lvalue overload).
auto failed() const -> bool noexcept
Returns true if parsing failed.
auto get() && -> table&& noexcept
Returns the internal toml::table (rvalue overload).
auto get() & -> table& noexcept
Returns the internal toml::table.
auto get() const & -> const table& noexcept
Returns the internal toml::table (const lvalue overload).
auto operator=(parse_result&& rhs) -> parse_result& noexcept
Move-assignment operator.
auto operator[](string_view key) const -> node_view<const node> noexcept
Gets a node_view for the selected key-value pair in the wrapped table (const overload).
auto operator[](string_view key) -> node_view<node> noexcept
Gets a node_view for the selected key-value pair in the wrapped table.
auto succeeded() const -> bool noexcept
Returns true if parsing succeeeded.

Function documentation

const_table_iterator toml::parse_result::begin() const noexcept

Returns an iterator to the first key-value pair in the wrapped table.

table_iterator toml::parse_result::begin() noexcept

Returns an iterator to the first key-value pair in the wrapped table.

const_table_iterator toml::parse_result::cbegin() const noexcept

Returns an iterator to the first key-value pair in the wrapped table.

const_table_iterator toml::parse_result::cend() const noexcept

Returns an iterator to one-past-the-last key-value pair in the wrapped table.

const_table_iterator toml::parse_result::end() const noexcept

Returns an iterator to one-past-the-last key-value pair in the wrapped table.

table_iterator toml::parse_result::end() noexcept

Returns an iterator to one-past-the-last key-value pair in the wrapped table.

node_view<node> toml::parse_result::operator[](string_view key) noexcept

Gets a node_view for the selected key-value pair in the wrapped table.

Parameters
key The key used for the lookup.
Returns A view of the value at the given key if parsing was successful and a matching key existed, or an empty node view.