Defined in header <fkYAML/node.hpp>
fkyaml::basic_node::(constructor)¶
basic_node() = default; // (1)
explicit basic_node(const node_type type); // (2)
explicit basic_node(const node_t type); // (3) deprecated
basic_node(const basic_node& rhs); // (4)
basic_node(basic_node&& rhs) noexcept; // (5)
template <typename CompatibleType, typename U = detail::remove_cvref_t<CompatibleType>>
basic_node(CompatibleType&& val) noexcept(
noexcept(ConverterType<U>::to_node(std::declval<basic_node&>(), std::declval<CompatibleType>()))); // (6)
basic_node(initializer_list_t init); // (7)
Constructs a new basic_node from a variety of data sources.
Available overloads are:
- Constructs a basic_node with a null value.
The resulting basic_node has thenode_type::NULL_OBJECT
type. -
Constructs a basic_node with the given type.
The resulting basic_node has a default value for the given type.type
default value node_type::SEQUENCE (empty sequence) node_type::MAPPING (empty mapping) node_type::NULL_VALUE nullptr
node_type::BOOLEAN false
node_type::INTEGER 0
node_type::FLOAT 0.0
node_type::STRING (empty string) -
Constructs a basic_node with the given type.
The resulting basic_node has a default value for the given type.
Default values are the same as those in the above table.Deprecation
The overload(2)
basic_node(const node_type);
replaces the overload(3)basic_node(const node_t);
which has been deprecated in version 0.3.12. It will be removed in a future version. Please replace calls likewith
-
Copy constructs a basic_node with an existing basic_node.
- Move constructs a basic_node with an existing basic_node.
The existing basic_node will be the same as a default-constructed basic_node at the end of this constructor. -
Constructs a basic_node with a value of a compatible type (see below).
The resulting basic_node has the value ofval
and the type which is associated withCompatibleType
.
Template parameterCompatibleType
includes, but not limited to, the following types:- sequences:
sequence_type
- container types which fulfills the following requirements, e.g.,
std::vector
orstd::set
.- Both
begin()
andend()
are callable on aCompatibleType
object. CompatibleType
doesn't have bothkey_type
andmapped_type
member types.- A
string_type
object is not constructible from aCompatibleType
object.
- Both
std::pair
andstd::tuple
- mappings
mapping_type
- container types which fullfills the following requirements, e.g.,
std::map
orstd::unordered_multimap
.- Both
begin()
andend()
are callable on aCompatibleType
object. CompatibleType
has bothkey_type
andmapped_type
member types.
- Both
- null
- booleans
- integers
integer_type
- all integral types except
bool
such asint
,uint32_t
orsize_t
.
- floating point values
float_number_type
float
,double
orlong double
- strings
string_type
- types with which
string_type
is constructible such aschar[]
,char*
orstd::string_view
(since C++17)
You can add types to meet your needs by implementing custom
to_node()
functions. Seenode_value_converter
for details. 7. Constructs a basic_node with a initializer_list_t object.
The resulting basic_node has the value of a container (sequence or mapping) which has the contents ofinit
.
Basically,init
is considered to be a sequence.
Ifinit
contains a sequence where each element contains 2 basic_node objects, however, such a sequence is interpreted as a mapping.initializer lists which have a single element
fkyaml::node n {true};
will create a sequence of 1 boolean value, not a boolean value alone. This is because overload resolution in C++ prioritizes a constructor with a single initializer list argument is prioritized to the others. In such cases, change the code tofkyaml::node n(true);
instead. - sequences:
Template Parameters¶
- CompatibleType
- A type such that
ConverterType<U>
has ato_node
function. - U
- A type referred to by
ValueType
with its topmost cv-qualifiers removed.
Parameters¶
type
[in]- the type of the value to create
rhs
[in]- the basic_node object to copy/move the value from
val
[in]- the value of a compatible type.
init
[in]- initializer list of basic_node objects
Examples¶
Overload(1): create a default value
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
fkyaml::node n;
std::cout << n << std::endl;
return 0;
}
output:
Overload(2): create an empty value with a given type
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
fkyaml::node n(fkyaml::node_type::INTEGER);
std::cout << n << std::endl;
return 0;
}
output:
Overload(3): create an empty value with a given type (deprecated)
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
fkyaml::node n(fkyaml::node::node_t::INTEGER);
std::cout << n << std::endl;
return 0;
}
output:
Overload(4): copy construct a basic_node object
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
fkyaml::node n(fkyaml::node::node_t::BOOLEAN);
fkyaml::node n2(n);
std::cout << n2 << std::endl;
return 0;
}
output:
Overload(5): move construct a basic_node object
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
fkyaml::node n(fkyaml::node::node_t::BOOLEAN);
fkyaml::node n2(std::move(n));
std::cout << n2 << std::endl;
return 0;
}
output:
Overload(6): create a basic_node object with compatible types
#include <iostream>
#include <list>
#include <tuple>
#include <unordered_map>
#include <fkYAML/node.hpp>
int main() {
// create nodes from objects of various types.
fkyaml::node sequence0 = std::list<bool> {true, false, false};
fkyaml::node sequence1 = std::make_tuple<std::string, int, bool>("foo", 123, true);
std::unordered_map<int, float> map_val = {{123, 3.14f}, {-456, 1.41f}};
fkyaml::node mapping = std::move(map_val);
fkyaml::node null = nullptr;
fkyaml::node boolean = false;
fkyaml::node integer = 12345;
fkyaml::node floating_point = 3.141592;
const char str_chars[] = "test";
fkyaml::node string = str_chars;
// print node values
std::cout << sequence0 << std::endl;
std::cout << sequence1 << std::endl;
std::cout << mapping << std::endl;
std::cout << null << std::endl;
std::cout << boolean << std::endl;
std::cout << integer << std::endl;
std::cout << floating_point << std::endl;
std::cout << string << std::endl;
return 0;
}
output:
Overload(7): create a basic_node object from an initializer list
#include <iostream>
#include <fkYAML/node.hpp>
int main() {
// create a sequence.
fkyaml::node n = {true, false};
std::cout << n << std::endl;
// create a mapping.
fkyaml::node n2 = {{"foo", 1024}};
std::cout << n2 << std::endl;
return 0;
}
output: