### Install toml++ with Meson Source: https://github.com/marzer/tomlplusplus/blob/master/README.md Shows how to install the toml++ wrap using the Meson build system and then use it as a dependency. ```meson meson wrap install tomlplusplus tomlplusplus_dep = dependency('tomlplusplus') ``` -------------------------------- ### Install toml++ with Vcpkg Source: https://github.com/marzer/tomlplusplus/blob/master/README.md Command to install the toml++ library using the vcpkg package manager. ```shell vcpkg install tomlplusplus ``` -------------------------------- ### Install build tools on Linux Source: https://github.com/marzer/tomlplusplus/blob/master/toml-test/README.md Installs Python, pip, ninja, and meson required for building on Linux systems. ```bash sudo apt update && sudo apt install -y python3 python3-pip ninja-build sudo pip3 install meson ``` -------------------------------- ### Add Example Target with CMake Source: https://github.com/marzer/tomlplusplus/blob/master/examples/CMakeLists.txt Defines a CMake function to add an executable example, link it against tomlplusplus, and set C++17 as the standard. It also creates a custom target to run the example. ```cmake cmake_minimum_required(VERSION 3.14) project(Examples LANGUAGES CXX) include(../cmake/project-is-top-level.cmake) if(PROJECT_IS_TOP_LEVEL) find_package(tomlplusplus REQUIRED) endif() add_custom_target(run_examples COMMENT "Running all examples") function(add_example name) cmake_parse_argument(PARSE_ARGV 1 "" "" "" ARGS) add_executable("${name}" "${name}.cpp") target_link_libraries("${name}" PRIVATE tomlplusplus::tomlplusplus) target_compile_features("${name}" PRIVATE cxx_std_17) add_custom_target("run_${name}" COMMAND "${name}" ${_ARGS} VERBATIM) add_dependencies(run_examples "run_${name}") endfunction() add_example(error_printer) add_example(parse_benchmark) add_example(simple_parser) add_example(toml_generator) add_example(toml_merger) add_example(toml_to_json_transcoder ARGS "${PROJECT_SOURCE_DIR}/example.toml") ``` -------------------------------- ### Install Python Dependencies Source: https://github.com/marzer/tomlplusplus/blob/master/CONTRIBUTING.md Install the required Python packages for code generation. This is a prerequisite before running the generation script. ```bash pip3 install -r tools/requirements.txt ``` -------------------------------- ### Install tomlplusplus Modules Source: https://github.com/marzer/tomlplusplus/blob/master/src/modules/CMakeLists.txt Installs the tomlplusplus modules library and C++ module files if the TOMLPLUSPLUS_ENABLE_INSTALL option is set. This makes the modules available for external projects. ```cmake if(TOMLPLUSPLUS_ENABLE_INSTALL) install(TARGETS tomlplusplus_modules EXPORT tomlplusplus-targets LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} FILE_SET CXX_MODULES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/tomlplusplus/modules ) endif() ``` -------------------------------- ### Install pytomlpp using pip Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Installs the pytomlpp Python wrapper library using pip. This is a convenient way to integrate toml++ functionality into Python projects. ```shell pip install pytomlpp ``` -------------------------------- ### Example JSON Output Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md This is an example of the JSON output generated from a TOML structure using toml::json_formatter. ```json { "author" : { "github" : "https://github.com/marzer", "name" : "Mark Gillard", "twitter" : "https://twitter.com/marzer8789" }, "cpp" : [ 17, 20, "and beyond" ], "lib" : "toml++", "repo" : "https://github.com/marzer/tomlplusplus/", "toml" : [ "1.0.0", "and beyond" ] } ``` -------------------------------- ### Add toml++ to DDS Project Source: https://github.com/marzer/tomlplusplus/blob/master/README.md Example of how to declare toml++ as a dependency in a DDS project's package.json5 file. ```plaintext depends: [ 'tomlpp^3.4.0', ] ``` -------------------------------- ### Configure and build with Meson on Linux Source: https://github.com/marzer/tomlplusplus/blob/master/toml-test/README.md Sets up the Meson build environment for toml-test with release build type and enables toml-test building. ```bash # create the meson build target folder (first time only) meson build_tt --buildtype=release -Dbuild_tt=true -Dgenerate_cmake_config=false ``` -------------------------------- ### Build and Test on Linux/WSL Source: https://github.com/marzer/tomlplusplus/blob/master/CONTRIBUTING.md Commands to set up Meson build configurations, build the project with Ninja, and run tests for both debug and release builds. Ensure locales are generated correctly for international character support. ```bash # install ninja, meson, locales (first time only) sudo apt update && sudo apt install -y locales python3 python3-pip ninja-build sudo pip3 install meson sudo locale-gen 'en_US.utf8' \ 'ja_JP.utf8' \ 'de_DE.utf8' \ 'it_IT.utf8' \ 'tr_TR.utf8' \ 'fi_FI.utf8' \ 'fr_FR.utf8' \ 'zh_CN.utf8' # create the build configs (first time only) meson setup build-debug --buildtype=debug -Ddevel=true meson setup build-release --buildtype=release -Ddevel=true # run the tests cd build-debug && ninja && ninja test \ && cd ../build-release && ninja && ninja test \ && cd .. ``` -------------------------------- ### Add toml++ using Tipi.build Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md In tipi.build projects, add the 'marzer/tomlplusplus' entry to your .tipi/deps file. ```json { "marzer/tomlplusplus": { } } ``` -------------------------------- ### Build and run toml-test on Linux Source: https://github.com/marzer/tomlplusplus/blob/master/toml-test/README.md Builds the project using ninja and runs the toml-test decoder and encoder. ```bash # build and run cd build_tt ninja && toml-test ./toml-test/tt_decoder && toml-test ./toml-test/tt_encoder --encoder ``` -------------------------------- ### Fetch tomlplusplus using CMake Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Integrates the tomlplusplus library into a CMake project using FetchContent. Ensure CMake is recent enough to support FetchContent. ```cmake FetchContent_MakeAvailable(tomlplusplus) ``` -------------------------------- ### Include toml++ using CMake FetchContent Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Use CMake's FetchContent module to declare and download the tomlplusplus library from its GitHub repository. ```cmake include(FetchContent) FetchContent_Declare( tomlplusplus GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git GIT_TAG v3.4.0 ) ``` -------------------------------- ### Add toml++ to Tipi.build Project Source: https://github.com/marzer/tomlplusplus/blob/master/README.md Configuration snippet for adding toml++ as a dependency in a tipi.build project's .tipi/deps file. ```json { "marzer/tomlplusplus": {} } ``` -------------------------------- ### Parse and Access TOML Data Source: https://github.com/marzer/tomlplusplus/blob/master/README.md Demonstrates how to parse a TOML file, access key-value pairs, and retrieve values with defaults. Also shows how to modify the TOML data and iterate over its contents. ```cpp #include using namespace std::literals; auto config = toml::parse_file( "configuration.toml" ); // get key-value pairs std::string_view library_name = config["library"]["name"].value_or(""sv); std::string_view library_author = config["library"]["authors"][0].value_or(""sv); int64_t depends_on_cpp_version = config["dependencies"]["cpp"].value_or(0); // modify the data config.insert_or_assign("alternatives", toml::array{ "cpptoml", "toml11", "Boost.TOML" }); // use a visitor to iterate over heterogenous data config.for_each([](auto& key, auto& value) { std::cout << value << "\n"; if constexpr (toml::is_string) do_something_with_string_values(value); }); // you can also iterate more 'traditionally' using a ranged-for for (auto&& [k, v] : config) { // ... } // re-serialize as TOML std::cout << config << "\n"; // re-serialize as JSON std::cout << toml::json_formatter{ config } << "\n"; // re-serialize as YAML std::cout << toml::yaml_formatter{ config } << "\n"; ``` -------------------------------- ### Parse TOML File in C++ Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Demonstrates how to parse a TOML file using `toml::parse_file`. Handles potential `toml::parse_error` exceptions. ```cpp #include #include // or alternatively: import tomlplusplus; // if C++20 or later int main(int argc, char** argv) { toml::table tbl; try { tbl = toml::parse_file(argv[1]); std::cout << tbl << "\n"; } catch (const toml::parse_error& err) { std::cerr << "Parsing failed:\n" << err << "\n"; return 1; } return 0; } ``` -------------------------------- ### Link to tomlplusplus Library Source: https://github.com/marzer/tomlplusplus/blob/master/src/modules/CMakeLists.txt Links the tomlplusplus_modules target to the tomlplusplus::tomlplusplus library. This is necessary for the modules to use the core tomlplusplus functionality. ```cmake target_link_libraries(tomlplusplus_modules PUBLIC tomlplusplus::tomlplusplus ) ``` -------------------------------- ### Add toml++ as a Git Submodule Source: https://github.com/marzer/tomlplusplus/blob/master/README.md Instructions for adding the toml++ repository as a Git submodule to your project. ```git git submodule add --depth 1 https://github.com/marzer/tomlplusplus.git tomlplusplus ``` -------------------------------- ### Discover and Add C++ Modules Source: https://github.com/marzer/tomlplusplus/blob/master/src/modules/CMakeLists.txt Discovers all .cppm files recursively and adds them as C++ modules to a library target. Ensures C++20 standard is used. ```cmake cmake_minimum_required(VERSION 3.28) file(GLOB_RECURSE TOMLPLUSPLUS_MODULES *.cppm) add_library(tomlplusplus_modules) target_sources(tomlplusplus_modules PUBLIC FILE_SET CXX_MODULES FILES ${TOMLPLUSPLUS_MODULES} ) target_include_directories(tomlplusplus_modules PRIVATE ${tomlplusplus_SOURCE_DIR}/include ) target_compile_features(tomlplusplus_modules PUBLIC cxx_std_20) ``` -------------------------------- ### Configure Fuzzer Build with CMake Source: https://github.com/marzer/tomlplusplus/blob/master/fuzzing/CMakeLists.txt This CMake script configures the build for a fuzzing target. It sets up the project, includes necessary modules, defines build flags based on environment variables, finds the tomlplusplus package, and links the fuzzing engine. ```cmake cmake_minimum_required(VERSION 3.14) project(Fuzzer LANGUAGES CXX) include(../cmake/project-is-top-level.cmake) add_definitions(-DNDEBUG) # Do not want assertions if (DEFINED ENV{CFLAGS}) set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} $ENV{CFLAGS}") endif () if (DEFINED ENV{CXXFLAGS}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} $ENV{CXXFLAGS}") endif () if(PROJECT_IS_TOP_LEVEL) find_package(tomlplusplus REQUIRED) endif() add_executable(toml_fuzzer toml_fuzzer.cpp) target_link_libraries(toml_fuzzer PRIVATE tomlplusplus::tomlplusplus $ENV{LIB_FUZZING_ENGINE}) target_compile_features(toml_fuzzer PRIVATE cxx_std_17) if (DEFINED ENV{OUT}) install(TARGETS toml_fuzzer DESTINATION $ENV{OUT}) else () message(WARNING "Cannot install if $OUT is not defined!") endif () ``` -------------------------------- ### Parse TOML String and Iostream in C++ Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Shows how to parse TOML data directly from a string view or an input stream using `toml::parse`. Includes error handling for `toml::parse_error`. ```cpp #include #include #include using namespace std::string_view_literals; int main() { static constexpr std::string_view some_toml = R"( [library] name = "toml++" authors = ["Mark Gillard "] cpp = 17 )"sv; try { // parse directly from a string view: { toml::table tbl = toml::parse(some_toml); std::cout << tbl << "\n"; } // parse from a string stream: { std::stringstream ss{ std::string{ some_toml } }; toml::table tbl = toml::parse(ss); std::cout << tbl << "\n"; } } catch (const toml::parse_error& err) { std::cerr << "Parsing failed:\n" << err << "\n"; return 1; } return 0; } ``` -------------------------------- ### Generate Single-Header File Source: https://github.com/marzer/tomlplusplus/blob/master/CONTRIBUTING.md Run the script to regenerate the single-header toml.hpp file. This should be done after making changes to the source files in the 'include' directory. ```bash tools/generate_single_header.py ``` -------------------------------- ### Serialize TOML Data to TOML Format Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Serialize a `toml::table` into TOML format by printing it to an output stream using `operator<<`. ```cpp #include #include int main() { auto tbl = toml::table{ { "lib", "toml++" }, { "cpp", toml::array{ 17, 20, "and beyond" } }, { "toml", toml::array{ "1.0.0", "and beyond" } }, { "repo", "https://github.com/marzer/tomlplusplus/" }, { "author", toml::table{ { "name", "Mark Gillard" }, { "github", "https://github.com/marzer" }, { "twitter", "https://twitter.com/marzer8789" } } }, }; // serializing as TOML std::cout << "###### TOML ######" << "\n\n"; ``` -------------------------------- ### Run toml-test encoder on Windows Source: https://github.com/marzer/tomlplusplus/blob/master/toml-test/README.md Executes the toml-test encoder against the compiled tt_encoder executable, using the --encoder flag. ```bash toml-test ./bin/win64_vc143_Release_Application/tt_encoder.exe --encoder ``` -------------------------------- ### Run toml-test decoder on Windows Source: https://github.com/marzer/tomlplusplus/blob/master/toml-test/README.md Executes the toml-test decoder against the compiled tt_decoder executable. ```bash toml-test ./bin/win64_vc143_Release_Application/tt_decoder.exe ``` -------------------------------- ### Serialize TOML to YAML Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Use toml::yaml_formatter to serialize a TOML table into a YAML string. The toml::yaml_formatter must be included in the output stream. ```cpp std::cout << "###### YAML ######" << "\n\n"; std::cout << toml::yaml_formatter{ tbl } << "\n\n"; ``` -------------------------------- ### Enable toml++ Implementation in One Translation Unit Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md After disabling header-only mode, define TOML_IMPLEMENTATION in a single translation unit before including the toml++ header to compile the library's implementation. ```cpp #define TOML_IMPLEMENTATION #include "global_header_that_includes_toml++.hpp" ``` -------------------------------- ### Parse TOML and Access Data Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Parse a TOML string into a `toml::table` and access various data types using `toml::node_view`. Demonstrates direct value access, type casting, and chained access for nested elements. ```cpp #include #include using namespace std::string_view_literals; int main() { static constexpr auto source = R"( str = "hello world" numbers = [ 1, 2, 3, "four", 5.0 ] vegetables = [ "tomato", "onion", "mushroom", "lettuce" ] minerals = [ "quartz", "iron", "copper", "diamond" ] [animals] cats = [ "tiger", "lion", "puma" ] birds = [ "macaw", "pigeon", "canary" ] fish = [ "salmon", "trout", "carp" ] )"sv; toml::table tbl = toml::parse(source); // different ways of directly querying data std::optional str1 = tbl["str"].value(); std::optional str2 = tbl["str"].value(); std::string_view str3 = tbl["str"].value_or(""sv); std::string& str4 = tbl["str"].ref(); // ~~dangerous~~ std::cout << *str1 << "\n"; std::cout << *str2 << "\n"; std::cout << str3 << "\n"; std::cout << str4 << "\n"; // get a toml::node_view of the element 'numbers' using operator[] auto numbers = tbl["numbers"]; std::cout << "table has 'numbers': " << !!numbers << "\n"; std::cout << "numbers is an: " << numbers.type() << "\n"; std::cout << "numbers: " << numbers << "\n"; // get the underlying array object to do some more advanced stuff if (toml::array* arr = numbers.as_array()) { // visitation with for_each() helps deal with heterogeneous data arr->for_each([](auto&& el) { if constexpr (toml::is_number) (*el)++; else if constexpr (toml::is_string) el = "five"sv; }); // arrays are very similar to std::vector arr->push_back(7); arr->emplace_back(8, 9); std::cout << "numbers: " << numbers << "\n"; } // node-views can be chained to quickly query deeper std::cout << "cats: " << tbl["animals"]["cats"] << "\n"; std::cout << "fish[1]: " << tbl["animals"]["fish"][1] << "\n"; // can also be retrieved via absolute path std::cout << "cats: " << tbl.at_path("animals.cats") << "\n"; std::cout << "fish[1]: " << tbl.at_path("animals.fish[1]") << "\n"; // ...even if the element doesn't exist std::cout << "dinosaurs: " << tbl["animals"]["dinosaurs"] << "\n"; //no dinosaurs :( return 0; } ``` -------------------------------- ### Serialize TOML to JSON Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Use toml::json_formatter to serialize a TOML table into a JSON string. Ensure the toml::json_formatter is included in the output stream. ```cpp std::cout << "###### JSON ######" << "\n\n"; std::cout << toml::json_formatter{ tbl } << "\n\n"; ``` -------------------------------- ### Disable Header-Only Mode for toml++ Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md To optimize compilation times, set TOML_HEADER_ONLY to 0 before including the toml++ header. This should be done consistently across your project. ```cpp #define TOML_HEADER_ONLY 0 #include ``` -------------------------------- ### Handle TOML Parse Errors Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Catch `toml::parse_error` to access source file path, line/column information, and error descriptions for custom error handling. ```cpp #include #include toml::table tbl; try { tbl = toml::parse_file("configuration.toml"); } catch (const toml::parse_error& err) { std::cerr << "Error parsing file '" << *err.source().path << "':\n" << err.description() << "\n (" << err.source().begin << ")\n"; return 1; } ``` -------------------------------- ### Handle TOML Parsing Errors Without Exceptions in C++ Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md Demonstrates parsing TOML files when exceptions are disabled. The `toml::parse_file` function returns a `toml::parse_result`, which can be checked for errors. ```cpp #include #define TOML_EXCEPTIONS 0 // only necessary if you've left them enabled in your compiler #include int main() { toml::parse_result result = toml::parse_file("configuration.toml"); if (!result) { std::cerr << "Parsing failed:\n" << result.error() << "\n"; return 1; } do_stuff_with_your_config(std::move(result).table()); // 'steal' the table from the result return 0; } ``` -------------------------------- ### Disable Parser Feature in toml++ Source: https://github.com/marzer/tomlplusplus/blob/master/docs/pages/main_page.md To further speed up compilation, disable the parser by setting TOML_ENABLE_PARSER to 0 if you only need to serialize TOML data and not parse it. ```cpp #define TOML_ENABLE_PARSER 0 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.