Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::get_value_or¶
This function tries to convert a fkyaml::basic_node
to T
.
Visit the documentation for the get_value function for supported types since this function internally calls it.
If the conversion fails, this function returns default_value
instead of throwing an exception as the get_value
function does.
Just as the get_value
function, this function also makes a copy of the value.
If the copying costs too much, or if you need an address of the original value, then you should call one of the following functions instead.
Template Parameters¶
- T
- A compatible value type which might be cv-qualified or a reference type.
- U
- The default value type from which
T
(the 1st overload) orBasicNodeType
(the 2nd overload) must be constructible.
This is likely to beconst T&
orT&&
. - BasicNodeType
- A basic_node template instance type.
Return Value¶
A value converted from the basic_node object if the conversion succeeded, default_value
otherwise.
Examples¶
Example
#include <iostream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#include <fkYAML/node.hpp>
int main() {
// try get sequence node values
fkyaml::node seq = {true, false};
// successful case
std::vector<bool> vec_def {false, true};
auto bool_vec = seq.get_value_or<std::vector<bool>>(vec_def);
for (auto b : bool_vec) {
std::cout << std::boolalpha << b << std::endl;
}
// error case
auto tpl_def = std::make_tuple<int, bool, std::string>(0, false, "default");
auto tpl = seq.get_value_or<std::tuple<int, bool, std::string>>(std::move(tpl_def));
std::cout << std::get<0>(tpl) << ", ";
std::cout << std::get<1>(tpl) << ", ";
std::cout << std::get<2>(tpl) << "\n\n";
// try to get mapping node values
fkyaml::node map = {
{0, "foo"},
{1, "bar"},
{2, "baz"},
};
std::unordered_map<uint32_t, std::string> umap_def {{0, "defalt"}};
auto umap = map.get_value_or<std::unordered_map<uint32_t, std::string>>(std::move(umap_def));
for (auto& p : umap) {
std::cout << p.first << " : " << p.second << std::endl;
}
std::cout << std::endl;
// try to get scalar node values.
fkyaml::node scalar = 1.23;
// get the node value (value gets copied).
auto dbl_val = scalar.get_value_or<double>(0.);
auto str_val = scalar.get_value_or<std::string>("default");
std::cout << dbl_val << std::endl;
std::cout << str_val << std::endl;
// Numeric scalar value will be converted to target numeric types inside get_value_or().
auto bool_val = scalar.get_value_or<bool>(false); // 1.23 -> true
auto int_val = scalar.get_value_or<int>(0); // 1.23 -> 1
std::cout << std::boolalpha << bool_val << std::endl;
std::cout << int_val << std::endl;
return 0;
}
output: