template<typename ViewedType>
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 T>
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.
template<typename T>
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 node() const -> viewed_type* noexcept
Returns the node that's being referenced by the view.
auto operator[](size_t index) const -> node_view noexcept
Returns a view of the selected subnode.
auto operator[](std::string_view key) const -> node_view noexcept
Returns a view of the selected subnode.
auto operator[](std::wstring_view key) const -> node_view noexcept
Returns a view of the selected subnode.
template<typename T>
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 T>
auto value() const -> optional<T> noexcept
Gets the value contained by the referenced node.
template<typename T>
auto value_exact() const -> optional<T> noexcept
Gets the value contained by the referenced node.
template<typename T>
auto value_or(T&& 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

template<typename Char, typename T>
auto operator<<(std::basic_ostream<Char>&, const node_view<T>&) -> std::basic_ostream<Char>&
Prints the viewed node out to a stream.
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 T>
auto operator==(const node_view& lhs, const std::initializer_list<T>& rhs) -> bool noexcept
Returns true if the viewed node is an array with the same contents as the RHS initializer list.
template<typename T>
auto operator==(const node_view& lhs, const std::vector<T>& rhs) -> bool noexcept
Returns true if the viewed node is an array with the same contents as the RHS vector.
template<typename T, typename = std::enable_if_t<...impl::is_native<T> || impl::is_losslessly_convertible_to_native<T> >>
auto operator==(const node_view& lhs, const T& rhs) -> bool noexcept
Returns true if the viewed node is a value with the same value as RHS.
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 T>
auto operator==(const node_view& lhs, const toml::value<T>& rhs) -> bool noexcept
Returns true if the viewed node is a value with the same value as RHS.

Function documentation

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

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

Template parameters
T 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 ViewedType> template<typename T>
bool toml::node_view<ViewedType>::is() const noexcept

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

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

template<typename ViewedType>
node_view toml::node_view<ViewedType>::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 ViewedType>
node_view toml::node_view<ViewedType>::operator[](std::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 ViewedType>
node_view toml::node_view<ViewedType>::operator[](std::wstring_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 ViewedType> template<typename T>
decltype(auto) toml::node_view<ViewedType>::ref() const noexcept

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

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

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

Gets the value contained by the referenced node.

Template parameters
T One of the native TOML value types, or a type capable of convertible to one.
Returns The underlying value if the node was a value of the matching type (or convertible to it) and within the range of the output type, or an empty optional.

This function has 'permissive' retrieval semantics; some value types are allowed to convert to others (e.g. retrieving a boolean as an integer), and the specified return value type can be any type where a reasonable conversion from a native TOML value exists (e.g. std::wstring on Windows). If the source value cannot be represented by the destination type, an empty optional is returned. See node::value() for examples.

template<typename ViewedType> template<typename T>
optional<T> toml::node_view<ViewedType>::value_exact() const noexcept

Gets the value contained by the referenced node.

Template parameters
T One of the native TOML value types, or a type capable of losslessly representing one.
Returns The underlying value if the node was a value of the matching type (or losslessly convertible to it), or an empty optional.

This function has 'exact' retrieval semantics; the only return value types allowed are the TOML native value types, or types that can losslessly represent a native value type (e.g. std::wstring on Windows).

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

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

Template parameters
T Default value type. Must be one of the native TOML value types, or convertible to it.
Parameters
default_value The default value to return if the node wasn't a value, wasn't the correct type, or no conversion was possible.
Returns The underlying value if the node was a value of the matching type (or convertible to it) and within the range of the output type, or the provided default.

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

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