Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::get_value_inplace¶
template <typename T>
void get_value_inplace(T& value_ref) const noexcept(
noexcept(ConverterType<T>::from_node(std::declval<const basic_node&>(), std::declval<T&>()))); // (1)
template <typename BasicNodeType>
void get_value_inplace(BasicNodeType& value_ref) const; // (2)
This function converts a fkyaml::basic_node
to either of the followings and fills the conversion result into the given value_ref
parameter.
- a compatible value (copy-constructible)
The function is equivalent to executing
Unlike theget_value
function, this function does not require the template parameter typeT
to be default-constructible since no instantiation ofT
is necessary inside the function to return the conversion result. - a fkyaml::basic_node object
The function is equivalent to executing
This function shares internal implementation with the get_value()
function.
Thus, all the STL container & scalar types which are supported by that function, are also supported by this function as well.
See the notes there for details.
Differences from get_value()
One crutial difference from the get_value
function is, this function does not require the template parameter type T
to be default-constructible since no instantiation of T
is necessary inside the function to return the conversion result anymore.
So, you might prefer this function, for example, if you already created a T
object as a member variable and want to assign a node value to it.
Another is C-style array support.
While get_value
cannot accept any kind of C-style array types since returning such array objects is impossible due to its implementation, this function accepts 1D, 2D and 3D arrays. Note that, if T
is one of them, the target basic_node object must be a sequence. A type_error
is thrown otherwise.
Template Parameters¶
- T
- A compatible value type.
- BasicNodeType
- A basic_node template instance type.
Parameters¶
value_ref
[out]- A storage into which the conversion result is filled.
Examples¶
Example
#include <iostream>
#include <fkYAML/node.hpp>
struct not_default_constructible {
not_default_constructible() = delete;
explicit not_default_constructible(int v) noexcept
: value(v) {
}
not_default_constructible(const not_default_constructible&) = default;
int value {0};
};
// Custom implementation of from_node() for not_default_constructible objects.
// This function is called inside get_value_inplace().
// See https://fktn-k.github.io/fkYAML/api/node_value_converter/from_node/ for details.
inline void from_node(const fkyaml::node& n, not_default_constructible& ndc) {
ndc.value = n.get_value<int>();
}
int main() {
// create a sequence node.
fkyaml::node seq = {true, false};
// fill the node values into a 1D C-style array
bool bools[2] {};
seq.get_value_inplace(bools);
for (auto b : bools) {
std::cout << std::boolalpha << b << " ";
}
std::cout << "\n\n";
// create an integer scalar node
fkyaml::node integer = 123;
// get_value_inplace() accepts a type which is not default constructible.
not_default_constructible ndc {0};
integer.get_value_inplace(ndc);
std::cout << ndc.value << std::endl;
// of course, you can pass a value of default constructible type as well.
integer = -123;
integer.get_value_inplace(ndc.value);
std::cout << ndc.value << std::endl;
return 0;
}
output: