### CMake Integration for fkYAML Source: https://context7.com/fktn-k/fkyaml/llms.txt Demonstrates three methods for integrating fkYAML into a CMake project: using an installed package, FetchContent for automatic downloading, or add_subdirectory with a Git submodule. The example shows how to link the fkYAML::fkYAML target to an executable. ```cmake cmake_minimum_required(VERSION 3.8) project(myapp LANGUAGES CXX) # --- Option 1: installed package --- find_package(fkYAML REQUIRED) # --- Option 2: FetchContent (downloads automatically) --- # include(FetchContent) # FetchContent_Declare(fkYAML # GIT_REPOSITORY https://github.com/fktn-k/fkYAML.git # GIT_TAG v0.4.2) # FetchContent_MakeAvailable(fkYAML) # --- Option 3: add_subdirectory (Git submodule) --- # add_subdirectory(path/to/fkYAML) add_executable(myapp main.cpp) # The exported target sets include paths and compile flags automatically target_link_libraries(myapp PUBLIC fkYAML::fkYAML) ``` -------------------------------- ### Building and Installing fkYAML with CMake Source: https://context7.com/fktn-k/fkyaml/llms.txt Provides bash commands for installing the single-header variant of fkYAML and for building a project that uses fkYAML. Ensure to set the CMAKE_INSTALL_PREFIX for installation and CMAKE_BUILD_TYPE for build optimization. ```bash # Install from source (single-header variant with -DFK_YAML_USE_SINGLE_HEADER=ON) cmake -B build -S . -DCMAKE_INSTALL_PREFIX=/usr/local cmake --build build --target install # Build and test your project cmake -B build -S . -DCMAKE_BUILD_TYPE=Release cmake --build build --config Release ``` -------------------------------- ### Install fkYAML with CMake Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/tutorials/index.md Install fkYAML using CMake. Optionally, use -DFK_YAML_USE_SINGLE_HEADER=ON 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 ``` -------------------------------- ### Test and Install Amalgamate Source: https://github.com/fktn-k/fkyaml/blob/develop/tools/amalgamation/README.md Commands to test the amalgamate.py script and then install it to the system's binary path. ```bash ./test.sh && sudo -k cp ./amalgamate.py /usr/local/bin/ ``` -------------------------------- ### Basic Node Example Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/node.md This example demonstrates the basic usage of the `fkyaml::node` type. It shows how to create and manipulate a node, and the expected output. ```cpp --8<-- "apis/basic_node/node.cpp:9" ``` -------------------------------- ### Example Usage of begin() Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/begin.md This example demonstrates how to use the `begin()` method to access the first element of a sequence node. The output shows the value of the first element. ```cpp --8<-- "apis/basic_node/begin.cpp:9" ``` -------------------------------- ### Include Examples Subdirectory Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/CMakeLists.txt Includes the 'apis' subdirectory, enabling CMake to process its CMakeLists.txt. This is used for building example code. ```cmake add_subdirectory(apis) ``` -------------------------------- ### Get value or default C++ Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/get_value_or.md This example demonstrates how to use `get_value_or` to retrieve a value, providing a default if the conversion fails. It shows the behavior when the node contains a convertible value and when it does not. ```cpp fkyaml::basic_node node; // Try to get an integer value, return 0 if it fails. const auto value1 = node.get_value_or(0); // Try to get a string value, return "default" if it fails. const auto value2 = node.get_value_or("default"); // If the node has a value that can be converted to int. node = 10; const auto value3 = node.get_value_or(0); // If the node has a value that can be converted to string. node = "hello"; const auto value4 = node.get_value_or("default"); ``` -------------------------------- ### Example Usage of yaml_version_type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/yaml_version_type.md Demonstrates the usage of the `yaml_version_type` enumeration. This example is part of a larger API demonstration. ```cpp --8<-- "apis/yaml_version_type/yaml_version_type.cpp:9" ``` -------------------------------- ### Example Usage of string_type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/string_type.md Demonstrates the usage of basic_node with string types. This example shows how string values are stored and retrieved. ```cpp --8<-- "apis/basic_node/string_type.cpp:9" ``` -------------------------------- ### Example Usage of serialize Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/serialize.md This example demonstrates how to use the `serialize` function to convert a YAML node into a string. The output will be the YAML representation of the node. ```cpp --8<-- "apis/basic_node/serialize.cpp:9" ``` -------------------------------- ### Example Usage of basic_node::size Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/size.md This example demonstrates how to use the `size()` method on different node types. It shows the expected output for sequence, mapping, and string nodes. ```cpp --8<-- "apis/basic_node/size.cpp:9" ``` -------------------------------- ### Example Usage of map_items() Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/map_items.md A complete example demonstrating the usage of `map_items()` to iterate through a YAML mapping and print its key-value pairs. This example assumes a `mapping` node is already defined and populated. ```cpp --8<-- "apis/basic_node/map_items.cpp:9" ``` -------------------------------- ### CMake Build Configuration for Ordered Map Examples Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/apis/ordered_map/CMakeLists.txt This snippet configures the build process for multiple C++ example executables. It finds all .cpp files, creates an executable for each, links them to a common library, and defines post-build commands to copy necessary configuration files and generate output. ```cmake file(GLOB_RECURSE EX_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) foreach(EX_SRC_FILE ${EX_SRC_FILES}) file(RELATIVE_PATH REL_EX_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR} ${EX_SRC_FILE}) string(REPLACE ".cpp" "" EX_SRC_FILE_BASE ${REL_EX_SRC_FILE}) set(TARGET_NAME "ordered_map_${EX_SRC_FILE_BASE}") add_executable(${TARGET_NAME} ${EX_SRC_FILE}) target_link_libraries(${TARGET_NAME} example_common_config) add_custom_command( TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_YAML_PATH} $ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_MULTI_YAML_PATH} $ COMMAND $ > ${CMAKE_CURRENT_SOURCE_DIR}/${EX_SRC_FILE_BASE}.output WORKING_DIRECTORY $ ) endforeach() ``` -------------------------------- ### Install FK YAML Package Source: https://github.com/fktn-k/fkyaml/blob/develop/CMakeLists.txt Installs the FK YAML package components if FK_YAML_INSTALL is enabled. This includes headers, configuration files, NATVIS files, the library target, and the pkg-config file. ```cmake if(FK_YAML_INSTALL) install( DIRECTORY ${FK_YAML_INCLUDE_BUILD_DIR} DESTINATION ${FK_YAML_INCLUDE_INSTALL_DIR} ) install( FILES ${FK_YAML_CMAKE_PROJECT_CONFIG_FILE} ${FK_YAML_CMAKE_VERSION_CONFIG_FILE} DESTINATION ${FK_YAML_CONFIG_INSTALL_DIR} ) if(FK_YAML_INSTALL_NATVIS) install( FILES ${FK_YAML_NATVIS_FILE} DESTINATION . ) endif() export( TARGETS ${FK_YAML_TARGET_NAME} NAMESPACE ${PROJECT_NAME}:: FILE ${FK_YAML_CMAKE_PROJECT_TARGETS_FILE} ) install( TARGETS ${FK_YAML_TARGET_NAME} EXPORT ${FK_YAML_TARGETS_EXPORT_NAME} ) install( EXPORT ${FK_YAML_TARGETS_EXPORT_NAME} NAMESPACE ${PROJECT_NAME}:: DESTINATION ${FK_YAML_CONFIG_INSTALL_DIR} ) install( FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" DESTINATION ${FK_YAML_PKGCONFIG_INSTALL_DIR} ) endif() ``` -------------------------------- ### Print Library Version Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/macros.md This example demonstrates how to print the major, minor, and patch versions of the fkYAML library using predefined macros. ```cpp #include #include int main() { std::cout << "fkYAML Version: " << FK_YAML_MAJOR_VERSION << "." << FK_YAML_MINOR_VERSION << "." << FK_YAML_PATCH_VERSION << std::endl; return 0; } ``` ```bash fkYAML Version: 0.3.0 ``` -------------------------------- ### Example Usage of integer_type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/integer_type.md Demonstrates the usage of `integer_type` within a `basic_node`. This example shows how integer values are stored and retrieved. ```cpp --8<-- "apis/basic_node/integer_type.cpp:9" ``` -------------------------------- ### Example of using is_null Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/is_null.md This example demonstrates how to use the `is_null()` method to check if a YAML node is null. It requires including the necessary header and setting up a YAML node. ```cpp fkyaml::basic_node node; node.set_null(); if (node.is_null()) { // node is null } ``` -------------------------------- ### Basic Node Mapping Type Example Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/mapping_type.md This example demonstrates the usage of the basic_node with its default mapping type. The output shows the structure and content of a mapping node. ```cpp --8<-- "apis/basic_node/mapping_type.cpp:9" ``` -------------------------------- ### Applying GPL to New Programs Source: https://github.com/fktn-k/fkyaml/blob/develop/LICENSES/GPL-3.0-only.txt Include these notices at the start of each source file to apply the GPL to your program. Ensure copyright information and a link to the full license are present. ```text Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ``` -------------------------------- ### Example of Adding Anchor Name (C++) Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/add_anchor_name.md This example demonstrates how to add an anchor name to a YAML node using the `add_anchor_name` method. ```cpp --8<-- "apis/basic_node/add_anchor_name.cpp:9" ``` -------------------------------- ### Example Usage of yaml_version_t Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/yaml_version_t.md This example demonstrates the usage of the `yaml_version_t` enum. The output shows the expected result of operations involving this enum. ```cpp --8<-- "apis/basic_node/yaml_version_t.cpp:9" ``` -------------------------------- ### Basic fkYAML C++ Project Setup Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/tutorials/index.md Set up a minimal C++ project to load a YAML file using fkYAML. Ensure the YAML file is UTF-8 encoded. ```cmake cmake_minimum_required(VERSION 3.8) project(tutorial LANGUAGES CXX) find_package(fkYAML REQUIRED) add_executable(tutorial tutorial.cpp) # This exported CMake target sets the necessary configurations for the project. target_link_libraries(tutorial PUBLIC fkYAML::fkYAML) ``` ```cpp #include #include #include int main() { std::ifstream ifs("example.yaml"); if (!ifs.is_open()) { std::cerr << "failed to open \"example.yaml\"\n"; return 1; } try { const auto node = fyaml::load(ifs); std::cout << node << std::endl; } catch (const std::exception& e) { std::cerr << "failed to parse \"example.yaml\": " << e.what() << std::endl; return 1; } return 0; } ``` ```yaml key: value list: - item1 - item2 ``` -------------------------------- ### Example Usage of operator<= Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/operator_le.md Demonstrates the usage of the less than or equal to operator for `basic_node` objects. This example requires the definition of nodes and their comparison. ```cpp --8<-- "apis/basic_node/operator_le.cpp:9" ``` -------------------------------- ### CMakeLists.txt for Operator Literal YAML Example Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/apis/operator_literal_yaml/CMakeLists.txt This CMake script configures the build process for multiple C++ examples. It finds all .cpp files, creates an executable for each, links them to 'example_common_config', and defines post-build commands to copy input YAML files and run the executable to generate output. ```cmake file(GLOB_RECURSE EX_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) foreach(EX_SRC_FILE ${EX_SRC_FILES}) file(RELATIVE_PATH REL_EX_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR} ${EX_SRC_FILE}) string(REPLACE ".cpp" "" EX_SRC_FILE_BASE ${REL_EX_SRC_FILE}) set(TARGET_NAME "operator_literal_yaml_${EX_SRC_FILE_BASE}") add_executable(${TARGET_NAME} ${EX_SRC_FILE}) target_link_libraries(${TARGET_NAME} example_common_config) add_custom_command( TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_YAML_PATH} $ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_MULTI_YAML_PATH} $ COMMAND $ > ${CMAKE_CURRENT_SOURCE_DIR}/${EX_SRC_FILE_BASE}.output WORKING_DIRECTORY $ ) endforeach() ``` -------------------------------- ### Example Usage of alias_of Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/alias_of.md This example demonstrates how to use the `alias_of` function to create an alias node. The output shows the resulting YAML structure. ```cpp --8<-- "apis/basic_node/alias_of.cpp:9" ``` -------------------------------- ### Example Usage of Sequence Type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/sequence_type.md This example demonstrates the usage of the sequence type. It shows how sequence objects are stored within a basic_node, utilizing heap allocation for memory efficiency. ```cpp --8<-- "apis/basic_node/sequence_type.cpp:9" ``` -------------------------------- ### Example of using rend() Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/rend.md This example demonstrates how to use the `rend()` function to iterate over a sequence node in reverse. It requires the `fkYAML/node.hpp` header and assumes a `basic_node` object named `node` has been initialized. ```cpp --8<-- "apis/basic_node/rend.cpp:9" ``` -------------------------------- ### Example: Move Assignment of basic_node Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/operator=.md Demonstrates how to use the move assignment operator to transfer the contents of one basic_node to another. ```cpp --8<-- "apis/basic_node/move_assignment_operator.cpp:9" ``` -------------------------------- ### Iterator Example Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/iterator.md Demonstrates the usage of iterators with basic_node containers. ```APIDOC ## Examples ??? Example ```cpp --8<-- "apis/basic_node/iterator.cpp:9" ``` output: ```bash --8<-- "apis/basic_node/iterator.output" ``` ``` -------------------------------- ### Output of get_value example Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/get_value.md The expected output when retrieving a value from a basic_node. ```bash --8<-- "apis/basic_node/get_value.output" ``` -------------------------------- ### Example: Copy Assignment of basic_node Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/operator=.md Demonstrates how to use the copy assignment operator to assign the contents of one basic_node to another. ```cpp --8<-- "apis/basic_node/copy_assignment_operator.cpp:9" ``` -------------------------------- ### Deprecation Warning Example Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/yaml_version_t.md Illustrates the deprecated usage of `fkyaml::basic_node::yaml_version_t` and its replacement with `fkyaml::yaml_version_type`. ```cpp fkyaml::node::yaml_version_t v; ``` ```cpp fkyaml::yaml_version_type v; ``` -------------------------------- ### Example Usage of Extraction Operator Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/extraction_operator.md This example demonstrates how to use the extraction operator to load YAML content from a file into a node. The input file 'apis/input.yaml' is expected to contain valid YAML. ```yaml --8<-- "apis/input.yaml" ``` ```cpp --8<-- "apis/basic_node/extraction_operator.cpp:9" ``` ```bash --8<-- "apis/basic_node/extraction_operator.output" ``` -------------------------------- ### Example usage of contains Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/contains.md This example demonstrates how to use the `contains` method to check for the presence of keys in a YAML mapping. It shows successful and unsuccessful key lookups. ```cpp --8<-- "apis/basic_node/contains.cpp:9" ``` -------------------------------- ### Example Usage of node_type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/node_type.md Demonstrates the usage of the node_type enumeration in a C++ context. This example shows how to interact with the node types, likely for type checking or manipulation within the fkyaml library. ```cpp --8<-- "apis/node_type/node_type.cpp:9" ``` -------------------------------- ### Example Usage of YAML Node Literal Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/operator_literal_yaml.md Demonstrates how to use the `_yaml` literal to create a YAML node from a string. The output shows the deserialized node. ```cpp --8<-- "apis/operator_literal_yaml/operator_literal_yaml.cpp:9" ``` -------------------------------- ### Reverse Iterator Example Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/reverse_iterator.md Demonstrates the usage of reverse iterators for iterating through a basic_node container in reverse. ```cpp --8<-- "apis/basic_node/reverse_iterator.cpp:9" ``` -------------------------------- ### Set YAML Version Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/set_yaml_version.md This example demonstrates how to set the YAML version for a node. The function `set_yaml_version` is deprecated and will be removed in a future version. Use `set_yaml_version_type` instead. ```cpp n.set_yaml_version(fkyaml::node::yaml_version_t::VER_1_2); ``` -------------------------------- ### Integrate fkYAML with find_package Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/tutorials/cmake_integration.md Use this method when a release of fkYAML is installed on your system. It locates the package and links the `fkYAML::fkYAML` target to your executable. ```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) ``` -------------------------------- ### Find and Link fkYAML with CMake Source: https://github.com/fktn-k/fkyaml/blob/develop/tests/cmake_find_package_test/project/CMakeLists.txt Use `find_package` to locate the fkYAML library and `target_link_libraries` to link your executable against it. Ensure fkYAML is installed and discoverable by CMake. ```cmake cmake_minimum_required(VERSION 3.8...3.10) project(CMakeFindPackageTestProject) find_package(fkYAML REQUIRED) add_executable( CMakeFindPackageTest main.cpp ) target_link_libraries( CMakeFindPackageTest PUBLIC fkYAML::fkYAML ) ``` -------------------------------- ### Example Usage of Insertion Operator Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/insertion_operator.md Demonstrates how to use the insertion operator to serialize a node and print its content to standard output. The output will reflect the serialized representation of the node. ```cpp --8<-- "apis/basic_node/insertion_operator.cpp:9" ``` -------------------------------- ### Iterating through a basic_node container Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/iterator.md Demonstrates how to iterate through a basic_node container, accessing its elements. This example shows the typical usage of iterators for traversing nodes. ```cpp --8<-- "apis/basic_node/iterator.cpp:9" ``` -------------------------------- ### Build and Run fkYAML Tutorial Project Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/tutorials/index.md Build the tutorial project using CMake and run the executable to see the YAML content output. ```bash cd /path/to/tutorial/ cmake -B build -S . -DCMAKE_BUILD_TYPE=Release cmake --build build --config Release ``` ```bash key: value list: - item1 - item2 ``` -------------------------------- ### Build and Run fkYAML Benchmark Source: https://github.com/fktn-k/fkyaml/blob/develop/tools/benchmark/README.md Commands to build the fkYAML project with benchmarking enabled and then run the benchmark tool against a specified YAML file. Ensure you replace `{Debug|Release}` with your desired build type. ```bash cd path/to/fkYAML cmake -S . -B build -DCMAKE_BUILD_TYPE={Debug|Release} -DFK_YAML_RUN_BENCHMARK=ON cmake --build build --config {Debug|Release} # You can specify an arbitrary input file such as /foo/bar/sample.yml or /foo/bar/sample.json ./build/tool/benchmark/benchmarker ./tool/benchmark/macos.yml ``` -------------------------------- ### Get Iterator to First Element Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/begin.md Use `begin()` to get a mutable iterator or `cbegin()` to get a constant iterator to the first element of a sequence or mapping node. Ensure the node is a container type before calling. ```cpp iterator begin(); const_iterator begin() const; const_iterator cbegin() const; ``` -------------------------------- ### Example Usage of boolean_type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/boolean_type.md This example demonstrates the usage of the boolean_type within a basic_node. It shows how boolean values are represented and stored. ```cpp --8<-- "apis/basic_node/boolean_type.cpp:9" ``` -------------------------------- ### Fetch fkYAML using CMake Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/home/releases.md Use CMake's FetchContent module to download and include the minimum required files for fkYAML. This reduces download size and simplifies integration for client applications. ```cmake FetchContent_Declare( fkYAML URL https://github.com/fktn-k/fkYAML/releases/download/v0.4.1/fkYAML_min.zip ) FetchContent_MakeAvailable(fkYAML) ``` -------------------------------- ### Create Sequence and Mapping Nodes with Factory Methods Source: https://context7.com/fktn-k/fkyaml/llms.txt Use static factory methods `fkyaml::node::sequence()` and `fkyaml::node::mapping()` for explicit creation of empty containers. Populate them afterwards using `emplace_back` for sequences or map-like assignment for mappings. ```cpp #include #include int main() { // Create an empty sequence, then populate it fkyaml::node seq = fkyaml::node::sequence(); seq.as_seq().emplace_back(1); seq.as_seq().emplace_back(2); seq.as_seq().emplace_back(3); std::cout << seq << std::endl; // - 1 // - 2 // - 3 // Create an empty mapping, then populate it fkyaml::node map = fkyaml::node::mapping(); map["name"] = "Alice"; map["age"] = 30; map["active"] = true; std::cout << map << std::endl; // active: true // age: 30 // name: Alice return 0; } ``` -------------------------------- ### Include Tutorials Subdirectory Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/CMakeLists.txt Includes the 'tutorials' subdirectory, allowing CMake to process its CMakeLists.txt file. Use this to build separate tutorial projects. ```cmake add_subdirectory(tutorials) ``` -------------------------------- ### Example Usage of is_float_number Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/is_float_number.md This example demonstrates how to use the `is_float_number` function to check the type of a YAML node. The output shows the result of the check. ```cpp --8<-- "apis/basic_node/is_float_number.cpp:9" ``` -------------------------------- ### Get boolean node value Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/as_bool.md Use `as_bool()` to get a reference to the boolean value of a node. A `fkyaml::type_error` is thrown if the node is not of boolean type. ```cpp auto node = fkyaml::basic_node( true ); std::cout << node.as_bool() << std::endl; // output: 1 ``` -------------------------------- ### Fetch fkYAML using CMake FetchContent Source: https://github.com/fktn-k/fkyaml/blob/develop/tests/cmake_fetch_content_test/project/CMakeLists.txt Use FetchContent_Declare to specify the Git repository and tag for fkYAML. FetchContent_MakeAvailable then downloads and makes the dependency available for use in your build. ```cmake cmake_minimum_required(VERSION 3.11) project("CMakeFetchContentTestProject") include(FetchContent) FetchContent_Declare( fkYAML GIT_REPOSITORY https://github.com/fktn-k/fkYAML.git GIT_TAG v0.4.2) FetchContent_MakeAvailable(fkYAML) ``` -------------------------------- ### Build and Run fkYAML Unit Tests Source: https://github.com/fktn-k/fkyaml/blob/develop/README.md Execute the unit tests for the fkYAML library. Ensure CMake is in your PATH and specify the build type as Debug. The tests will be built and then executed, with failures reported. ```bash cd path/to/fkYAML cmake -B build -S . -DCMAKE_BUILD_TYPE=Debug -DFK_YAML_BUILD_TEST=ON cmake --build build ctest -C Debug --test-dir build --output-on-failure ``` -------------------------------- ### Construct fkyaml::node from C++ Types Source: https://context7.com/fktn-k/fkyaml/llms.txt Demonstrates constructing fkyaml::node from default (null), scalar types (bool, int, float, string), compatible C++ types, and initializer lists for sequences and mappings. Use initializer lists for concise construction. ```cpp #include #include int main() { // (1) Default: null node fkyaml::node null_node; // (2) Scalar values fkyaml::node bool_node(true); fkyaml::node int_node(42); fkyaml::node float_node(3.14); fkyaml::node str_node("hello"); // (6) Compatible type constructor — wraps any supported C++ value fkyaml::node from_int = 100; fkyaml::node from_str = std::string("world"); // (7) Initializer list: pairs → mapping, otherwise → sequence fkyaml::node seq = {true, false}; // sequence fkyaml::node map = {{"foo", 1024}, {"bar", 2}}; // mapping std::cout << seq << std::endl; // - true - false std::cout << map << std::endl; // foo: 1024 bar: 2 return 0; } ``` -------------------------------- ### Find Include What You Use Tool Source: https://github.com/fktn-k/fkyaml/blob/develop/tools/iwyu/CMakeLists.txt Locates the include-what-you-use executable and checks its version. Ensures the tool is available and reports its version and path. ```cmake find_program(IWYU_TOOL NAMES include-what-you-use iwyu REQUIRED) execute_process( COMMAND ${IWYU_TOOL} --version OUTPUT_VARIABLE IWYU_TOOL_VERSION ERROR_VARIABLE IWYU_TOOL_VERSION ) string(REGEX MATCH "[0-9]+(\.[0-9]+)+" IWYU_TOOL_VERSION "${IWYU_TOOL_VERSION}") message(STATUS "Found iwyu. version: ${IWYU_TOOL_VERSION} path: ${IWYU_TOOL}") ``` -------------------------------- ### fkyaml::node - Node Construction Source: https://context7.com/fktn-k/fkyaml/llms.txt Demonstrates the various ways to construct a `fkyaml::node`, including default construction (null), construction from scalar C++ types, and construction from initializer lists to create sequences and mappings. ```APIDOC ## `fkyaml::node` — Node Construction `fkyaml::node` is the primary value type. It can be default-constructed (null), constructed from any compatible C++ type, or built from an initializer list where a list of 2-element pairs becomes a mapping and any other list becomes a sequence. ```cpp #include #include int main() { // (1) Default: null node fkyaml::node null_node; // (2) Scalar values fkyaml::node bool_node(true); fkyaml::node int_node(42); fkyaml::node float_node(3.14); fkyaml::node str_node("hello"); // (6) Compatible type constructor — wraps any supported C++ value fkyaml::node from_int = 100; fkyaml::node from_str = std::string("world"); // (7) Initializer list: pairs → mapping, otherwise → sequence fkyaml::node seq = {true, false}; // sequence fkyaml::node map = {{"foo", 1024}, {"bar", 2}}; // mapping std::cout << seq << std::endl; std::cout << map << std::endl; return 0; } // Output: // - true // - false // foo: 1024 // bar: 2 ``` ``` -------------------------------- ### Example of using end() iterator Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/end.md This example demonstrates how to use the `end()` iterator to iterate through a sequence node. Ensure the node is a sequence or mapping before calling `end()`, otherwise a `fkyaml::type_error` will be thrown. ```cpp --8<-- "apis/basic_node/end.cpp:9" ``` -------------------------------- ### Get reverse iterators for container nodes Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/rend.md Use `rend()` to get a reverse iterator to the end of a container node. This function is useful for iterating through sequence or mapping nodes from the last element to the first. It throws a `fkyaml::type_error` if the node is not a sequence or mapping. ```cpp reverse_iterator rend(); const_reverse_iterator rend() const; const_reverse_iterator crend() const; ``` -------------------------------- ### Configure Package Files Source: https://github.com/fktn-k/fkyaml/blob/develop/CMakeLists.txt Configures the main package configuration file and version configuration file using templates. Ensure the template files and output paths are correctly defined. ```cmake include(CMakePackageConfigHelpers) configure_file( ${FK_YAML_CMAKE_CONFIG_TEMPLATE} ${FK_YAML_CMAKE_PROJECT_CONFIG_FILE} @ONLY ) configure_file( ${FK_YAML_CMAKE_VERSION_CONFIG_TEMPLATE} ${FK_YAML_CMAKE_VERSION_CONFIG_FILE} @ONLY ) ``` -------------------------------- ### Generate Executables with Post-Build Commands Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/apis/macros/CMakeLists.txt This snippet iterates through C++ source files, creates an executable for each, links it to 'example_common_config', and defines post-build commands to copy YAML files and generate an output file. ```cmake file(GLOB_RECURSE EX_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) foreach(EX_SRC_FILE ${EX_SRC_FILES}) file(RELATIVE_PATH REL_EX_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR} ${EX_SRC_FILE}) string(REPLACE ".cpp" "" EX_SRC_FILE_BASE ${REL_EX_SRC_FILE}) set(TARGET_NAME "macros_${EX_SRC_FILE_BASE}") add_executable(${TARGET_NAME} ${EX_SRC_FILE}) target_link_libraries(${TARGET_NAME} example_common_config) add_custom_command( TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_YAML_PATH} $ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_MULTI_YAML_PATH} $ COMMAND $ > ${CMAKE_CURRENT_SOURCE_DIR}/${EX_SRC_FILE_BASE}.output WORKING_DIRECTORY $ ) endforeach() ``` -------------------------------- ### Create a default ordered_map Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/ordered_map/constructor.md Use the default constructor to create an empty ordered_map. No setup is required. ```cpp auto map = fkyaml::ordered_map(); ``` -------------------------------- ### Generate Pkg-Config File Source: https://github.com/fktn-k/fkyaml/blob/develop/CMakeLists.txt Generates the pkg-config file from a template. This file is used by pkg-config to find the installed library. ```cmake configure_file( "${CMAKE_CURRENT_SOURCE_DIR}/cmake/pkg-config.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" ) ``` -------------------------------- ### Check if Node is Alias C++ Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/is_alias.md Use `is_alias()` to determine if a node is an alias. This method is `noexcept` and does not require any setup. ```cpp bool is_alias() const noexcept; ``` -------------------------------- ### Build Basic Executable Node Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/apis/basic_node/CMakeLists.txt This snippet defines how to build a C++ executable from source files. It links against a common library and sets up post-build commands to copy YAML configuration files and generate an output file. ```cmake file(GLOB_RECURSE EX_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) foreach(EX_SRC_FILE ${EX_SRC_FILES}) file(RELATIVE_PATH REL_EX_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR} ${EX_SRC_FILE}) string(REPLACE ".cpp" "" EX_SRC_FILE_BASE ${REL_EX_SRC_FILE}) set(TARGET_NAME "basic_node_${EX_SRC_FILE_BASE}") add_executable(${TARGET_NAME} ${EX_SRC_FILE}) target_link_libraries(${TARGET_NAME} example_common_config) add_custom_command( TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_YAML_PATH} $ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_MULTI_YAML_PATH} $ COMMAND $ > ${CMAKE_CURRENT_SOURCE_DIR}/${EX_SRC_FILE_BASE}.output WORKING_DIRECTORY $ ) endforeach() ``` -------------------------------- ### Amalgamate Limitations: Macro Includes Source: https://github.com/fktn-k/fkyaml/blob/develop/tools/amalgamation/README.md Example of a complex include directive that amalgamate.py will not handle correctly, as it does not expand macros like HEADER_PATH. ```c #define HEADER_PATH "path/to/header.h" #include HEADER_PATH ``` -------------------------------- ### basic_node::size Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/size.md Gets the size of a node value. Throws a `fkyaml::exception` if a basic_node is neither a sequence, mapping nor string value. ```APIDOC ## basic_node::size ### Description Gets the size of a node value. Throws a `fkyaml::exception` if a basic_node is neither a sequence, mapping nor string value. ### Method ```cpp std::size_t size() const ``` ### Return Value The size of a node value. ### Throws - `fkyaml::exception`: If the node is not a sequence, mapping, or string value. ``` -------------------------------- ### Get Value from basic_node Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/get_value.md Retrieves the native data value from a basic_node. Ensure the node contains a value compatible with the target type. ```cpp --8<-- "apis/basic_node/get_value.cpp:9" ``` -------------------------------- ### Get Value as BasicNodeType Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/get_value.md Converts a node to another fkyaml::basic_node object by copy-constructing it. This is equivalent to returning a copy of the current node. ```cpp template BasicNodeType get_value() const; // (2) ``` -------------------------------- ### Build Executables with Post-Build Commands Source: https://github.com/fktn-k/fkyaml/blob/develop/examples/apis/exception/CMakeLists.txt This snippet iterates through C++ source files, creates an executable for each, links a common library, and defines post-build commands to copy YAML configuration files and execute the built target to generate an output file. ```cmake file(GLOB_RECURSE EX_SRC_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp) foreach(EX_SRC_FILE ${EX_SRC_FILES}) file(RELATIVE_PATH REL_EX_SRC_FILE ${CMAKE_CURRENT_SOURCE_DIR} ${EX_SRC_FILE}) string(REPLACE ".cpp" "" EX_SRC_FILE_BASE ${REL_EX_SRC_FILE}) set(TARGET_NAME "exception_${EX_SRC_FILE_BASE}") add_executable(${TARGET_NAME} ${EX_SRC_FILE}) target_link_libraries(${TARGET_NAME} example_common_config) add_custom_command( TARGET ${TARGET_NAME} POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_YAML_PATH} $ COMMAND ${CMAKE_COMMAND} -E copy_if_different ${INPUT_MULTI_YAML_PATH} $ COMMAND $ > ${CMAKE_CURRENT_SOURCE_DIR}/${EX_SRC_FILE_BASE}.output WORKING_DIRECTORY $ ) endforeach() ``` -------------------------------- ### Get Value Reference C++ Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/get_value_ref.md Demonstrates retrieving a reference to the node's value. This function is deprecated and should be replaced with type-specific accessors. ```cpp template ReferenceType get_value_ref(); template ReferenceType get_value_ref() const; ``` -------------------------------- ### Get Node Type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/get_type.md Call `get_type()` on a node to determine its YAML type. This function is `noexcept` and returns a `node_type` enum value. ```cpp fkyaml::basic_node node; // ... set node value ... const auto type = node.get_type(); if (type == fkyaml::node_type::STRING) { // ... } ``` -------------------------------- ### Reverse Iteration over Sequence/Mapping Nodes in C++ Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/home/releases.md Demonstrates how to iterate over sequence or mapping elements in a reversed order using reverse iterators (rbegin, rend) in C++ with fkYAML. ```cpp // node is [1, 2, 3] for (auto rit = node.rbegin(); rit != node.rend(); ++rit) { std::cout << *rit << std::endl; } // output: // 3 // 2 // 1 ``` -------------------------------- ### Example Usage of float_number_type Source: https://github.com/fktn-k/fkyaml/blob/develop/docs/docs/api/basic_node/float_number_type.md Demonstrates how floating-point numbers, including special values like infinity and NaN, are handled and stored using the `float_number_type`. ```cpp --8<-- "apis/basic_node/float_number_type.cpp:9" ```