### Install fkYAML with CMake Source: https://fktn-k.github.io/fkYAML/tutorials Install fkYAML using CMake. You can specify the installation prefix and choose to install the single-header version. ```bash cd /path/to/fkYAML cmake -B build -S . [-DCMAKE_INSTALL_PREFIX=] [-DFK_YAML_USE_SINGLE_HEADER=ON] cmake --build build --target install ``` -------------------------------- ### Emplace Example Source: https://fktn-k.github.io/fkYAML/api/ordered_map/emplace Demonstrates the initialization and iteration of an fkyaml::ordered_map. This example shows how to create an ordered_map with initial values and then print its contents. ```cpp #include #include int main() { fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; for (auto& pair : om) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` -------------------------------- ### Example Usage of as_int and Exception Handling Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_value_ref Demonstrates how to get a reference to an integer value from a node using `as_int()` and how an incompatible type access results in a `fkyaml::type_error` exception. ```cpp #include #include int main() { // create a YAML node. fkyaml::node n = 123; const fkyaml::node cn = 123; // get references to the value. auto& ref = n.as_int(); auto& cref = cn.as_int(); // print the referenced values std::cout << ref << std::endl; std::cout << cref << std::endl; // specifying incompatible reference type throws an exception try { auto iref = n.as_map(); } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } return 0; } ``` -------------------------------- ### Example Usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/yaml_version_t This example demonstrates how to deserialize a YAML string, retrieve the YAML version using `get_yaml_version()`, and set the YAML version using `set_yaml_version()`. ```APIDOC #include #include #include int main() { char input[] = "%YAML 1.2\n" "---\n" "foo: bar\n"; // deserialize a YAML formatted string. fkyaml::node n = fkyaml::node::deserialize(input); // call get_yaml_version(). fkyaml::node::yaml_version_t yaml_ver = n.get_yaml_version(); std::cout << std::boolalpha; std::cout << (n.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2) << std::endl; // overwrite the YAML version to 1.1. n.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_1); // call get_yaml_version() again. std::cout << (n.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_1) << std::endl; return 0; } ``` -------------------------------- ### Example of preferred constructor usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/constructor This example demonstrates the preferred way to construct a node with `fkyaml::node_type`, replacing the deprecated `node_t` overload. ```cpp fkyaml::node n(fkyaml::node_type::MAPPING); ``` -------------------------------- ### Fetch fkYAML using CMake FetchContent Source: https://fktn-k.github.io/fkYAML/home/releases Example of how to integrate fkYAML into a CMake project using the `FetchContent` module. This example downloads the `fkYAML_min.zip` release artifact, which contains the minimum required files for CMake. ```cmake FetchContent_Declare( fkYAML URL https://github.com/fktn-k/fkYAML/releases/download/v0.4.1/fkYAML_min.zip ) FetchContent_MakeAvailable(fkYAML) ``` -------------------------------- ### Get Value or Default Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_value_or Demonstrates using `get_value_or` for sequence, mapping, and scalar nodes. It shows successful conversions and how default values are returned on failure. Numeric scalars are also converted to different numeric types. ```cpp #include #include #include #include #include #include int main() { // try get sequence node values fkyaml::node seq = {true, false}; // successful case std::vector vec_def {false, true}; auto bool_vec = seq.get_value_or>(vec_def); for (auto b : bool_vec) { std::cout << std::boolalpha << b << std::endl; } // error case auto tpl_def = std::make_tuple(0, false, "default"); auto tpl = seq.get_value_or>(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 umap_def {{0, "defalt"}}; auto umap = map.get_value_or>(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(0.); auto str_val = scalar.get_value_or("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(false); // 1.23 -> true auto int_val = scalar.get_value_or(0); // 1.23 -> 1 std::cout << std::boolalpha << bool_val << std::endl; std::cout << int_val << std::endl; return 0; } ``` -------------------------------- ### Example of deprecated constructor usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/constructor This example shows the deprecated usage of constructing a node with `fkyaml::node::node_t`. It is recommended to use `fkyaml::node_type` instead. ```cpp fkyaml::node n(fkyaml::node::node_t::MAPPING); ``` -------------------------------- ### Basic Node Get Value Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_value Demonstrates how to use the get_value method to convert node values to different C++ types, including sequences, mappings, and scalar values. It also shows how to handle potential type conversion errors. ```APIDOC ## get_value() ### Description Converts the basic_node object to a compatible native data value of type `T`. ### Template Parameters * `_T_`: A compatible value type which might be cv-qualified or a reference type. * `_ValueType_`: A compatible value type. By default, this is `std::remove_cvref_t`. * `_BasicNodeType_`: A basic_node template instance type. ### Return Value A compatible native data value converted from the basic_node object. ### Method ```cpp template _T_ get_value(_Args_&&... args) const; ``` ### Example ```cpp #include #include #include #include #include #include int main() { // create sequence nodes. fkyaml::node seq = {true, false}; fkyaml::node seq2 = {123, 3.14, "foo"}; // get the node values // to std::vector auto bool_vec = seq.get_value>(); for (auto b : bool_vec) { std::cout << std::boolalpha << b << " "; } std::cout << "\n\n"; // to std::tuple auto tpl = seq2.get_value>(); std::cout << std::get<0>(tpl) << " "; std::cout << std::get<1>(tpl) << " "; std::cout << std::get<2>(tpl) << "\n\n"; // create a mapping node. fkyaml::node map = { {0, "foo"}, {1, "bar"}, {2, "baz"}, }; // get the node values // to std::unordered_map auto umap = map.get_value>(); for (auto& p : umap) { std::cout << p.first << " : " << p.second << std::endl; } std::cout << std::endl; // create scalar nodes. fkyaml::node n = 1.23; fkyaml::node n2 = "foo"; // get the node value (value gets copied). auto float_val = n.get_value(); auto str_val = n2.get_value(); std::cout << float_val << std::endl; std::cout << str_val << std::endl; // Numeric scalar value can be converted to other numeric types inside get_value(). auto bool_val = n.get_value(); auto int_val = n.get_value(); std::cout << std::boolalpha << bool_val << std::endl; std::cout << int_val << std::endl; // specifying incompatible type throws an exception try { auto float_val = n2.get_value(); } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } return 0; } ``` ``` -------------------------------- ### Node Comparison Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/operator_lt Demonstrates the usage of operator< for various fkyaml::node types, including comparisons within the same type and between different types. This example requires including the fkyaml/node.hpp header. ```cpp #include #include #include int main() { // create YAML nodes. fkyaml::node seq_1 = {1, 2, 3}; fkyaml::node seq_2 = {1, 2, 4}; fkyaml::node map_1 = {{"foo", true}, {"bar", 123}}; fkyaml::node map_2 = {{"bar", 123}, {"foo", true}}; fkyaml::node null_1 = nullptr; fkyaml::node null_2 = nullptr; fkyaml::node bool_1 = false; fkyaml::node bool_2 = true; fkyaml::node integer_1 = 321; fkyaml::node integer_2 = 123; fkyaml::node float_1 = 1.23; fkyaml::node float_2 = 3.14; fkyaml::node string_1 = "foo"; fkyaml::node string_2 = "bar"; // the same type std::cout << std::boolalpha; std::cout << (seq_1 < seq_2) << std::endl; std::cout << (map_1 < map_2) << std::endl; std::cout << (null_1 < null_2) << std::endl; std::cout << (bool_1 < bool_2) << std::endl; std::cout << (integer_1 < integer_2) << std::endl; std::cout << (float_1 < float_2) << std::endl; std::cout << (string_1 < string_2) << std::endl; // different types std::cout << (bool_1 < string_1) << std::endl; std::cout << (float_2 < seq_2) << std::endl; return 0; } ``` -------------------------------- ### Example Usage Source: https://fktn-k.github.io/fkYAML/api/yaml_version_type Demonstrates how to use yaml_version_type with fkyaml::node, including deserialization, querying the version, and setting the version. ```APIDOC #include #include #include int main() { char input[] = "%YAML 1.2\n" "---\n" "foo: bar\n"; // deserialize a YAML formatted string. fkyaml::node n = fkyaml::node::deserialize(input); // query the YAML version by calling get_yaml_version_type(). std::cout << std::boolalpha; std::cout << (n.get_yaml_version_type() == fkyaml::yaml_version_type::VERSION_1_2) << std::endl; // overwrite the YAML version to 1.1. n.set_yaml_version_type(fkyaml::yaml_version_type::VERSION_1_1); // query the YAML version again. std::cout << (n.get_yaml_version_type() == fkyaml::yaml_version_type::VERSION_1_1) << std::endl; return 0; } ``` -------------------------------- ### Iterator Usage Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/iterator A C++ example demonstrating how to use `fkyaml::basic_node::iterator` and `fkyaml::basic_node::const_iterator` to traverse a sequence node and handle potential exceptions when accessing keys. ```APIDOC ## Example ```cpp #include #include #include int main() { // create a container node. fkyaml::node sequence_node = {1, 2, 3}; // get an iterator to the first sequence element. fkyaml::node::iterator it = sequence_node.begin(); fkyaml::node::const_iterator end_it = sequence_node.cend(); // print all the elements. // note `iterator` and `const_iterator` are comparable. for (; it != end_it; ++it) { std::cout << *it << std::endl; } it = sequence_node.begin(); try { // key() cannot be called on an iterator pointing to a sequence element. std::cout << it.key() << std::endl; } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } return 0; } ``` ### Output ``` 1 2 3 Cannot retrieve key from non-mapping iterators. ``` ``` -------------------------------- ### fkyaml::node Usage Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/node Demonstrates how to create, manipulate, and output a YAML node using fkyaml::node. ```APIDOC ## fkyaml::node This type is a specialization of the basic_node template class and uses the default template parameter types. ### Example ```cpp #include #include int main() { // create a YAML node. fkyaml::node n = {{"foo", 3.14}, {"bar", true}, {"baz", nullptr}, {"qux", {{"corge", {1, 2, 3}}}}}; // add a new value. n["qux"]["key"] = {"another", "value"}; // output a YAML formatted string. std::cout << n << std::endl; } ``` ### Output ```yaml bar: true baz: null foo: 3.14 qux: corge: - 1 - 2 - 3 key: - another - value ``` ### See Also * basic_node * operator<< ``` -------------------------------- ### Example of creating and printing a mapping node Source: https://fktn-k.github.io/fkYAML/api/basic_node/mapping Demonstrates how to create a mapping node with initial key-value pairs and print its YAML representation to standard output. Ensure you include the necessary headers. ```cpp #include #include int main() { fkyaml::node::mapping_type m = {{"foo", false}, {"bar", 3.14}}; fkyaml::node n = fkyaml::node::mapping(m); std::cout << n << std::endl; return 0; } ``` -------------------------------- ### YAML Node Type Checking Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/node_t Creates various YAML nodes and checks their types using the `type()` method against the `fkyaml::node::node_t` enum. This example requires including ``, ``, and ``. ```cpp #include #include #include int main() { // create YAML nodes. fkyaml::node sequence_node = {1, 2, 3}; fkyaml::node mapping_node = {{"foo", true}, {"bar", false}}; fkyaml::node null_node; fkyaml::node boolean_node = true; fkyaml::node integer_node = 256; fkyaml::node float_node = 3.14; fkyaml::node string_node = "Hello, world!"; // call type() std::cout << std::boolalpha; std::cout << (sequence_node.type() == fkyaml::node::node_t::SEQUENCE) << std::endl; std::cout << (mapping_node.type() == fkyaml::node::node_t::MAPPING) << std::endl; std::cout << (null_node.type() == fkyaml::node::node_t::NULL_OBJECT) << std::endl; std::cout << (boolean_node.type() == fkyaml::node::node_t::BOOLEAN) << std::endl; std::cout << (integer_node.type() == fkyaml::node::node_t::INTEGER) << std::endl; std::cout << (float_node.type() == fkyaml::node::node_t::FLOAT_NUMBER) << std::endl; std::cout << (string_node.type() == fkyaml::node::node_t::STRING) << std::endl; return 0; } ``` -------------------------------- ### Example Usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/mapping_type This example demonstrates how to check if the default mapping_type is std::map. ```APIDOC ## Example ```cpp #include #include #include #include #include int main() { std::cout << std::boolalpha << std::is_same, fkyaml::node::mapping_type>::value << std::endl; return 0; } ``` ### Output ``` false ``` ``` -------------------------------- ### Verify Default string_type Source: https://fktn-k.github.io/fkYAML/api/basic_node/string_type This example demonstrates how to verify that the default `string_type` for `fkyaml::node` is indeed `std::string` using `std::is_same`. ```cpp #include #include #include #include #include int main() { std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } ``` -------------------------------- ### Set YAML Version Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/set_yaml_version Demonstrates how to set the YAML version for two nodes and verify their versions. This function is being deprecated in favor of `set_yaml_version_type`. ```cpp #include #include #include int main() { fkyaml::node n; n.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_1); fkyaml::node n2; n2.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_2); std::cout << std::boolalpha; std::cout << (n.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_1) << std::endl; std::cout << (n2.get_yaml_version() == fkyaml::node::yaml_version_t::VER_1_2) << std::endl; return 0; } ``` -------------------------------- ### Get Iterator to First Element of Sequence Node Source: https://fktn-k.github.io/fkYAML/api/basic_node/begin Use `begin()` to get an iterator to the first element of a sequence node. Ensure the node is a sequence or mapping, otherwise a `fkyaml::type_error` will be thrown. ```cpp #include #include int main() { // create a sequence node. fkyaml::node n = {"foo", "bar"}; // get an iterator to the first element. fkyaml::node::iterator it = n.begin(); std::cout << *it << std::endl; return 0; } ``` -------------------------------- ### Node Type Checking Example Source: https://fktn-k.github.io/fkYAML/api/node_type Demonstrates how to create various YAML nodes and check their types using the get_type() method. ```APIDOC ## Example: Checking Node Types ### Description This example shows how to create different types of fkyaml::node objects and then use the `get_type()` method to determine their underlying YAML type. ### Code ```cpp #include #include #include int main() { // create YAML nodes. fkyaml::node sequence_node = {1, 2, 3}; fkyaml::node mapping_node = {{"foo", true}, {"bar", false}}; fkyaml::node null_node; fkyaml::node boolean_node = true; fkyaml::node integer_node = 256; fkyaml::node float_node = 3.14; fkyaml::node string_node = "Hello, world!"; // query node value types by calling get_type() std::cout << std::boolalpha; std::cout << (sequence_node.get_type() == fkyaml::node_type::SEQUENCE) << std::endl; std::cout << (mapping_node.get_type() == fkyaml::node_type::MAPPING) << std::endl; std::cout << (null_node.get_type() == fkyaml::node_type::NULL_OBJECT) << std::endl; std::cout << (boolean_node.get_type() == fkyaml::node_type::BOOLEAN) << std::endl; std::cout << (integer_node.get_type() == fkyaml::node_type::INTEGER) << std::endl; std::cout << (float_node.get_type() == fkyaml::node_type::FLOAT) << std::endl; std::cout << (string_node.get_type() == fkyaml::node_type::STRING) << std::endl; return 0; } ``` ### Output ``` true true true true true true true ``` ``` -------------------------------- ### Access elements with basic_node keys Source: https://fktn-k.github.io/fkYAML/api/basic_node/at Illustrates using `fkyaml::node` objects as keys to access elements in sequence and mapping nodes. This example also demonstrates exception handling for out-of-range access. ```cpp #include #include 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; } ``` -------------------------------- ### Example: Swapping Node Data using Member Function Source: https://fktn-k.github.io/fkYAML/api/basic_node/swap Demonstrates swapping data between two `fkyaml::node` objects using the member `swap` function. The output shows the values after the swap. ```cpp #include #include int main() { // create YAML nodes. fkyaml::node n1 = 123; fkyaml::node n2 = "foo"; // swap the internally stored data between n1 & n2. n1.swap(n2); // print the swapped values. std::cout << n1.as_str() << std::endl; std::cout << n2.get_value() << std::endl; return 0; } ``` -------------------------------- ### Example Usage of get_value_inplace Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_value_inplace Demonstrates filling node values into a C-style array and a non-default-constructible object, as well as a default-constructible type. ```cpp #include #include 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 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; } ``` -------------------------------- ### Deprecated Usage Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/set_yaml_version Illustrates the deprecated usage of `set_yaml_version` with `fkyaml::node::yaml_version_t::VER_1_2`. ```cpp n.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_2); ``` -------------------------------- ### Recommended get_type() function usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/type Example of the recommended `get_type()` function call. ```cpp fkyaml::node_type t = n.get_type(); ``` -------------------------------- ### Set YAML Version Type Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/set_yaml_version_type Demonstrates how to set the YAML version for a `basic_node` object to either VERSION_1_1 or VERSION_1_2 and verifies the setting using `get_yaml_version_type`. ```cpp #include #include #include int main() { fkyaml::node n; n.set_yaml_version_type(fkyaml::yaml_version_type::VERSION_1_1); fkyaml::node n2; n2.set_yaml_version_type(fkyaml::yaml_version_type::VERSION_1_2); std::cout << std::boolalpha; std::cout << (n.get_yaml_version_type() == fkyaml::yaml_version_type::VERSION_1_1) << std::endl; std::cout << (n2.get_yaml_version_type() == fkyaml::yaml_version_type::VERSION_1_2) << std::endl; return 0; } ``` -------------------------------- ### Use fkYAML with find_package() Source: https://fktn-k.github.io/fkYAML/tutorials/cmake_integration Locate fkYAML using find_package() and link against the imported target. This method requires a release package to be installed. ```cmake cmake_minimum_required(VERSION 3.8) project(ExampleProject LANGUAGES CXX) find_package(fkYAML 0.3.1 REQUIRED) add_executable(example example.cpp) target_link_libraries(example PRIVATE fkYAML::fkYAML) ``` -------------------------------- ### Example: Swapping Node Data using Non-Member Function Source: https://fktn-k.github.io/fkYAML/api/basic_node/swap Illustrates swapping data between two `fkyaml::node` objects using the non-member `swap` function. The output confirms the data has been exchanged. ```cpp #include #include int main() { // create YAML nodes. fkyaml::node n1 = 123; fkyaml::node n2 = "foo"; // swap the internally stored data between n1 & n2. using std::swap; swap(n1, n2); // print the swapped values. std::cout << n1.as_str() << std::endl; std::cout << n2.get_value() << std::endl; return 0; } ``` -------------------------------- ### Deserialize Multiple YAML Documents from a FILE Pointer Source: https://fktn-k.github.io/fkYAML/api/basic_node/deserialize_docs This example demonstrates deserializing multiple YAML documents from a `FILE` pointer using `fkyaml::node::deserialize_docs`. ```APIDOC ## `fkyaml::node::deserialize_docs(FILE*)` ### Description Deserializes multiple YAML documents from a `FILE` pointer. ### Method `std::vector deserialize_docs(FILE* p_file)` ### Parameters * `p_file` (FILE*) - A pointer to an open `FILE` stream containing the YAML content. ### Request Example ```cpp #include #include #include #include #include int main() { // deserialize a YAML string. FILE* p_file = std::fopen("input_multi.yaml", "r"); if (!p_file) { // You must not pass a null FILE pointer. return -1; } std::vector docs = fkyaml::node::deserialize_docs(p_file); std::fclose(p_file); // check the deserialization result. std::cout << docs[0]["foo"].get_value() << std::endl; std::cout << docs[0]["bar"].get_value() << std::endl; std::cout << std::setprecision(3) << docs[0]["baz"].get_value() << std::endl; std::cout << std::endl; std::cout << docs[1][nullptr].as_str() << std::endl; std::cout << docs[1][false].get_value() << std::endl; std::cout << std::setprecision(4) << docs[1][true].get_value() << std::endl; return 0; } ``` ### Response #### Success Response A `std::vector` of `fkyaml::node` objects, where each object represents a parsed YAML document. #### Response Example ```cpp 1 123 3.14 one 456 1.414 ``` ``` -------------------------------- ### Get Iterator to End of Node Source: https://fktn-k.github.io/fkYAML/api/basic_node/end Use `end()` to obtain an iterator to the element past the last element of a container node. This example demonstrates creating a sequence node, getting its end iterator, and then decrementing it to access the last element. ```cpp #include #include int main() { // create a sequence node. fkyaml::node n = {"foo", "bar"}; // get an iterator to the past-the-last element. fkyaml::node::iterator it = n.end(); // decrement the iterator to point to the last element. --it; std::cout << *it << std::endl; return 0; } ``` -------------------------------- ### Build Project with CMake Source: https://fktn-k.github.io/fkYAML/tutorials Commands to build a project that uses the fkYAML library. Ensure you are in the project's root directory. ```bash cd /path/to/tutorial/ cmake -B build -S . -DCMAKE_BUILD_TYPE=Release cmake --build build --config Release ``` -------------------------------- ### Example Usage of string_type Source: https://fktn-k.github.io/fkYAML/api/basic_node/string_type This example demonstrates how to check if the default `string_type` for `fkyaml::node` is `std::string`. ```APIDOC ## Example ```cpp #include #include #include #include #include int main() { std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } ``` ### Output ``` true ``` ``` -------------------------------- ### Basic CMakeLists.txt for fkYAML Project Source: https://fktn-k.github.io/fkYAML/tutorials A minimal CMakeLists.txt file to find and link the fkYAML library to your executable. ```cmake cmake_minimum_required(VERSION 3.8) project(tutorial LANGUAGES CXX) find_package(fkYAML REQUIRED) add_executable(tutorial tutorial.cpp) ``` -------------------------------- ### Boolean Type Usage Example Source: https://fktn-k.github.io/fkYAML/api/basic_node/boolean_type This example demonstrates how to check if the `boolean_type` alias resolves to `bool` using `std::is_same`. It includes the necessary headers and a simple main function. ```cpp #include #include #include #include int main() { std::cout << std::boolalpha << std::is_same::value << std::endl; return 0; } ``` -------------------------------- ### Get Size of YAML Node Source: https://fktn-k.github.io/fkYAML/api/basic_node/size Demonstrates how to get the size of various YAML node types using `size()`. It includes error handling for nodes that do not support the `size()` operation. ```cpp #include #include #include int main() { // create YAML nodes. std::vector nodes = { {1, 2, 3}, {{"foo", true}, {"bar", false}, {"baz", true}}, fkyaml::node(), true, 256, 3.14, "foo"}; for (const auto& n : nodes) { try { // call size() std::cout << n.size() << std::endl; } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } } return 0; } ``` -------------------------------- ### Deprecated: Get YAML Version Type Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_yaml_version Shows the deprecated method for getting the YAML version and its recommended replacement. Calls to `basic_node::yaml_version_t get_yaml_version()` should be replaced with `fkyaml::yaml_version_type get_yaml_version_type()`. ```cpp fkyaml::node::yaml_version_t v = n.get_yaml_version(); ``` ```cpp fkyaml::yaml_version_type v = n.get_yaml_version_type(); ``` -------------------------------- ### Access elements with compatible keys Source: https://fktn-k.github.io/fkYAML/api/basic_node/at Demonstrates accessing elements in sequence and mapping nodes using compatible key types like integers and strings. Includes examples of handling `fkyaml::out_of_range` exceptions for invalid indices or non-existent keys. ```cpp #include #include 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; } ``` -------------------------------- ### Get String Node Reference Source: https://fktn-k.github.io/fkYAML/api/basic_node/as_str Use `as_str()` to get a reference to the string value of a node. This method throws `fkyaml::type_error` if the node is not a string. It can be used on both non-const and const nodes. ```cpp #include #include int main() { // create a string node. fkyaml::node n = "foo"; // get reference to the string value from a non-const node. // use `auto&` or `fkyaml::node::string_type&` for forward compatibility. auto& s = n.as_str(); // get const reference to the string value from a const node. const fkyaml::node cn = n; const auto& cs = cn.as_str(); // modify the string value. s = "bar"; std::cout << s << std::endl; std::cout << cs << std::endl; } ``` -------------------------------- ### Create and Manipulate YAML Node Source: https://fktn-k.github.io/fkYAML/api/basic_node/node Demonstrates creating a YAML node with initial values, adding a new nested value, and outputting the node in YAML format. Requires including and . ```cpp #include #include int main() { // create a YAML node. fkyaml::node n = {{"foo", 3.14}, {"bar", true}, {"baz", nullptr}, {"qux", {{"corge", {1, 2, 3}}}}}; // add a new value. n["qux"]["key"] = {"another", "value"}; // output a YAML formatted string. std::cout << n << std::endl; } ``` -------------------------------- ### Querying Node Types with get_type() Source: https://fktn-k.github.io/fkYAML/api/node_type Demonstrates how to create various fkyaml::node types and query their type using the get_type() method, comparing the result against the fkyaml::node_type enumeration. ```cpp #include #include #include int main() { // create YAML nodes. fkyaml::node sequence_node = {1, 2, 3}; fkyaml::node mapping_node = {{"foo", true}, {"bar", false}}; fkyaml::node null_node; fkyaml::node boolean_node = true; fkyaml::node integer_node = 256; fkyaml::node float_node = 3.14; fkyaml::node string_node = "Hello, world!"; // query node value types by calling get_type() std::cout << std::boolalpha; std::cout << (sequence_node.get_type() == fkyaml::node_type::SEQUENCE) << std::endl; std::cout << (mapping_node.get_type() == fkyaml::node_type::MAPPING) << std::endl; std::cout << (null_node.get_type() == fkyaml::node_type::NULL_OBJECT) << std::endl; std::cout << (boolean_node.get_type() == fkyaml::node_type::BOOLEAN) << std::endl; std::cout << (integer_node.get_type() == fkyaml::node_type::INTEGER) << std::endl; std::cout << (float_node.get_type() == fkyaml::node_type::FLOAT) << std::endl; std::cout << (string_node.get_type() == fkyaml::node_type::STRING) << std::endl; return 0; } ``` -------------------------------- ### Get Tag Name of YAML Node Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_tag_name Demonstrates how to get a tag name from a YAML node. It includes error handling for cases where no tag name is set and shows how to set and retrieve a tag name. ```cpp #include #include int main() { // create a YAML node. fkyaml::node n = 123; // try to get a tag name before any tag name has been set. try { std::cout << n.get_tag_name() << std::endl; } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } // set a tag name to the node. n.add_tag_name("!!int"); std::cout << n.get_tag_name() << std::endl; return 0; } ``` -------------------------------- ### Get Anchor Name from YAML Node Source: https://fktn-k.github.io/fkYAML/api/basic_node/get_anchor_name Demonstrates how to get an anchor name from a YAML node. It includes error handling for cases where no anchor name is set and shows how to set and then retrieve an anchor name. ```cpp #include #include int main() { // create a YAML node. fkyaml::node n = 123; // try to get an anchor name before any anchor name has been set. try { std::cout << n.get_anchor_name() << std::endl; } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } // set an anchor name to the node. n.add_anchor_name("anchor"); std::cout << n.get_anchor_name() << std::endl; return 0; } ``` -------------------------------- ### Accessing elements with fkyaml::ordered_map::at Source: https://fktn-k.github.io/fkYAML/api/ordered_map/at Demonstrates how to access elements using `at` with existing keys and how to handle exceptions for non-existent keys. Ensure the key exists to avoid runtime errors. ```cpp #include #include int main() { fkyaml::ordered_map om = {{"foo", 123}, {"bar", "baz"}}; std::cout << om.at("foo") << std::endl; std::cout << om.at("bar") << std::endl; // accesses with a unknown key will throw an exception. try { fkyaml::node& n = om.at("baz"); } catch (const fkyaml::exception& e) { std::cout << e.what() << std::endl; } return 0; } ``` -------------------------------- ### Deprecated type() function usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/type Example of the deprecated `type()` function call. Replace with `get_type()`. ```cpp fkyaml::node::node_t t = n.type(); ``` -------------------------------- ### Recommended Replacement Usage Source: https://fktn-k.github.io/fkYAML/api/basic_node/set_yaml_version Shows the recommended replacement for `set_yaml_version` using `set_yaml_version_type` with `fkyaml::yaml_version_type::VERSION_1_2`. ```cpp n.set_yaml_version_type(fkyaml::yaml_version_type::VERSION_1_2); ``` -------------------------------- ### Move Constructor Source: https://fktn-k.github.io/fkYAML/api/basic_node/constructor Creates a new fkyaml::node object by moving resources from an existing one. ```APIDOC ## Move Constructor ### Description Creates a new fkyaml::node object by efficiently transferring ownership of resources from a specified `basic_node` object. The source object will be left in a valid but unspecified state. ### Method `fkyaml::node(fkyaml::node&& other)` ### Parameters #### Path Parameters - **other** (fkyaml::node&&) - Required - The node object to move from. ### Example ```cpp #include #include fkyaml::node source_node(fkyaml::node::node_t::BOOLEAN); fkyaml::node moved_node(std::move(source_node)); ``` ```