Defined in header <fkYAML/node_value_converter.hpp>
fkyaml::basic_node::from_node¶
template <typename BasicNodeType, typename TargetType = ValueType>
static auto from_node(BasicNodeType&& n, TargetType& val) noexcept(
noexcept(::fkyaml::from_node(std::forward<BasicNodeType>(n), val)))
-> decltype(::fkyaml::from_node(std::forward<BasicNodeType>(n), val), void())
Converts a basic_node
object to the target native data object.
This function is usually called by the get_value()
function of the basic_node
class.
Note that the TargetType
must be default-constructible.
Tips
This function can be used for user-defined types by implementing (partial) specialization for from_node()
function which is called internally by this function.
Note that the specialization must be implemented in the same namespace as the user-defined types (including the global namespace) so that the specialization can successfully be found by ADL (Argument Dependent Lookup).
You can find a detailed explanation of how this customization point works at this link.
Also, see the example below for the feel of how it can be achieved.
Template Parameters¶
- BasicNodeType
- A basic_node template instance type.
- TargetType
- A target native data type.
Parameters¶
n
[in]- A basic_node object used for conversion.
val
[out]- A native data object to which the converted value is assigned.
Example
#include <iostream>
#include <fkYAML/node.hpp>
namespace ns {
struct book {
std::string title;
std::string author;
int year;
};
void from_node(const fkyaml::node& n, book& b) {
b.title = n["title"].get_value_ref<const fkyaml::node::string_type&>();
b.author = n["author"].get_value_ref<const fkyaml::node::string_type&>();
b.year = n["year"].get_value<int>();
}
} // namespace ns
int main() {
fkyaml::node n = {
{"title", "Noman's Journey"},
{"author", "John Doe"},
{"year", 2023},
};
auto b = n.get_value<ns::book>();
std::cout << "\"" << b.title << "\" was written by " << b.author << " in " << b.year << "." << std::endl;
return 0;
}
output: