2022-09-24 15:49:34 +00:00
[![banner ](docs/images/banner.png )][homepage]
2020-04-02 21:39:21 +00:00
[![Releases ](https://img.shields.io/github/v/release/marzer/tomlplusplus?style=flat-square )](https://github.com/marzer/tomlplusplus/releases)
2021-04-18 12:04:46 +00:00
[![C++17 ](docs/images/badge-C++17.svg )][cpp_compilers]
[![TOML ](docs/images/badge-TOML.svg )][v1.0.0]
[![MIT license ](docs/images/badge-license-MIT.svg )](./LICENSE)
2022-04-23 14:28:25 +00:00
[![ci ](https://github.com/marzer/tomlplusplus/actions/workflows/ci.yaml/badge.svg?branch=master )](https://github.com/marzer/tomlplusplus/actions/workflows/ci.yaml)
2021-04-18 12:04:46 +00:00
[![Mentioned in Awesome C++ ](docs/images/badge-awesome.svg )](https://github.com/fffaraz/awesome-cpp)
2022-10-15 08:46:52 +00:00
[![Sponsor ](docs/images/badge-sponsor.svg )](https://github.com/sponsors/marzer)
2022-05-30 18:57:15 +00:00
[![Gitter ](docs/images/badge-gitter.svg )](https://gitter.im/marzer/tomlplusplus)
2020-04-02 21:39:21 +00:00
====
2022-09-24 15:49:34 +00:00
## toml++ homepage
2020-07-02 17:17:23 +00:00
< p align = "center" >
2021-04-18 12:04:46 +00:00
< strong > ✨️ This README is fine, but the < a href = "https://marzer.github.io/tomlplusplus/" > toml++ homepage< / a > is better. ✨️ < / strong >
2020-07-02 17:17:23 +00:00
< / p >
< br >
2022-09-24 15:49:34 +00:00
## Library features
2020-07-02 17:17:23 +00:00
2022-04-25 16:39:34 +00:00
- Header-only (optional!)
- Supports the latest [TOML] release ([v1.0.0]), plus optional support for some unreleased TOML features
- Passes all tests in the [toml-test ](https://github.com/BurntSushi/toml-test ) suite
- Supports serializing to JSON and YAML
- Proper UTF-8 handling (incl. BOM)
2022-09-24 15:49:34 +00:00
- C++17 (plus some C++20 features where available, e.g. experimental support for [char8_t] strings)
2022-04-25 16:39:34 +00:00
- Doesn't require RTTI
- Works with or without exceptions
- Tested on Clang (6+), GCC (7+) and MSVC (VS2019)
- Tested on x64, x86 and ARM
2020-01-13 06:31:49 +00:00
< br >
2022-09-24 15:49:34 +00:00
## Basic usage
2020-01-13 06:31:49 +00:00
2021-04-18 12:04:46 +00:00
> ℹ ️ _The following example favours brevity. If you'd prefer full API documentation and lots of specific code snippets
2020-08-11 15:29:40 +00:00
instead, visit the project [homepage]_
2020-04-03 13:49:33 +00:00
2020-06-24 11:28:20 +00:00
Given a [TOML] file `configuration.toml` containing the following:
2022-09-24 15:49:34 +00:00
2020-02-03 09:12:43 +00:00
```toml
[library]
name = "toml++"
2020-04-02 21:39:21 +00:00
authors = ["Mark Gillard < mark.gillard @ outlook . com . au > "]
2020-02-03 09:12:43 +00:00
[dependencies]
cpp = 17
```
2022-09-24 15:49:34 +00:00
2020-06-24 11:28:20 +00:00
Reading it in C++ is easy with toml++:
2022-09-24 15:49:34 +00:00
2020-01-13 06:31:49 +00:00
```cpp
2020-04-03 13:33:02 +00:00
#include <toml.hpp>
2020-02-03 09:12:43 +00:00
auto config = toml::parse_file( "configuration.toml" );
// get key-value pairs
2020-02-22 14:10:32 +00:00
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);
2020-02-03 09:12:43 +00:00
2020-02-16 13:11:57 +00:00
// modify the data
config.insert_or_assign("alternatives", toml::array{
2020-02-18 21:29:59 +00:00
"cpptoml",
"toml11",
"Boost.TOML"
2020-02-16 13:11:57 +00:00
});
2022-04-25 16:39:34 +00:00
// use a visitor to iterate over heterogenous data
config.for_each([](auto& key, auto& value)
{
std::cout < < value << " \n";
if constexpr (toml::is_string< decltype ( value ) > )
do_something_with_string_values(value);
});
// you can also iterate more 'traditionally' using a ranged-for
2020-04-03 13:33:02 +00:00
for (auto& & [k, v] : config)
2020-02-16 13:11:57 +00:00
{
2022-04-25 16:39:34 +00:00
// ...
2020-02-16 13:11:57 +00:00
}
// re-serialize as TOML
2020-08-11 13:34:03 +00:00
std::cout < < config << " \n";
2020-02-16 13:11:57 +00:00
2020-02-03 09:12:43 +00:00
// re-serialize as JSON
2020-08-11 13:34:03 +00:00
std::cout < < toml::json_formatter { config } << " \n";
2020-02-03 09:12:43 +00:00
2021-11-02 20:13:09 +00:00
// re-serialize as YAML
std::cout < < toml::yaml_formatter { config } << " \n";
2020-02-03 09:12:43 +00:00
2020-01-13 06:31:49 +00:00
```
2022-09-24 15:49:34 +00:00
2020-02-03 09:12:43 +00:00
You'll find some more code examples in the `examples` directory, and plenty more as part of the [API documentation].
2020-01-04 14:21:38 +00:00
< br >
2022-09-24 15:49:34 +00:00
## Adding toml++ to your project
2020-04-02 21:39:21 +00:00
`toml++` comes in two flavours: Single-header and Regular. The API is the same for both.
2021-04-18 12:04:46 +00:00
### 🍦️ Single-header flavour
2022-09-24 15:49:34 +00:00
2022-04-25 16:39:34 +00:00
1. Drop [`toml.hpp`] wherever you like in your source tree
2020-04-02 21:39:21 +00:00
2. There is no step two
2020-01-04 14:21:38 +00:00
2021-04-18 12:04:46 +00:00
### 🍨️ Regular flavour
2022-09-24 15:49:34 +00:00
2022-04-25 16:39:34 +00:00
1. Clone the repository
2. Add `tomlplusplus/include` to your include paths
3. `#include <toml++/toml.h>`
2020-01-04 14:21:38 +00:00
2020-06-08 15:32:25 +00:00
### Conan
2022-09-24 15:49:34 +00:00
2022-08-29 18:28:19 +00:00
Add `tomlplusplus/3.2.0` to your conanfile.
2020-08-11 15:29:40 +00:00
### DDS
2022-09-24 15:49:34 +00:00
2020-08-11 15:29:40 +00:00
Add `tomlpp` to your `package.json5` , e.g.:
2022-09-24 15:49:34 +00:00
```plaintext
2020-08-11 15:29:40 +00:00
depends: [
2022-08-29 18:28:19 +00:00
'tomlpp^3.2.0',
2020-08-11 15:29:40 +00:00
]
```
2022-09-24 15:49:34 +00:00
2021-04-18 12:04:46 +00:00
> ℹ ️ _[What is DDS?](https://dds.pizza/)_
2020-08-11 15:29:40 +00:00
2022-04-29 12:54:50 +00:00
### Tipi.build
`tomlplusplus` can be easily used in [tipi.build ](https://tipi.build ) projects by adding the following entry to your `.tipi/deps` :
```json
{
"marzer/tomlplusplus": { }
}
```
2020-08-11 15:29:40 +00:00
### Vcpkg
2022-09-24 15:49:34 +00:00
```plaintext
2020-08-11 15:29:40 +00:00
vcpkg install tomlplusplus
```
2022-04-25 16:39:34 +00:00
### Meson
2022-09-24 15:49:34 +00:00
2022-04-25 16:39:34 +00:00
You can install the wrap with:
2022-09-24 15:49:34 +00:00
```plaintext
2022-04-25 16:39:34 +00:00
meson wrap install tomlplusplus
```
After that, you can use it like a regular dependency:
2022-09-24 15:49:34 +00:00
```meson
2022-04-25 16:39:34 +00:00
tomlplusplus_dep = dependency('tomlplusplus')
```
You can also add it as a subproject directly.
2021-05-19 10:24:25 +00:00
### CMake FetchContent
2022-09-24 15:49:34 +00:00
```cmake
2021-05-19 10:24:25 +00:00
include(FetchContent)
FetchContent_Declare(
tomlplusplus
GIT_REPOSITORY https://github.com/marzer/tomlplusplus.git
2022-08-29 18:28:19 +00:00
GIT_TAG v3.2.0
2021-05-19 10:24:25 +00:00
)
FetchContent_MakeAvailable(tomlplusplus)
```
2022-09-24 15:49:34 +00:00
2021-05-19 10:24:25 +00:00
> ℹ ️ _[What is FetchContent?](https://cmake.org/cmake/help/latest/module/FetchContent.html)_
2022-04-24 17:21:59 +00:00
### Git submodules
2022-09-24 15:49:34 +00:00
```plaintext
2022-04-24 17:21:59 +00:00
git submodule add --depth 1 https://github.com/marzer/tomlplusplus.git tomlplusplus
git config -f .gitmodules submodule.tomlplusplus.shallow true
```
2022-09-24 15:49:34 +00:00
2022-05-01 07:35:21 +00:00
> ⚠️ The toml++ repository has some submodules of its own, but **they are only used for testing**!
> You should **not** use the `--recursive` option for regular library consumption.
2022-04-24 17:21:59 +00:00
2020-08-11 15:29:40 +00:00
### Other environments and package managers
2022-09-24 15:49:34 +00:00
2022-04-25 16:39:34 +00:00
The C++ tooling ecosystem is a fractal nightmare of unbridled chaos so naturally I'm not up-to-speed with all of the
available packaging and integration options. I'm always happy to see new ones supported, though! If there's some
integration you'd like to see and have the technical know-how to make it happen, feel free to
[make a pull request ](./CONTRIBUTING.md ).
2020-01-04 14:21:38 +00:00
2022-01-03 18:51:03 +00:00
### What about dependencies?
2022-09-24 15:49:34 +00:00
2022-01-03 18:51:03 +00:00
If you just want to consume `toml++` as a regular library then you don't have any dependencies to worry about.
There's a few test-related dependencies to be aware of if you're working on the library, though.
See [CONTRIBUTING] for information.
2020-04-02 21:39:21 +00:00
< br >
2020-01-04 14:21:38 +00:00
2022-09-24 15:49:34 +00:00
## Configuration
2021-10-30 12:56:14 +00:00
A number of configurable options are exposed in the form of preprocessor `#defines` Most likely you
2020-02-27 17:52:10 +00:00
won't need to mess with these at all, but if you do, set them before including toml++.
2020-01-04 14:21:38 +00:00
2021-11-06 16:59:47 +00:00
| Option | Type | Description | Default |
|-----------------------------------|:--------------:|----------------------------------------------------------------------------------------------------------|------------------------|
| `TOML_ASSERT(expr)` | function macro | Sets the assert function used by the library. | `assert()` |
2022-06-06 20:46:04 +00:00
| `TOML_CALLCONV` | define | Calling convention to apply to exported free/static functions. | undefined |
2021-11-06 16:59:47 +00:00
| `TOML_CONFIG_HEADER` | string literal | Includes the given header file before the rest of the library. | undefined |
| `TOML_ENABLE_FORMATTERS` | boolean | Enables the formatters. Set to `0` if you don't need them to improve compile times and binary size. | `1` |
2022-11-15 19:38:28 +00:00
| `TOML_ENABLE_FLOAT16` | boolean | Enables support for the built-in `_Float16` type. | per compiler settings |
2021-11-06 16:59:47 +00:00
| `TOML_ENABLE_PARSER` | boolean | Enables the parser. Set to `0` if you don't need it to improve compile times and binary size. | `1` |
2021-11-07 19:25:42 +00:00
| `TOML_ENABLE_UNRELEASED_FEATURES` | boolean | Enables support for [unreleased TOML language features]. | `0` |
2021-11-06 16:59:47 +00:00
| `TOML_ENABLE_WINDOWS_COMPAT` | boolean | Enables support for transparent conversion between wide and narrow strings. | `1` on Windows |
| `TOML_EXCEPTIONS` | boolean | Sets whether the library uses exceptions. | per compiler settings |
2022-02-12 12:25:43 +00:00
| `TOML_EXPORTED_CLASS` | define | API export annotation to add to classes. | undefined |
| `TOML_EXPORTED_MEMBER_FUNCTION` | define | API export annotation to add to non-static class member functions. | undefined |
| `TOML_EXPORTED_FREE_FUNCTION` | define | API export annotation to add to free functions. | undefined |
| `TOML_EXPORTED_STATIC_FUNCTION` | define | API export annotation to add to static functions. | undefined |
2021-11-06 16:59:47 +00:00
| `TOML_HEADER_ONLY` | boolean | Disable this to explicitly control where toml++'s implementation is compiled (e.g. as part of a library).| `1` |
| `TOML_IMPLEMENTATION` | define | Define this to enable compilation of the library's implementation when `TOML_HEADER_ONLY` == `0` . | undefined |
| `TOML_OPTIONAL_TYPE` | type name | Overrides the `optional<T>` type used by the library if you need [something better than std::optional]. | undefined |
| `TOML_SMALL_FLOAT_TYPE` | type name | If your codebase has a custom 'small float' type (e.g. half-precision), this tells toml++ about it. | undefined |
| `TOML_SMALL_INT_TYPE` | type name | If your codebase has a custom 'small integer' type (e.g. 24-bits), this tells toml++ about it. | undefined |
2020-01-04 14:21:38 +00:00
2021-04-18 12:04:46 +00:00
> ℹ ️ _A number of these have ABI implications; the library uses inline namespaces to prevent you from accidentally
2020-04-02 21:39:21 +00:00
linking incompatible combinations together._
2020-03-28 16:56:59 +00:00
2020-01-04 14:21:38 +00:00
< br >
2022-09-24 15:49:34 +00:00
## TOML Language Support
2020-06-24 11:28:20 +00:00
At any given time the library aims to support whatever the [most recently-released version] of TOML is, with opt-in
2020-04-10 16:46:00 +00:00
support for a number of unreleased features from the [TOML master] and some sane cherry-picks from the
2020-01-04 14:21:38 +00:00
[TOML issues list] where the discussion strongly indicates inclusion in a near-future release.
The library advertises the most recent numbered language version it fully supports via the preprocessor
2020-02-20 21:08:20 +00:00
defines `TOML_LANG_MAJOR` , `TOML_LANG_MINOR` and `TOML_LANG_PATCH` .
2020-01-04 14:21:38 +00:00
2021-11-07 19:25:42 +00:00
### **Unreleased language features:**
2022-09-24 15:49:34 +00:00
2020-01-04 14:21:38 +00:00
- [#516]: Allow newlines and trailing commas in inline tables
2020-04-02 21:39:21 +00:00
- [#562]: Allow hex floating-point values
2020-01-04 14:21:38 +00:00
- [#644]: Support `+` in key names
- [#671]: Local time of day format should support `09:30` as opposed to `09:30:00`
- [#687]: Relax bare key restrictions to allow additional unicode characters
2022-03-06 14:09:14 +00:00
- [#790]: Include an `\e` escape code sequence (shorthand for `\u001B` )
- [#796]: Include an `\xHH` escape code sequence
2022-04-23 09:58:15 +00:00
- [#891]: Allow non-English scripts for unquoted keys
2020-01-04 14:21:38 +00:00
2021-10-30 14:26:05 +00:00
> ℹ ️ _`#define TOML_ENABLE_UNRELEASED_FEATURES 1` to enable these features (see [Configuration](#Configuration))._
2020-01-04 14:21:38 +00:00
2021-04-18 12:04:46 +00:00
### 🔹️ **TOML v1.0.0:**
2022-09-24 15:49:34 +00:00
2020-04-02 21:39:21 +00:00
All features supported, including:
2022-09-24 15:49:34 +00:00
2020-04-02 21:39:21 +00:00
- [#356]: Allow leading zeros in the exponent part of a float
- [#567]: Control characters are not permitted in comments
- [#571]: Allow raw tabs inside strings
- [#665]: Make arrays heterogeneous
2020-10-09 05:50:15 +00:00
- [#766]: Allow comments before commas in arrays
2020-04-02 21:39:21 +00:00
2021-04-18 12:04:46 +00:00
### 🔹️ **TOML v0.5.0:**
2022-09-24 15:49:34 +00:00
2020-04-02 21:39:21 +00:00
All features supported.
2020-01-04 14:21:38 +00:00
< br >
2022-09-24 15:49:34 +00:00
## Contributing
2020-02-24 20:47:00 +00:00
Contributions are very welcome! Either by [reporting issues] or submitting pull requests.
If you wish to submit a pull request, please see [CONTRIBUTING] for all the details you need to get going.
2020-01-04 14:21:38 +00:00
< br >
2022-09-24 15:49:34 +00:00
## License and Attribution
2020-01-04 14:21:38 +00:00
2020-06-24 11:28:20 +00:00
toml++ is licensed under the terms of the MIT license - see [LICENSE].
2020-01-04 14:21:38 +00:00
2020-03-12 15:23:25 +00:00
UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[Flexible and Economical UTF-8 Decoder]'.
2020-01-04 14:21:38 +00:00
2020-06-08 18:47:31 +00:00
### With thanks to:
2022-09-24 15:49:34 +00:00
2021-05-11 21:48:53 +00:00
- **[@beastle9end](https://github.com/beastle9end)** - Made Windows.h include bypass
2020-06-22 22:32:25 +00:00
- **[@bjadamson](https://github.com/bjadamson)** - Reported some bugs and helped design a new feature
- **[@bobfang1992](https://github.com/bobfang1992)** - Reported a bug and created a [wrapper in python ](https://github.com/bobfang1992/pytomlpp )
- **[@GiulioRomualdi](https://github.com/GiulioRomualdi)** - Added cmake+meson support
2022-06-04 13:49:26 +00:00
- **[@jonestristand](https://github.com/jonestristand)** - Designed and implemented the `toml::path` s feature
2022-08-25 05:13:22 +00:00
- **[@kcsaul](https://github.com/kcsaul)** - Fixed a bug
2020-07-13 18:18:04 +00:00
- **[@levicki](https://github.com/levicki)** - Helped design some new features
2022-01-08 13:17:12 +00:00
- **[@moorereason](https://github.com/moorereason)** - Reported a whole bunch of bugs
2020-06-22 22:32:25 +00:00
- **[@mosra](https://github.com/mosra)** - Created the awesome [m.css] used to generate the API docs
- **[@ned14](https://github.com/ned14)** - Reported a bunch of bugs and helped design some new features
- **[@okureta](https://github.com/okureta)** - Reported a bug
2020-06-24 11:28:20 +00:00
- **[@prince-chrismc](https://github.com/prince-chrismc)** - Added toml++ to ConanCenter, and fixed some typos
2020-06-22 22:32:25 +00:00
- **[@rbrugo](https://github.com/rbrugo)** - Helped design a new feature
2020-08-11 15:29:40 +00:00
- **[@Reedbeta](https://github.com/Reedbeta)** - Fixed a bug and added additional Visual Studio debugger native visualizers
2021-05-24 06:53:34 +00:00
- **[@Ryan-rsm-McKenzie](https://github.com/Ryan-rsm-McKenzie)** - Add natvis file to cmake install script
2020-06-22 22:32:25 +00:00
- **[@shdnx](https://github.com/shdnx)** - Fixed a bug on GCC 8.2.0 and some meson config issues
2020-10-10 08:45:53 +00:00
- **[@sneves](https://github.com/sneves)** - Helped fix a number of parser bugs
2021-10-30 14:26:05 +00:00
- **[@sobczyk](https://github.com/sobczyk)** - Reported some bugs
2020-10-22 13:25:26 +00:00
- **[@std-any-emplace](https://github.com/std-any-emplace)** - Reported some bugs
2022-02-12 13:25:20 +00:00
- **[@Tachi107](https://github.com/Tachi107)** - Made some tweaks to meson.build, added compile_library build option
2020-06-22 22:32:25 +00:00
- **[@traversaro](https://github.com/traversaro)** - Added vcpkg support and reported a bunch of bugs
2020-07-30 17:41:28 +00:00
- **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions
2021-10-30 14:26:05 +00:00
- **[@ximion](https://github.com/ximion)** - Added support for installation with meson
2022-09-18 23:52:48 +00:00
- **[@a-is](https://github.com/a-is)** - Fixed a bug
2021-05-11 21:48:53 +00:00
2020-04-01 12:53:10 +00:00
< br >
2022-09-24 15:49:34 +00:00
## Contact
2020-04-01 12:53:10 +00:00
For bug reports and feature requests please consider using the [issues] system here on GitHub. For anything else
though you're welcome to reach out via other means. In order of likely response time:
2022-09-24 15:49:34 +00:00
2022-05-20 14:54:58 +00:00
- Gitter: [marzer/tomlplusplus ](https://gitter.im/marzer/tomlplusplus ) ("Discord for repos")
2020-04-01 12:53:10 +00:00
- Twitter: [marzer8789 ](https://twitter.com/marzer8789 )
- Email: [mark.gillard@outlook.com.au ](mailto:mark.gillard@outlook.com.au )
- Facebook: [marzer ](https://www.facebook.com/marzer )
- LinkedIn: [marzer ](https://www.linkedin.com/in/marzer/ )
2020-02-20 21:08:20 +00:00
[API documentation]: https://marzer.github.io/tomlplusplus/
2020-04-02 21:39:21 +00:00
[homepage]: https://marzer.github.io/tomlplusplus/
2021-11-07 19:25:42 +00:00
[unreleased TOML language features]: #unreleased -language-features
2020-04-02 21:39:21 +00:00
[most recently-released version]: https://github.com/toml-lang/toml/releases
2020-01-04 14:21:38 +00:00
[char8_t]: https://en.cppreference.com/w/cpp/keyword/char8_t
2020-06-24 11:28:20 +00:00
[TOML]: https://toml.io/
2020-01-04 14:21:38 +00:00
[TOML master]: https://github.com/toml-lang/toml/blob/master/README.md
[TOML issues list]: https://github.com/toml-lang/toml/issues
2021-01-13 08:59:15 +00:00
[v1.0.0]: https://toml.io/en/v1.0.0
2020-02-25 22:09:02 +00:00
[CONTRIBUTING]: ./CONTRIBUTING.md
[LICENSE]: ./LICENSE
2020-01-04 14:21:38 +00:00
[Flexible and Economical UTF-8 Decoder]: http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
2020-02-03 09:12:43 +00:00
[cpp_compilers]: https://en.cppreference.com/w/cpp/compiler_support
2020-04-02 21:39:21 +00:00
[reporting issues]: https://github.com/marzer/tomlplusplus/issues/new/choose
2020-04-01 12:53:10 +00:00
[issues]: https://github.com/marzer/tomlplusplus/issues
2020-01-04 14:21:38 +00:00
[#356]: https://github.com/toml-lang/toml/issues/356
[#516]: https://github.com/toml-lang/toml/issues/516
[#562]: https://github.com/toml-lang/toml/issues/562
[#567]: https://github.com/toml-lang/toml/issues/567
[#571]: https://github.com/toml-lang/toml/issues/571
[#644]: https://github.com/toml-lang/toml/issues/644
[#665]: https://github.com/toml-lang/toml/issues/665
[#671]: https://github.com/toml-lang/toml/issues/671
[#687]: https://github.com/toml-lang/toml/issues/687
2020-10-09 05:50:15 +00:00
[#766]: https://github.com/toml-lang/toml/issues/766
2022-03-06 14:09:14 +00:00
[#790]: https://github.com/toml-lang/toml/pull/790
[#796]: https://github.com/toml-lang/toml/pull/796
2022-04-23 09:58:15 +00:00
[#891]: https://github.com/toml-lang/toml/pull/891
2020-03-03 21:28:24 +00:00
[something better than std::optional]: https://github.com/TartanLlama/optional
2020-06-08 18:47:31 +00:00
[m.css]: https://mcss.mosra.cz/documentation/doxygen
2022-04-25 16:39:34 +00:00
[`toml.hpp`]: https://raw.githubusercontent.com/marzer/tomlplusplus/master/toml.hpp