2020-04-03 00:39:21 +03:00
[][homepage]
[](https://github.com/marzer/tomlplusplus/releases)
[][cpp_compilers]
[][cpp_compilers]
[][v1.0.0-rc.1]
[](./LICENSE)
[](https://circleci.com/gh/marzer/tomlplusplus)
[](https://github.com/fffaraz/awesome-cpp)
====
2020-02-26 00:09:02 +02:00
- Header-only
2020-04-03 00:39:21 +03:00
- [TOML v1.0.0-rc.1], plus optional support for some [unreleased TOML language features]
2020-02-23 13:13:18 +02:00
- C++17 (plus some C++20 features where available, e.g. experimental support for char8_t strings)
2020-02-22 16:10:32 +02:00
- Proper UTF-8 handling (incl. BOM)
- Works with or without exceptions
- Doesn't require RTTI
- First-class support for serializing to JSON
2020-02-27 19:52:10 +02:00
- Tested on Clang, GCC and MSVC (VS2019)
2020-03-28 18:56:59 +02:00
- Tested on x64, x86 and ARM
2020-01-13 08:31:49 +02:00
< br >
2020-04-01 15:53:10 +03:00
# Basic usage
2020-01-13 08:31:49 +02:00
2020-04-03 16:49:33 +03:00
> ℹ _The following example favours brevity. If you'd prefer full API documentation and lots of specific code snippets instead, visit the project homepage at **[marzer.github.io/tomlplusplus](https://marzer.github.io/tomlplusplus/)**_
2020-02-03 11:12:43 +02:00
Given a TOML file `configuration.toml` containing the following:
```toml
[library]
name = "toml++"
2020-04-03 00:39:21 +03:00
authors = ["Mark Gillard < mark.gillard @outlook .com.au > "]
2020-02-03 11:12:43 +02:00
[dependencies]
cpp = 17
```
Reading it in C++ is easy with `toml++` :
2020-01-13 08:31:49 +02:00
```cpp
2020-04-03 16:33:02 +03:00
#include <toml.hpp>
#include <fstream> //required for toml::parse_file()
2020-02-03 11:12:43 +02:00
auto config = toml::parse_file( "configuration.toml" );
// get key-value pairs
2020-02-22 16:10:32 +02: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 11:12:43 +02:00
2020-02-16 15:11:57 +02:00
// modify the data
config.insert_or_assign("alternatives", toml::array{
2020-02-18 23:29:59 +02:00
"cpptoml",
"toml11",
"Boost.TOML"
2020-02-16 15:11:57 +02:00
});
// iterate & visit over the data
2020-04-03 16:33:02 +03:00
for (auto& & [k, v] : config)
2020-02-16 15:11:57 +02:00
{
2020-02-18 23:29:59 +02:00
v.visit([](auto& node) noexcept
{
2020-04-23 11:15:13 -04:00
std::cout < < node < < std::endl ;
2020-02-18 23:29:59 +02:00
if constexpr (toml::is_string< decltype ( node ) > )
do_something_with_string_values(node);
});
2020-02-16 15:11:57 +02:00
}
// re-serialize as TOML
std::cout < < config < < std::endl ;
2020-02-03 11:12:43 +02:00
// re-serialize as JSON
std::cout < < toml::json_formatter { config } < < std::endl ;
2020-01-13 08:31:49 +02:00
```
2020-02-03 11:12:43 +02:00
You'll find some more code examples in the `examples` directory, and plenty more as part of the [API documentation].
2020-01-04 16:21:38 +02:00
< br >
2020-01-13 08:31:49 +02:00
# Adding toml++ to your project
2020-04-03 00:39:21 +03:00
`toml++` comes in two flavours: Single-header and Regular. The API is the same for both.
2020-06-08 21:47:31 +03:00
### 🍦 Single-header flavour
2020-04-03 00:39:21 +03:00
1. Drop `toml.hpp` wherever you like in your source tree
2. There is no step two
2020-01-04 16:21:38 +02:00
2020-06-08 21:47:31 +03:00
### 🍨 Regular flavour
2020-01-04 16:21:38 +02:00
1. Add `tomlplusplus/include` to your include paths
2. `#include <toml++/toml.h>`
2020-04-03 00:39:21 +03:00
## _"What about build system X, or package manager Y?"_
2020-06-08 11:32:25 -04:00
Currently there's support for use as a meson submodule. That's the extent of my knowledge in this area; clearly an area
of opportunity! If you would like me to add support for a particular build system or package manager please let me
know by making a [feature request]. Better still, if you have the skills and motivation to add support yourself, I'd
welcome a pull request with a smile and open arms!
### Conan
`toml++` is available through the [Conan-Center-Index ](https://github.com/conan-io/conan-center-index ). You simply need to add `tomlplusplus/1.2.3` to your _conanfile_ to include this in your project. The default options are set for the single-header
flavour, however specifying the option `"multiple_headers": True` is available.
2020-01-04 16:21:38 +02:00
2020-04-03 00:39:21 +03:00
< br >
2020-01-04 16:21:38 +02:00
2020-04-03 00:39:21 +03:00
# Configuration
2020-02-03 11:12:43 +02:00
A number of configurable options are exposed in the form of preprocessor `#defines` . Most likely you
2020-02-27 19:52:10 +02:00
won't need to mess with these at all, but if you do, set them before including toml++.
2020-01-04 16:21:38 +02:00
2020-03-02 12:14:54 +02:00
| Option | Type | Default | Description |
|----------------------------|:--------------:|-----------------------------------|------------------------------------------------------------------------------------------------------------|
| `TOML_ALL_INLINE` | boolean | `1` | Disable this to explicitly control where toml++'s implementation is compiled (e.g. as part of a library). |
2020-03-03 10:10:07 +02:00
| `TOML_API` | define | undefined | API annotation to add to public symbols (e.g. `__declspec(dllexport)` on Windows). |
2020-03-02 12:14:54 +02:00
| `TOML_ASSERT(expr)` | function macro | `assert(expr)` < br > (or undefined) | Sets the assert function used by the library. |
| `TOML_CHAR_8_STRINGS` | boolean | `0` | Uses C++20 [char8_t]-based strings as the toml string data type. **_Experimental!_** |
| `TOML_CONFIG_HEADER` | string literal | undefined | Includes the given header file before the rest of the library. |
2020-03-08 23:54:57 +02:00
| `TOML_EXCEPTIONS` | boolean | per your compiler's settings | Sets whether the library uses exceptions. |
2020-03-02 12:14:54 +02:00
| `TOML_IMPLEMENTATION` | define | undefined | Define this to enable compilation of the library's implementation. Meaningless if `TOML_ALL_INLINE` is `1` .|
| `TOML_LARGE_FILES` | boolean | `0` | Uses 32-bit integers for line and column indices (instead of 16-bit). |
2020-03-03 23:28:24 +02:00
| `TOML_OPTIONAL_TYPE` | type name | undefined | Overrides the `optional<T>` type used by the library if you need [something better than std::optional]. |
2020-04-09 11:13:12 +03:00
| `TOML_PARSER` | boolean | `1` | Disable this to prevent inclusion of the parser-related parts of the library if you don't need them. |
2020-03-02 12:14:54 +02:00
| `TOML_SMALL_FLOAT_TYPE` | type name | undefined | If your codebase has an additional 'small' float type (e.g. half-precision), this tells toml++ about it. |
| `TOML_SMALL_INT_TYPE` | type name | undefined | If your codebase has an additional 'small' integer type (e.g. 24-bits), this tells toml++ about it. |
2020-04-10 19:46:00 +03:00
| `TOML_UNRELEASED_FEATURES` | boolean | `0` | Enables support for [unreleased TOML language features] not yet part of a [numbered version]. |
2020-01-04 16:21:38 +02:00
2020-04-03 00:39:21 +03:00
> ℹ _A number of these have ABI implications; the library uses inline namespaces to prevent you from accidentally
linking incompatible combinations together._
2020-03-28 18:56:59 +02:00
2020-01-04 16:21:38 +02:00
< br >
# TOML Language Support
2020-04-10 19:46:00 +03:00
At any given time `toml++` aims to implement whatever the [most recently-released version] of TOML is, with opt-in
support for a number of unreleased features from the [TOML master] and some sane cherry-picks from the
2020-01-04 16:21:38 +02: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 23:08:20 +02:00
defines `TOML_LANG_MAJOR` , `TOML_LANG_MINOR` and `TOML_LANG_PATCH` .
2020-01-04 16:21:38 +02:00
2020-06-08 21:47:31 +03:00
### 🔸 **Unreleased language features:**
2020-01-04 16:21:38 +02:00
- [#516 ]: Allow newlines and trailing commas in inline tables
2020-04-03 00:39:21 +03:00
- [#562 ]: Allow hex floating-point values
2020-01-04 16:21:38 +02: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
2020-03-12 17:23:25 +02:00
- [#709 ]: Include an \xHH escape code sequence
2020-01-04 16:21:38 +02:00
2020-04-10 19:46:00 +03:00
> ℹ _`#define TOML_UNRELEASED_FEATURES 1` to enable these features (see [Configuration](#Configuration))._
2020-01-04 16:21:38 +02:00
2020-06-08 21:47:31 +03:00
### 🔹 **TOML v1.0.0-rc.1:**
2020-04-03 00:39:21 +03:00
All features supported, including:
- [#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-06-08 21:47:31 +03:00
### 🔹 **TOML v0.5.0:**
2020-04-03 00:39:21 +03:00
All features supported.
2020-01-04 16:21:38 +02:00
< br >
# Contributing
2020-02-24 22:47:00 +02: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 16:21:38 +02:00
< br >
# License and Attribution
2020-02-03 11:12:43 +02:00
`toml++` is licensed under the terms of the MIT license - see [LICENSE].
2020-01-04 16:21:38 +02:00
2020-03-12 17:23:25 +02:00
UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[Flexible and Economical UTF-8 Decoder]'.
2020-01-04 16:21:38 +02:00
2020-06-08 21:47:31 +03:00
### With thanks to:
- **@bjadamson ** - Reported some bugs and helped design a new feature
- **@bobfang1992 ** - Reported a bug and created a [wrapper in python ](https://github.com/bobfang1992/pytomlpp )
- **@GiulioRomualdi ** - Added cmake+meson support
- **@mosra ** - Created the awesome [m.css] used to generate the API docs
- **@ned14 ** - Reported a bunch of bugs and helped design some new features
- **@prince -chrismc** - Added `toml++` to ConanCenter, and fixed some typos
- **@rbrugo ** - helped design a new feature
- **@shdnx ** - Fixed a bug on GCC 8.2.0 and some meson config issues
- **@traversaro ** - Added vcpkg support and reported a bunch of bugs
- **@ximion ** - Added support for installation with meson
2020-04-01 15:53:10 +03:00
< br >
# Contact
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:
- 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 23:08:20 +02:00
[API documentation]: https://marzer.github.io/tomlplusplus/
2020-04-03 00:39:21 +03:00
[homepage]: https://marzer.github.io/tomlplusplus/
2020-02-26 00:09:02 +02:00
[unreleased TOML language features]: #unreleased -toml-features
2020-04-03 00:39:21 +03:00
[most recently-released version]: https://github.com/toml-lang/toml/releases
2020-04-10 19:46:00 +03:00
[numbered version]: https://github.com/toml-lang/toml/releases
2020-01-04 16:21:38 +02:00
[char8_t]: https://en.cppreference.com/w/cpp/keyword/char8_t
[TOML master]: https://github.com/toml-lang/toml/blob/master/README.md
[TOML issues list]: https://github.com/toml-lang/toml/issues
2020-04-03 16:33:02 +03:00
[TOML v1.0.0-rc.1]: https://github.com/toml-lang/toml/blob/master/versions/en/toml-v1.0.0-rc.1.md
[v1.0.0-rc.1]: https://github.com/toml-lang/toml/blob/master/versions/en/toml-v1.0.0-rc.1.md
2020-02-26 00:09:02 +02:00
[CONTRIBUTING]: ./CONTRIBUTING.md
[LICENSE]: ./LICENSE
2020-01-04 16:21:38 +02:00
[Flexible and Economical UTF-8 Decoder]: http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
2020-02-03 11:12:43 +02:00
[cpp_compilers]: https://en.cppreference.com/w/cpp/compiler_support
2020-04-03 00:39:21 +03:00
[reporting issues]: https://github.com/marzer/tomlplusplus/issues/new/choose
[feature request]: https://github.com/marzer/tomlplusplus/issues/new/choose
2020-04-01 15:53:10 +03:00
[issues]: https://github.com/marzer/tomlplusplus/issues
2020-01-04 16:21:38 +02: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
[#622 ]: https://github.com/toml-lang/toml/issues/622
[#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-03-12 17:23:25 +02:00
[#709 ]: https://github.com/toml-lang/toml/pull/709
2020-02-26 00:09:02 +02:00
[LICENSE-utf8-decoder]: ./LICENSE-utf8-decoder
2020-03-03 23:28:24 +02:00
[something better than std::optional]: https://github.com/TartanLlama/optional
2020-06-08 21:47:31 +03:00
[m.css]: https://mcss.mosra.cz/documentation/doxygen