### Compiling toml11 Examples Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md These console commands demonstrate how to build the example programs provided with toml11. The first command configures CMake to enable example building, and the second command compiles the project, generating executables in the `examples/` directory. ```console cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON cmake --build ./build/ ``` -------------------------------- ### Building toml11 with Boost Examples using CMake Source: https://github.com/toruniina/toml11/blob/main/examples/boost_container/README.md This command initializes the CMake build system for the `toml11` project, creating a build directory and enabling the compilation of examples. It specifically activates the `TOML11_BUILD_EXAMPLES` option, which often requires a compatible Boost installation (v1.81.0+ for `unordered_flat_map`). ```bash $ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON ``` -------------------------------- ### Building toml11 with Examples Enabled using CMake Source: https://github.com/toruniina/toml11/blob/main/examples/boost_multiprecision/README.md This command initiates the CMake build process for the `toml11` library, creating a build directory at `./build/` and enabling the compilation of examples. This setup is typically used when `toml11` is configured to work with `boost::multiprecision`. ```Shell $ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON ``` -------------------------------- ### Installing toml11 via CMake Commands Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md These console commands demonstrate how to build and install the toml11 library globally or to a specified prefix using CMake. The first command configures the build with tests enabled, and the second command performs the installation. ```console cmake -B ./build/ -DTOML11_BUILD_TESTS=ON cmake --install ./build/ --prefix=/opt/toml11 ``` -------------------------------- ### Building toml11 Tests (Example Build Command) Source: https://github.com/toruniina/toml11/blob/main/README.md This snippet, found under the 'Building Tests' section, shows how to configure and build the example programs for the toml11 library. It uses the CMake option `TOML11_BUILD_EXAMPLES=ON`, which compiles examples rather than the unit tests mentioned in the surrounding text. ```console $ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON $ cmake --build ./build/ ``` -------------------------------- ### Configuring 'key_example' Executable with CMake Source: https://github.com/toruniina/toml11/blob/main/examples/parse_file/CMakeLists.txt This CMake snippet defines an executable named `key_example` from `key_example.cpp`, links it privately against the `toml11::toml11` library, and sets its runtime output directory to the current source directory. This configuration is for an example demonstrating key handling in TOML. ```CMake add_executable(key_example key_example.cpp) target_link_libraries(key_example PRIVATE toml11::toml11) set_target_properties(key_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") ``` -------------------------------- ### Building toml11 Examples via CMake Source: https://github.com/toruniina/toml11/blob/main/README.md This snippet provides console commands to configure and build the example programs included with the toml11 library. It sets the CMake option `TOML11_BUILD_EXAMPLES` to `ON` to enable the compilation of examples located in the `examples/` directory. ```console $ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON $ cmake --build ./build/ ``` -------------------------------- ### Building toml11 with Examples (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/README.md This shell command configures and generates the build system for the `toml11` library using CMake. The `-DTOML11_BUILD_EXAMPLES=ON` flag ensures that the example programs are also built, and `-B ./build/` specifies the output build directory. ```Shell $ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON ``` -------------------------------- ### Building TOML11 Examples with CMake Source: https://github.com/toruniina/toml11/blob/main/examples/unicode/README.md This command builds the TOML11 project using CMake, enabling the examples by setting the `TOML11_BUILD_EXAMPLES` option to `ON`. It creates a build directory named `build` in the current location. ```Shell $ cmake -B ./build/ -DTOML11_BUILD_EXAMPLES=ON ``` -------------------------------- ### Using Installed toml11 as Header-Only Library Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md Even if toml11 is installed as a precompiled library, this CMake snippet shows how to use it as a header-only library. It finds the package to get `TOML11_INCLUDE_DIR` and then adds this directory to your target's include paths, avoiding direct linking. ```cmake find_package(toml11) add_executable(main main.cpp) # Include only, do not link target_include_directories(main PRIVATE ${TOML11_INCLUDE_DIR}) ``` -------------------------------- ### Using Installed toml11 with CMake `find_package` Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md This CMake snippet shows how to locate and link an already installed toml11 library using `find_package`. Once found, the `toml11::toml11` target can be linked to your executable, allowing your project to use the installed library. ```cmake find_package(toml11) add_executable(main main.cpp) target_link_libraries(main PRIVATE toml11::toml11) ``` -------------------------------- ### Installing toml11 Library via CMake Source: https://github.com/toruniina/toml11/blob/main/README_ja.md This console command sequence outlines the steps to install the `toml11` library using CMake. It involves cloning the repository, updating submodules, configuring the build with CMake, building the project, and finally installing the library to a specified prefix path. ```console $ git clone https://github.com/ToruNiina/toml11 $ cd toml11 $ git submodule update --init --recursive $ cmake -B ./build/ $ cmake --build ./build/ $ cmake --install ./build --prefix /path/to/toml11 ``` -------------------------------- ### Configuring 'array_example' Executable with CMake Source: https://github.com/toruniina/toml11/blob/main/examples/parse_file/CMakeLists.txt This CMake snippet defines an executable named `array_example` from `array_example.cpp`, links it privately against the `toml11::toml11` library, and sets its runtime output directory to the current source directory. This example focuses on demonstrating array parsing in TOML. ```CMake add_executable(array_example array_example.cpp) target_link_libraries(array_example PRIVATE toml11::toml11) set_target_properties(array_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") ``` -------------------------------- ### Configuring and Installing toml11 CMake Package and Headers Source: https://github.com/toruniina/toml11/blob/main/src/CMakeLists.txt This section handles the generation of CMake package configuration files and the installation of the `toml11` library's headers. It uses `write_basic_package_version_file` and `configure_package_config_file` to create discoverable CMake modules, and `install` commands to place headers and the library target in specified installation directories. ```CMake include(CMakePackageConfigHelpers) write_basic_package_version_file(${TOML11_CONFIG_VERSION} VERSION ${toml11_VERSION} COMPATIBILITY SameMajorVersion ) configure_package_config_file( ${PROJECT_SOURCE_DIR}/cmake/toml11Config.cmake.in ${TOML11_CONFIG} INSTALL_DESTINATION ${TOML11_INSTALL_CMAKE_DIR} PATH_VARS TOML11_INSTALL_CMAKE_DIR TOML11_INSTALL_INCLUDE_DIR ) install(FILES ${TOML11_CONFIG} ${TOML11_CONFIG_VERSION} DESTINATION ${TOML11_INSTALL_CMAKE_DIR}) install(FILES ${TOML11_ROOT_HEADER} DESTINATION ${TOML11_INSTALL_INCLUDE_DIR} ) install(FILES ${TOML11_MAIN_HEADERS} DESTINATION ${TOML11_INSTALL_INCLUDE_DIR}/toml11 ) install(FILES ${TOML11_FWD_HEADERS} DESTINATION ${TOML11_INSTALL_INCLUDE_DIR}/toml11/fwd ) install(FILES ${TOML11_IMPL_HEADERS} DESTINATION ${TOML11_INSTALL_INCLUDE_DIR}/toml11/impl ) install(TARGETS toml11 EXPORT toml11Targets) install(EXPORT toml11Targets FILE toml11Targets.cmake DESTINATION ${TOML11_INSTALL_CMAKE_DIR} NAMESPACE toml11:: ) ``` -------------------------------- ### Installing toml11 Library via CMake Source: https://github.com/toruniina/toml11/blob/main/README.md This snippet provides a sequence of console commands to manually clone, build, and install the toml11 library using CMake. It covers cloning the repository, updating submodules, configuring the build directory, compiling the project, and finally installing it to a specified prefix path. ```console $ git clone https://github.com/ToruNiina/toml11 $ cd toml11 $ git submodule update --init --recursive $ cmake -B ./build/ $ cmake --build ./build/ $ cmake --install ./build/ --prefix /path/to/toml11 ``` -------------------------------- ### Setting Installation and Configuration Directories in CMake Source: https://github.com/toruniina/toml11/blob/main/CMakeLists.txt This section includes the `GNUInstallDirs` module to leverage standard GNU installation directory variables. It then defines custom CMake variables for the installation paths of `toml11`'s CMake configuration files and include directories, as well as temporary configuration file paths within the build directory. These variables standardize where build artifacts and configuration files will be placed upon installation. ```CMake include(GNUInstallDirs) set(TOML11_INSTALL_CMAKE_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/toml11) set(TOML11_INSTALL_INCLUDE_DIR ${CMAKE_INSTALL_INCLUDEDIR}) set(TOML11_CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR}/cmake) set(TOML11_CONFIG ${TOML11_CONFIG_DIR}/toml11Config.cmake) set(TOML11_CONFIG_VERSION ${TOML11_CONFIG_DIR}/toml11ConfigVersion.cmake) ``` -------------------------------- ### Configuring 'array_of_tables_example' Executable with CMake Source: https://github.com/toruniina/toml11/blob/main/examples/parse_file/CMakeLists.txt This CMake snippet defines an executable named `array_of_tables_example` from `array_of_tables_example.cpp`, links it privately against the `toml11::toml11` library, and sets its runtime output directory to the current source directory. This specific example illustrates handling arrays of tables in TOML. ```CMake add_executable(array_of_tables_example array_of_tables_example.cpp) target_link_libraries(array_of_tables_example PRIVATE toml11::toml11) set_target_properties(array_of_tables_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") ``` -------------------------------- ### Including FetchContent Module (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This line includes the `FetchContent` CMake module, which allows for downloading and integrating external projects directly into the build system. It's a prerequisite for using `FetchContent_Declare` and `FetchContent_MakeAvailable`. ```CMake include(FetchContent) ``` -------------------------------- ### Example Usage of array_format_info in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/format.md This C++ example demonstrates how to manipulate the output format of a TOML array using `toml::array_format` and `toml::array_format_info`. It shows how to switch between single-line and multi-line formats, and how to customize indentation for multi-line arrays. ```cpp #include #include int main() { toml::value a(toml::array{ 3, 1, 4 }); a.as_array_fmt().fmt = toml::array_format::oneline; std::cout << "a = " << a << std::endl; // a = [3, 1, 4] a.as_array_fmt().fmt = toml::array_format::multiline; std::cout << "a = " << a << std::endl; // a = [ // 3, // 1, // 4, // ] a.as_array_fmt().body_indent = 4; a.as_array_fmt().closing_indent = 2; std::cout << "a = " << a << std::endl; // a = [ // 3, // 1, // 4, // ] return 0; } ``` -------------------------------- ### Using toml11 with CMake `FetchContent` Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md This CMake configuration uses `FetchContent` to automatically download the toml11 library from its GitHub repository at a specific tag. After making it available, the snippet shows how to link the `toml11::toml11` target to your executable, ensuring the library is included in your build. ```cmake include(FetchContent) FetchContent_Declare( toml11 GIT_REPOSITORY https://github.com/ToruNiina/toml11.git GIT_TAG v4.4.0 ) FetchContent_MakeAvailable(toml11) add_executable(main main.cpp) target_link_libraries(main PRIVATE toml11::toml11) ``` -------------------------------- ### Building and Running toml11 Tests Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md These console commands outline the process for building and executing the toml11 test suite. It involves updating Git submodules, configuring CMake with test building enabled, compiling the project, and finally running the tests using CTest. ```console git submodule update --init --recursive cmake -B ./build/ -DTOML11_BUILD_TESTS=ON cmake --build ./build/ ctest --test-dir ./build/ ``` -------------------------------- ### Specializing `toml::into` for `extlib::foo` Example in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/into.md This example demonstrates specializing `toml::into` for a custom struct `extlib::foo`. It defines an `into_toml` static method that converts an `extlib::foo` object into a `toml::basic_value` representing a TOML table, mapping `a` and `b` members to corresponding TOML fields. This enables seamless conversion of `extlib::foo` instances into TOML values. ```C++ namespace extlib { struct foo { int a; std::string b; }; } // extlib #include namespace toml { template<> struct into { template static toml::basic_value into_toml(const extlib::foo& f) { using value_type = toml::basic_value; using table_type = typename value_type::table_type; return value_type(table_type{{"a", f.a}, {"b", f.b}}); } }; } // toml ``` -------------------------------- ### Building and Running toml-lang/toml-tests Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md These console commands detail how to build and run the `toml-lang/toml-tests` suite against toml11. It requires updating Git submodules, configuring CMake to build the specific `toml11_decoder` and `toml11_encoder` test executables, compiling, and then running the tests with CTest. ```console git submodule update --init --recursive cmake -B ./build/ -DTOML11_BUILD_TOML_TESTS=ON cmake --build ./build/ ctest --test-dir ./build/ ``` -------------------------------- ### C++ Example: Formatting TOML Tables with `toml11` Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/format.md This C++ example demonstrates how to programmatically create a `toml::value` with nested tables and dynamically change its output format using `toml::table_format`. It shows how to switch between default multiline, dotted, and oneline table representations, illustrating the effect on the serialized TOML output. ```C++ #include #include int main() { toml::value t(toml::table{ {"a", 42}, {"pi", 3.14}, {"foo", "bar"}, { "table", toml::table{ {"a", 42}, {"pi", 3.14}, {"foo", "bar"} } }, }); std::cout << t << std::endl; // pi = 3.14 // foo = "bar" // a = 42 // // [table] // pi = 3.14 // foo = "bar" // a = 42 // root table cannot be dotted. // t.as_table_fmt().fmt = toml::table_format::dotted; t.at("table").as_table_fmt().fmt = toml::table_format::dotted; std::cout << t << std::endl; // table.pi = 3.14 // table.foo = "bar" // table.a = 42 // pi = 3.14 // foo = "bar" // a = 42 t.as_table_fmt().fmt = toml::table_format::oneline; std::cout << t << std::endl; // {table = {pi = 3.14, foo = "bar", a = 42}, pi = 3.14, foo = "bar", a = 42} return 0; } ``` -------------------------------- ### Making External Project Available (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This command makes the previously declared `boost_ext_reflect` project available to the current CMake build. It triggers the download and configuration of the external project, allowing its targets and properties to be used. ```CMake FetchContent_MakeAvailable(boost_ext_reflect) ``` -------------------------------- ### Adding Executable Target (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This command defines an executable target named `reflect` from the source file `reflect.cpp`. This is the primary step to create a runnable program within the CMake project. ```CMake add_executable(reflect reflect.cpp) ``` -------------------------------- ### TOML Example for Mixed Key and Index Access with toml::find Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/features/value.md A TOML snippet demonstrating an array of tables, where elements can be accessed using a mix of array indices and table keys. ```toml a = [ {b = 1}, {b = 2}, {b = 3} ] ``` -------------------------------- ### Linking Precompiled toml11 Static Library Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md This CMake command demonstrates how to link a precompiled toml11 static library to a target in your project. Using `target_link_libraries` with `PUBLIC toml11::toml11` ensures that your target correctly uses the precompiled library. ```cmake target_link_libraries(your_target PUBLIC toml11::toml11) ``` -------------------------------- ### Example TOML Syntax Error Message Source: https://github.com/toruniina/toml11/blob/main/README.md This snippet displays an example of a `toml::syntax_error` message generated by the toml11 library when parsing invalid TOML. It pinpoints the exact location of the error, explains the issue (e.g., invalid underscore placement), and provides hints for valid and invalid syntax. ```console [error] bad integer: `_` must be surrounded by digits --> internal string at line 64 in file main.cpp | 1 | a = 123__456 | ^-- invalid underscore Hint: valid : -42, 1_000, 1_2_3_4_5, 0xC0FFEE, 0b0010, 0o755 Hint: invalid: _42, 1__000, 0123 ``` -------------------------------- ### Compiling toml11 as a Static Library Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md This console command configures CMake to precompile parts of the toml11 library into a static library. Setting `-DTOML11_PRECOMPILE=ON` can help reduce overall compilation time for projects that frequently use toml11. ```console cmake -B ./build/ -DTOML11_PRECOMPILE=ON ``` -------------------------------- ### Adding Include Directories for Target (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This command adds the source directory of the `boost_ext_reflect` external project to the private include paths for the `reflect` executable. This ensures that the compiler can find header files from the `boost_ext_reflect` library. ```CMake target_include_directories(reflect PRIVATE ${boost_ext_reflect_SOURCE_DIR} ) ``` -------------------------------- ### Integrating toml11 with CMake `add_subdirectory` Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md This CMake snippet demonstrates how to include the toml11 library as a subdirectory within your project. It adds the toml11 source tree and then links the `toml11::toml11` target to your executable, making the library's functionalities available. ```cmake add_subdirectory(toml11) add_executable(main main.cpp) target_link_libraries(main PUBLIC toml11::toml11) ``` -------------------------------- ### Linking Libraries to Executable (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This command links the `toml11::toml11` library to the `reflect` executable target. The `PRIVATE` keyword indicates that `toml11::toml11` is only required for building `reflect` and not for targets that link against `reflect`. ```CMake target_link_libraries(reflect PRIVATE toml11::toml11) ``` -------------------------------- ### Example Usage of Integer Formatting in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/format.md This C++ example demonstrates how to apply various formatting options to an integer `toml::value` using `integer_format_info`. It shows changing the radix to hexadecimal, enabling uppercase letters, adding a spacer, and setting a minimum width with leading zeros, illustrating the flexibility in integer representation. ```cpp #include #include int main() { toml::value v(0xDEADBEEF); std::cout << v << std::endl; // 3735928559 v.as_integer_fmt().uppercase = true; v.as_integer_fmt().fmt = toml::integer_format::hex; std::cout << v << std::endl; // 0xDEADBEEF v.as_integer_fmt().spacer = 4; std::cout << v << std::endl; // 0xDEAD_BEEF v.as_integer_fmt().spacer = 8; v.as_integer_fmt().width = 16; std::cout << v << std::endl; // 0x00000000_DEADBEEF } ``` -------------------------------- ### Setting Runtime Output Directory for Target (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This command sets a property for the `reflect` executable target, specifically its `RUNTIME_OUTPUT_DIRECTORY`. It configures the executable to be placed in the current source directory after compilation, which can be useful for development and testing. ```CMake set_target_properties(reflect PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") ``` -------------------------------- ### Setting C++ Standard for Target (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This command specifies that the `reflect` executable target should be compiled using the C++20 standard. The `PRIVATE` keyword ensures this setting applies only to the `reflect` target itself. ```CMake target_compile_features(reflect PRIVATE cxx_std_20) ``` -------------------------------- ### Adding toml11 using CMake Package Manager (CPM) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/installation/_index.md This CMake snippet illustrates how to integrate toml11 using CPM.cmake. It provides two methods: a simple one-liner for adding the package and a more detailed approach allowing custom options like `TOML11_PRECOMPILE` and `TOML11_ENABLE_ACCESS_CHECK` before linking the library to an executable. ```cmake include(cmake/CPM.cmake) CPMAddPackage("gh:ToruNiina/toml11@4.4.0") # OR CPMAddPackage( NAME toml11 GITHUB_REPOSITORY "ToruNiina/toml11" VERSION 4.4.0 OPTIONS "TOML11_PRECOMPILE ON" # to pre-compile "TOML11_ENABLE_ACCESS_CHECK ON" # to use value.accessed() ) add_executable(main main.cpp) target_link_libraries(main PUBLIC toml11::toml11) ``` -------------------------------- ### TOML Example for Recursive Access with toml::find Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/features/value.md A TOML snippet demonstrating a nested table structure (`a.b.c`) that can be accessed recursively using `toml::find`. ```toml a = {b = {c = 42}} ``` -------------------------------- ### Applying String Formatting Options in TOML11 (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/format.md This example illustrates how to apply different string formatting styles (basic, literal, multiline basic, multiline literal) to `toml::value` objects. It also demonstrates how to control the `start_with_newline` property for multiline strings, affecting their output appearance. ```C++ #include #include int main() { toml::value s("foo"); std::cout << s << std::endl; // "foo" s.as_string_fmt().fmt = toml::string_format::literal; std::cout << s << std::endl; // 'foo' s.as_string_fmt().fmt = toml::string_format::multiline_basic; std::cout << s << std::endl; // """foo""" s.as_string_fmt().fmt = toml::string_format::multiline_literal; std::cout << s << std::endl; // '''foo''' toml::value multiline("foo\nbar"); std::cout << multiline << std::endl; // "foo\nbar" multiline.as_string_fmt().fmt = toml::string_format::multiline_basic; std::cout << multiline << std::endl; // """foo // bar""" multiline.as_string_fmt().start_with_newline = true; std::cout << multiline << std::endl; // """ // foo // bar""" multiline.as_string_fmt().fmt = toml::string_format::multiline_literal; std::cout << multiline << std::endl; // ''' // foo // bar''' return 0; } ``` -------------------------------- ### Customizing Floating-Point Formatting with `floating_format_info` in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.ja/docs/reference/format.md This example demonstrates how to use `floating_format_info` to control the output format and precision of floating-point `toml::value`s. It shows switching between default, fixed, and scientific notations, and adjusting the number of decimal places. ```C++ #include #include int main() { toml::value pi(3.141592653589793); std::cout << pi << std::endl; // 3.14159 pi.as_floating_fmt().fmt = toml::floating_format::fixed; std::cout << pi << std::endl; // 3.141593 pi.as_floating_fmt().prec = 16; std::cout << pi << std::endl; // 3.1415926535897931 toml::value na(6.02214076e+23); std::cout << na << std::endl; // 6.022e+23 na.as_floating_fmt().fmt = toml::floating_format::fixed; std::cout << na << std::endl; // 602214075999999987023872.000000 na.as_floating_fmt().fmt = toml::floating_format::scientific; std::cout << na << std::endl; // 6.022141e+23 na.as_floating_fmt().prec = 16; std::cout << na << std::endl; // 6.0221407599999999e+23 return 0; } ``` -------------------------------- ### Example: Enabling Error Message Colorization - C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/color.md Demonstrates how to use `toml::color::enable()` to activate error message colorization. Once called, all subsequent error messages will be displayed with ANSI colors, improving readability in compatible terminals. ```C++ #include int main() { toml::color::enable(); // All subsequent errors will be colored. const auto input = toml::parse("input.toml"); return 0; } ``` -------------------------------- ### TOML Example for Quoted Keys Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/features/value.md Demonstrates TOML's quoted keys feature, allowing special characters like '.' within key names without implying nested tables. Keys like `"127.0.0.1"` and `site."google.com"` are shown. ```toml "127.0.0.1" = "value" site."google.com" = true ``` -------------------------------- ### Declaring External Project with FetchContent (CMake) Source: https://github.com/toruniina/toml11/blob/main/examples/reflect/CMakeLists.txt This block declares an external project, `boost_ext_reflect`, using `FetchContent`. It specifies the Git repository URL, enables shallow cloning for faster downloads, and pins the project to a specific Git tag (`v1.1.1`). This prepares the external project for inclusion in the build. ```CMake FetchContent_Declare( boost_ext_reflect GIT_REPOSITORY https://github.com/boost-ext/reflect GIT_SHALLOW ON # Download the branch without its history GIT_TAG v1.1.1 ) ``` -------------------------------- ### Example: Checking Colorization Status - C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/color.md Shows how to use `toml::color::should_color()` to programmatically determine the current state of error message colorization. The output will be `true` if colors are enabled, and `false` otherwise, formatted as a boolean string. ```C++ #include #include #include int main() { std::cout << "colorized? : " << std::boolalpha << toml::color::should_color() << std::endl; return 0; } ``` -------------------------------- ### Defining Non-Intrusive Conversion with `TOML11_DEFINE_CONVERSION_NON_INTRUSIVE` Macro in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/features/value.md Shows how to automatically generate the `toml::from` definition for a custom type `extlib::foo` using the `TOML11_DEFINE_CONVERSION_NON_INTRUSIVE` macro. This macro simplifies the conversion setup by automatically mapping struct members `a` and `b`. ```C++ namespace extlib { struct foo { int a; std::string b; }; } // extlib TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(extlib::foo, a, b) ``` -------------------------------- ### Compiling and Running the expand Tool (Console) Source: https://github.com/toruniina/toml11/blob/main/tools/expand/README.md This snippet illustrates the command-line steps to compile the `expand` tool using `g++` with C++20 standard and optimization, and then execute it. The `expand` tool processes `../../include/toml.hpp` and outputs the consolidated content to `../../single_include/toml.hpp`, creating a single-file header for `toml11`. ```console $ g++-13 -std=c++20 -O2 main.cpp -o expand $ ./expand ../../include/toml.hpp > ../../single_include/toml.hpp ``` -------------------------------- ### Enabling User-Defined Type Conversion for TOML (C++) Source: https://github.com/toruniina/toml11/blob/main/README.md This example demonstrates how to make a user-defined struct (`extlib::foo`) convertible from TOML values using the `TOML11_DEFINE_CONVERSION_NON_INTRUSIVE` macro. It allows `toml::find` to directly populate instances of custom types from TOML tables. ```C++ namespace extlib { struct foo { int a; std::string b; }; } // extlib TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(extlib::foo, a, b) // ... const auto input = R"( [foo] a = 42 b = "bar" )"_toml; const extlib::foo f = toml::find(input, "foo"); ``` -------------------------------- ### Example TOML Mixed-Type Array Source: https://github.com/toruniina/toml11/blob/main/README.md This TOML snippet defines a `mixed_array` containing an integer, a double, and an inline table. It serves as an input example for advanced type conversions in C++. ```TOML mixed_array = [ 42, 3.14, {a = "foo", b = "bar"} ] ``` -------------------------------- ### Customizing Integer Formatting with `integer_format_info` in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.ja/docs/reference/format.md This example demonstrates how to use `integer_format_info` to customize the output format of an integer `toml::value`. It shows setting the base to hexadecimal, enabling uppercase, adding a spacer, and specifying a minimum width with leading zeros. ```C++ #include #include int main() { toml::value v(0xDEADBEEF); std::cout << v << std::endl; // 3735928559 v.as_integer_fmt().uppercase = true; v.as_integer_fmt().fmt = toml::integer_format::hex; std::cout << v << std::endl; // 0xDEADBEEF v.as_integer_fmt().spacer = 4; std::cout << v << std::endl; // 0xDEAD_BEEF v.as_integer_fmt().spacer = 8; v.as_integer_fmt().width = 16; std::cout << v << std::endl; // 0x00000000_DEADBEEF } ``` -------------------------------- ### Retrieving `std::forward_list` with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding an `array` into a `std::forward_list`. It requires `T` to be a `std::forward_list` specialization. A `toml::type_error` is thrown if the `toml::value` is not an array. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Retrieving `std::string_view` with TOML11 `get` (C++17) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload, available in C++17+, converts a `toml::basic_value` holding a `string_type` into a `std::string_view`. It requires `T` to be `std::string_view`. A `toml::type_error` is thrown if the `toml::value` does not contain a string. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Configuring 'spec_example' Executable with CMake Source: https://github.com/toruniina/toml11/blob/main/examples/parse_file/CMakeLists.txt This CMake snippet defines an executable named `spec_example` from `spec_example.cpp`, links it privately against the `toml11::toml11` library, and sets its runtime output directory to the current source directory. This ensures the executable is built and placed in a predictable location for testing TOML specification parsing. ```CMake add_executable(spec_example spec_example.cpp) target_link_libraries(spec_example PRIVATE toml11::toml11) set_target_properties(spec_example PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}") ``` -------------------------------- ### Mixed Key and Index Access with toml::find (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/features/value.md Illustrates how `toml::find` can navigate complex TOML structures by combining key names and array indices. This example accesses the 'b' key within the third element (index 2) of the 'a' array. ```cpp const auto a_2_b = toml::find(input, "a", 2, "b"); // Equivalent to: const auto a = toml::get(input.at("a").at(2).at("c")); ``` -------------------------------- ### Defining a Basic TOML Configuration File Source: https://github.com/toruniina/toml11/blob/main/README_ja.md This TOML snippet defines a simple configuration file named `example.toml`. It includes a string `title` and an array of integers `nums`, demonstrating basic key-value pairs and array syntax in TOML. A comment is also included to describe the `nums` array. ```toml # example.toml title = "an example toml file" nums = [3, 1, 4, 1, 5] # pi! ``` -------------------------------- ### Example of toml11 Type Error Message Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/features/value.md This snippet shows an example of the error message generated by `toml::type_error` when a type mismatch occurs, specifically when `as_string()` is called on a `toml::value` that contains an integer. The message indicates a bad cast and the actual type. ```text [error] toml::value::as_string(): bad_cast to string --> input.toml | 1 | a = 123_456 | ^^^^^^^-- the actual type is integer ``` -------------------------------- ### Retrieving `std::chrono::duration` with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding a `local_time` into a `std::chrono::duration` type. It requires `T` to be a `std::chrono::duration` specialization. A `toml::type_error` is thrown if the `toml::value` does not contain a local time. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Retrieving Floating-Point Types with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding a `floating_type` into a specified C++ floating-point type `T`. It requires `T` to be a floating-point type but not `toml::value::floating_type`. A `toml::type_error` is thrown if the `toml::value` does not contain a floating-point value. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Retrieving Integer Types with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding an `integer_type` into a specified C++ integral type `T`. It requires `T` to be an integral type but not `bool` or `toml::value::integer_type`. A `toml::type_error` is thrown if the `toml::value` does not contain an integer. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Example `toml::type_error` Message Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This snippet shows an example error message from `toml::type_error` when `toml::get` fails due to an incompatible type conversion, such as attempting to retrieve an integer from a TOML table. The message details the error type, the specific cast failure, and the location in the input file. ```text terminate called after throwing an instance of 'toml::type_error' what(): toml::value::as_integer(): bad_cast to integer --> input.toml | 6 | [fruit] | ^^^^^^^-- the actual type is table ``` -------------------------------- ### Retrieving `std::array` with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding an `array` into a `std::array`. It requires `T` to be a `std::array` specialization. A `toml::type_error` is thrown if the `toml::value` is not an array, and `std::out_of_range` is thrown if the TOML array has insufficient elements. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Customizing `toml::spec` Features Example in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/spec.md This example demonstrates how to create a `toml::spec` object for a base version (v1.0.0) and then manually enable specific features from a newer version (v1.1.0), such as allowing newlines in inline tables. The customized `spec` object is then passed to `toml::parse` to control parsing behavior. ```C++ auto spec = toml::spec::v(1, 0, 0); // Allow newlines in inline tables in addition to v1.0.0 features. // Other v1.1.0 features are not enabled. spec.v1_1_0_allow_newlines_in_inline_tables = true; auto input = toml::parse("input_file.toml", spec); ``` -------------------------------- ### Retrieving `std::chrono::system_clock::time_point` with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding a `local_date`, `local_datetime`, or `offset_datetime` into a `std::chrono::system_clock::time_point`. It requires `T` to be `std::chrono::system_clock::time_point`. A `toml::type_error` is thrown if the `toml::value` is not one of the expected date/time types. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Example Usage of `toml::visit` with Custom Visitor in C++ Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/visit.md This example demonstrates how to implement a custom visitor, `type_name_of`, to determine the underlying type of a `toml::value`. It defines overloaded `operator()` for various TOML types, allowing `toml::visit` to dispatch to the correct handler and print the type name of a `toml::value` instance. ```cpp #include #include struct type_name_of { std::string operator()(const toml::value::boolean_type &) const {return "boolean";} std::string operator()(const toml::value::integer_type &) const {return "integer";} std::string operator()(const toml::value::floating_type &) const {return "floating";} std::string operator()(const toml::value::string_type &) const {return "string";} std::string operator()(const toml::value::local_time_type &) const {return "local_time";} std::string operator()(const toml::value::local_date_type &) const {return "local_date";} std::string operator()(const toml::value::local_datetime_type &) const {return "local_datetime";} std::string operator()(const toml::value::offset_datetime_type&) const {return "offset_datetime";} std::string operator()(const toml::value::array_type &) const {return "array";} std::string operator()(const toml::value::table_type &) const {return "table";} }; int main() { toml::value v(3.14); std::cout << toml::visit(type_name_of{}, v) << std::endl; // floating return 0; } ``` -------------------------------- ### Illustrating Order Preservation in ordered_map (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/ordered_map.md Demonstrates how `ordered_map` preserves the order of keys within the same table. The first example shows preserved order for `apple` and `orange` properties when defined contiguously. The second example illustrates that order is preserved within nested tables but not across different top-level tables if their properties are interleaved. ```C++ apple.type = "fruit" apple.skin = "thin" apple.color = "red" orange.type = "fruit" orange.skin = "thick" orange.color = "orange" ``` ```C++ apple.type = "fruit" orange.type = "fruit" apple.skin = "thin" orange.skin = "thick" apple.color = "red" orange.color = "orange" ``` -------------------------------- ### Example: Defining Non-Intrusive Conversion for Custom Struct (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/conversion.md This example illustrates the practical application of `TOML11_DEFINE_CONVERSION_NON_INTRUSIVE`. It defines a `foo::Foo` struct with `std::string s`, `double d`, and `int i` members, then uses the macro to automatically generate the necessary conversion functions, allowing `foo::Foo` instances to be easily converted to and from TOML values. ```cpp namespace foo { struct Foo { std::string s; double d; int i; }; } // foo TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(foo::Foo, s, d, i) ``` -------------------------------- ### Configuring Build Options and Dependent Options in CMake Source: https://github.com/toruniina/toml11/blob/main/CMakeLists.txt This section defines various build options for the `toml11` library, including precompilation, access checks, installation, and building examples/tests. It uses `option()` for simple boolean flags and `cmake_dependent_option()` for options that depend on other CMake variables or project states. A policy push/pop is used to manage `CMP0127` for compatibility. It also includes a check to prevent building tests with both ASan and UBSan simultaneously. ```CMake include(CTest) # to use ${BUILD_TESTING} option(TOML11_PRECOMPILE "precompile toml11 library" OFF) option(TOML11_ENABLE_ACCESS_CHECK "enable access check feature (beta)" OFF) include(CMakeDependentOption) cmake_policy(PUSH) if(POLICY CMP0127) cmake_policy(SET CMP0127 OLD) # syntax of condition changed in 3.22 endif() cmake_dependent_option(TOML11_INSTALL "install toml11 library" ON "${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}" OFF) cmake_dependent_option(TOML11_BUILD_EXAMPLES "build toml11 examples" OFF "${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}" OFF) cmake_dependent_option(TOML11_BUILD_TESTS "build toml11 unit tests" OFF "${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}; ${BUILD_TESTING}" OFF) cmake_dependent_option(TOML11_BUILD_TOML_TESTS "build toml11 toml-test encoder & decoder" OFF "${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME}" OFF) cmake_policy(POP) cmake_dependent_option(TOML11_TEST_WITH_ASAN "build toml11 unit tests with asan" OFF "${TOML11_BUILD_TESTS}" OFF) cmake_dependent_option(TOML11_TEST_WITH_UBSAN "build toml11 unit tests with ubsan" OFF "${TOML11_BUILD_TESTS}" OFF) if(${TOML11_TEST_WITH_ASAN} AND ${TOML11_TEST_WITH_UBSAN}) message(FATAL_ERROR "trying to build tests with BOTH asan and ubsan") endif() ``` -------------------------------- ### Retrieving `std::pair` with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding an `array` into a `std::pair`. It requires `T` to be a `std::pair` specialization, recursively converting its elements. A `toml::type_error` is thrown if the `toml::value` is not an array, and `std::out_of_range` is thrown if the TOML array does not contain exactly two elements. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Retrieving Array-Like Types with TOML11 `get` (C++) Source: https://github.com/toruniina/toml11/blob/main/docs/content.en/docs/reference/get.md This `get` overload converts a `toml::basic_value` holding an `array` into a generic array-like C++ container (e.g., `std::vector`, `std::deque`). It applies to types with iterators, `value_type`, and `push_back`, excluding `toml::value::array_type`, `std::string`, `std::string_view`, map-like types, or types with custom `from_toml()`/constructors. A `toml::type_error` is thrown if the `toml::value` is not an array. ```cpp template T get(basic_value& v); ``` -------------------------------- ### Defining a Basic TOML Configuration File Source: https://github.com/toruniina/toml11/blob/main/README.md This TOML snippet defines a simple configuration with a string `title` and an array of integers `nums`. It demonstrates basic key-value pairs and array syntax in TOML, including inline comments. ```TOML # example.toml title = "an example toml file" nums = [3, 1, 4, 1, 5] # pi! ```