Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::at¶
// (1)
template <typename KeyType>
basic_node& at(KeyType&& key);
template <typename KeyType>
const basic_node& at(KeyType&& key) const;
// (2)
template <typename BasicNodeType>
basic_node& at(BasicNodeType&& key);
template <typename BasicNodeType>
const basic_node& at(BasicNodeType&& key) const;
Access to an element in a container node with either an index or key value.
This function must be called on a container node, or a fkyaml::type_error
would be thrown.
The input parameter key
must be either a basic_node
object or an object of a compatible type, i.e., a type with which a basic_node
object can be constructible.
Note that the overload (1) internally constructs a temporal basic_node
object.
So, if you use the same key multiple times, for example, in a for loop, consider creating a basic_node
as a key first for better performance.
Furthermore, unlike the operator[]
, this function executes one of the following checks depending on the target node value type.
- For sequence nodes
- Whether
key
is of an integeral type (e.g.,int
,size_t
) or an integer node.
If not, afkyaml::type_error
will be thrown. - Whether the value of
key
is more than or equal to the size of the queried sequence.
If not, afkyaml::out_of_range
will be thrown.
- Whether
- For mapping nodes
- Whether a given key exists in the target container.
If not,fkyaml::out_of_range
will be thrown.
- Whether a given key exists in the target container.
This function therefore costs a bit more than operator[]
due to the above extra checks.
Template Parameters¶
- KeyType
- A compatible key type.
- BasicNodeType
- A basic_node template instance type.
Parameters¶
key
[in]- A key to a target element in the sequence/mapping node.
Return Value¶
(Constant) reference to the node value which is associated with the given key.
Examples¶
Access an element with compatible keys
#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:
Access an element with basic_node
keys
#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: