template<typename T>
toml::node_view class

A view of a node.

A node_view is like a std::optional<toml::node> with lots of toml-specific stuff built-in. It may represent a node, and allows you to do many of the same operations that you'd do on nodes directly, as well as easily traversing the node tree by creating subviews (via node_view::operator[]).

auto tbl = toml::parse(R"(

    title = "my hardware store"
    
    [[products]]
    name = "Hammer"
    sku = 738594937
    keywords = [ "hammer", "construction", "build" ]

    [[products]]
    name = "Nail"
    sku = 284758393
    color = "gray"
    
)"sv);

std::cout << tbl["title"] << std::endl;
std::cout << tbl["products"][0]["name"] << std::endl;
std::cout << tbl["products"][0]["keywords"] << std::endl;
std::cout << tbl["products"][0]["keywords"][2] << std::endl;

tbl["products"][0]["keywords"].as_array()->push_back("heavy");
std::cout << tbl["products"][0]["keywords"] << std::endl;
std::cout << "has product[2]: "sv << !!tbl["products"][2] << std::endl;
std::cout << "product[2]: "sv << tbl["products"][2] << std::endl; */
"my hardware store"
"Hammer"
[ "hammer", "construction", "build" ]
"build"
[ "hammer", "construction", "build", "heavy" ]
has product[2]: false
product[2]:

Constructors, destructors, conversion operators

node_view() defaulted noexcept
Constructs an empty node view.
operator bool() const explicit noexcept
Returns true if the view references a node.

Public functions

template<typename U>
auto as() const -> auto noexcept
Gets a pointer to the viewed node as a more specific node type.
auto as_array() const -> auto noexcept
Returns a pointer to the viewed node as a toml::array, if it is one.
auto as_boolean() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<bool>, if it is one.
auto as_date() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<date>, if it is one.
auto as_date_time() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<date_time>, if it is one.
auto as_floating_point() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<double>, if it is one.
auto as_integer() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<int64_t>, if it is one.
auto as_string() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<string>, if it is one.
auto as_table() const -> auto noexcept
Returns a pointer to the viewed node as a toml::table, if it is one.
auto as_time() const -> auto noexcept
Returns a pointer to the viewed node as a toml::value<time>, if it is one.
auto get() const -> viewed_type* noexcept
Returns the node that's being referenced by the view.
template<typename U>
auto is() const -> bool noexcept
Checks if this view references a node of a specific type.
auto is_array() const -> bool noexcept
Returns true if the viewed node is a toml::array.
auto is_array_of_tables() const -> bool noexcept
Returns true if the viewed node is a toml::array that contains only tables.
auto is_boolean() const -> bool noexcept
Returns true if the viewed node is a toml::value<bool>.
auto is_date() const -> bool noexcept
Returns true if the viewed node is a toml::value<date>.
auto is_date_time() const -> bool noexcept
Returns true if the viewed node is a toml::value<date_time>.
auto is_floating_point() const -> bool noexcept
Returns true if the viewed node is a toml::value<double>.
auto is_integer() const -> bool noexcept
Returns true if the viewed node is a toml::value<int64_t>.
auto is_number() const -> bool noexcept
Returns true if the viewed node is a toml::value<int64_t> or toml::value<double>.
auto is_string() const -> bool noexcept
Returns true if the viewed node is a toml::value<string>.
auto is_table() const -> bool noexcept
Returns true if the viewed node is a toml::table.
auto is_time() const -> bool noexcept
Returns true if the viewed node is a toml::value<time>.
auto is_value() const -> bool noexcept
Returns true if the viewed node is a toml::value<>.
auto operator[](size_t index) const -> node_view noexcept
Returns a view of the selected subnode.
auto operator[](string_view key) const -> node_view noexcept
Returns a view of the selected subnode.
template<typename U>
auto ref() const -> decltype(auto) noexcept
Gets a raw reference to the viewed node's underlying data.
auto type() const -> node_type noexcept
Returns the type identifier for the viewed node.
template<typename U>
auto value() const -> optional<U> noexcept
Gets the raw value contained by the referenced node.
template<typename U>
auto value_or(U&& default_value) const -> auto noexcept
Gets the raw value contained by the referenced node, or a default.
template<typename Func>
auto visit(Func&& visitor) const -> decltype(auto) noexcept(…)
Invokes a visitor on the viewed node based on its concrete type.

Friends

auto operator==(const node_view& lhs, const array& rhs) -> bool noexcept
Returns true if the viewed node is an array with the same contents as RHS.
template<typename U>
auto operator==(const node_view& lhs, const std::initializer_list<U>& rhs) -> bool noexcept
Returns true if the viewed node is an array with the same contents as the RHS initializer list.
template<typename U>
auto operator==(const node_view& lhs, const std::vector<U>& rhs) -> bool noexcept
Returns true if the viewed node is an array with the same contents as the RHS vector.
auto operator==(const node_view& lhs, const table& rhs) -> bool noexcept
Returns true if the viewed node is a table with the same contents as RHS.
template<typename U>
auto operator==(const node_view& lhs, const toml::value<U>& rhs) -> bool noexcept
Returns true if the viewed node is a value with the same value as RHS.
template<typename U, typename = std::enable_if_t<...impl::is_value_or_promotable<U>>>
auto operator==(const node_view& lhs, const U& rhs) -> bool noexcept
Returns true if the viewed node is a value with the same value as RHS.

Function documentation

template<typename T> template<typename U>
auto toml::node_view<T>::as() const noexcept

Gets a pointer to the viewed node as a more specific node type.

Template parameters
U The node type or TOML value type to cast to.
Returns A pointer to the node as the given type, or nullptr if it was a different type.

template<typename T> template<typename U>
bool toml::node_view<T>::is() const noexcept

Checks if this view references a node of a specific type.

Template parameters
U A TOML node or value type.
Returns Returns true if the viewed node is an instance of the specified type.

template<typename T>
node_view toml::node_view<T>::operator[](size_t index) const noexcept

Returns a view of the selected subnode.

Parameters
index The index of the node to retrieve
Returns A view of the selected node if this node represented an array and it contained a value at the given index, or an empty view.

template<typename T>
node_view toml::node_view<T>::operator[](string_view key) const noexcept

Returns a view of the selected subnode.

Parameters
key The key of the node to retrieve
Returns A view of the selected node if this node represented a table and it contained a value at the given key, or an empty view.

template<typename T> template<typename U>
decltype(auto) toml::node_view<T>::ref() const noexcept

Gets a raw reference to the viewed node's underlying data.

Template parameters
U One of the TOML value types.
Returns A reference to the underlying data.

template<typename T> template<typename U>
optional<U> toml::node_view<T>::value() const noexcept

Gets the raw value contained by the referenced node.

Template parameters
U One of the TOML value types. Can also be a string_view.
Returns The underlying value if the node was a value of the matching type (or convertible to it), or an empty optional.

template<typename T> template<typename U>
auto toml::node_view<T>::value_or(U&& default_value) const noexcept

Gets the raw value contained by the referenced node, or a default.

Template parameters
U Default value type. Must be (or be promotable to) one of the TOML value types.
Parameters
default_value The default value to return if the view did not reference a node, or if the node wasn't a value, wasn't the correct type, or no conversion was possible.
Returns The node's underlying value, or the default if the node wasn't a value, wasn't the correct type, or no conversion was possible.

template<typename T> template<typename Func>
decltype(auto) toml::node_view<T>::visit(Func&& visitor) const noexcept(…)

Invokes a visitor on the viewed node based on its concrete type.