Skip to content

Defined in header <fkYAML/node.hpp>

fkyaml::basic_node::at

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
basic_node& at(KeyType&& key); // (1)

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
const basic_node& at(KeyType&& key) const; // (2)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
basic_node& at(KeyType&& key); // (3)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
const basic_node& at(KeyType&& key) const; // (4)

Access to a YAML node element with either an index (for sequences) or a key (for mappings).
Before accessing the element, this function checks the bounds in the case of a sequence or the existence of a key in the case of a mapping.
This function therefore costs a bit more than basic_node::operator[]() function due to the extra checks.
Furthermore, this function may throw the following exceptions:

  • fkyaml::type_error
    • if the queried node is neither a sequence nor a mapping, or
    • if the queried node is a sequence but the given key is not an integer.
  • fkyaml::out_of_range
    • if the given key does not exist in the queried mapping, or
    • if the given index exceeds the size of the queried sequence.

Overload (1), (2)

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
basic_node& at(KeyType&& key); // (1)

template <
    typename KeyType, detail::enable_if_t<
                            detail::conjunction<
                                detail::negation<detail::is_basic_node<KeyType>>,
                                detail::is_node_compatible_type<basic_node, KeyType>>::value,
                            int> = 0>
const basic_node& at(KeyType&& key) const; // (2)

Accesses to an element in the YAML sequence/mapping node with the given key object of a compatible type with the basic_node class, i.e., a type with which a basic_node object is constructible.
These overloads internally construct a basic_node object with key.

Parameters

index [in]
An index/key for an element in the YAML sequence/mapping node.

Return Value

Reference, or constant reference, to the YAML node object associated with the given index/key.

Example
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a YAML sequence node.
    fkyaml::node n1 = {123, 234, 345, 456};

    // print YAML nodes at the following indexes.
    std::cout << n1.at(0) << std::endl;
    std::cout << n1.at(1) << std::endl;
    std::cout << n1.at(2) << std::endl;
    std::cout << n1.at(3) << std::endl;

    // try to print a YAML node with an index which exceeds the size.
    try {
        std::cout << n1.at(4) << std::endl;
    }
    catch (const fkyaml::out_of_range& e) {
        std::cout << e.what() << std::endl;
    }

    // create a YAML mapping node.
    fkyaml::node n2 = {{"foo", true}, {"bar", 123}};

    // print YAML nodes associated with the following keys.
    std::cout << std::boolalpha << n2.at("foo") << std::endl;
    std::cout << n2.at("bar") << std::endl;

    // try to print a YAML node with a key which does not exist.
    try {
        std::cout << n2.at(true) << std::endl;
    }
    catch (const fkyaml::out_of_range& e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

output:

123
234
345
456
out_of_range: index 4 is out of range
true
123
out_of_range: key 'true' is not found.

Overload (3), (4)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
basic_node& at(KeyType&& key); // (3)

template <
    typename KeyType, detail::enable_if_t<detail::is_basic_node<detail::remove_cvref_t<KeyType>>::value, int> = 0>
const basic_node& at(KeyType&& key) const; // (4)

Accesses to an element in the YAML sequence/mapping node with the given basic_node key object.
Unlike the overloads (1) and (2) above, these overloads do not internally construct a basic_node object.
So, these overloads works more effectively when some key objects are used multiple times, for instance, in a for-loop.

Template Parameters

KeyType
A key type which is a kind of the basic_node template class.

Parameters

key [in]
An index/key for an element in the YAML sequence/mapping node.

Return Value

Reference, or constant reference, to the YAML node object associated with the given index/key.

Example
#include <iostream>
#include <fkYAML/node.hpp>

int main() {
    // create a YAML sequence node.
    fkyaml::node n1 = {123, 234, 345, 456};

    // print YAML nodes at the following indexes.
    fkyaml::node index_zero = 0;
    fkyaml::node index_one = 1;
    fkyaml::node index_two = 2;
    fkyaml::node index_three = 3;
    std::cout << n1[index_zero] << std::endl;
    std::cout << n1[index_one] << std::endl;
    std::cout << n1[index_two] << std::endl;
    std::cout << n1[index_three] << std::endl;

    // try to print a YAML node with an index which exceeds the size.
    try {
        fkyaml::node index_four = 4;
        std::cout << n1.at(index_four) << std::endl;
    }
    catch (const fkyaml::out_of_range& e) {
        std::cout << e.what() << std::endl;
    }

    // create a YAML node.
    fkyaml::node n2 = {{"foo", true}, {"bar", 123}};

    // print YAML nodes associated with the following keys.
    fkyaml::node foo_key = "foo";
    fkyaml::node bar_key = "bar";
    std::cout << std::boolalpha << n2[foo_key] << std::endl;
    std::cout << n2[bar_key] << std::endl;

    // try to print a YAML node with a key which does not exist.
    try {
        fkyaml::node true_key = true;
        std::cout << n2.at(true_key) << std::endl;
    }
    catch (const fkyaml::out_of_range& e) {
        std::cout << e.what() << std::endl;
    }

    return 0;
}

output:

123
234
345
456
out_of_range: index 4 is out of range
true
123
out_of_range: key 'true' is not found.

See Also