marzer / tomlplusplus

Header-only TOML config file parser and serializer for C++17.
https://marzer.github.io/tomlplusplus/
MIT License
1.47k stars 141 forks source link

Optional `get` #180

Closed A-F-V closed 1 year ago

A-F-V commented 1 year ago

Is your feature request related to a problem? Please describe.

I'd like a way to get nodes and sub tables in an optional without using get and checking for nullptr.

Describe the solution you'd like

A function possibly named get_optional that is similiar to value except which operates over tables and arrays and places the subnode inside an optional. For example, if we had a toml:

[outer.inner]
x=5

Then, toml.get_optional<toml::table>("inner") should return std::optional<toml::table> with the inner table as it's value.

marzer commented 1 year ago

This is what node views are for - chain them together to reduce verbosity, e.g. toml["outer"]["inner"]["x"] is safe even if outer didn't exist (it will just result in a null view).

marzer commented 1 year ago

https://marzer.github.io/tomlplusplus/classtoml_1_1node__view.html