marzer / tomlplusplus

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

Is it possible to add support for custom types? #175

Open CJCombrink opened 2 years ago

CJCombrink commented 2 years ago

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

Is it possible to add TOML support for custom types, example:

struct Point {
    int32_t x = 10;
    int32_t y = 20;
}

Which would probably look like the following in TOML

[custom_point]
x = 10
y = 20

Describe the solution you'd like

I would like to use it as follow:

const Point my_point = tbl["custom_point"].value<Point>();

Additional context

I am not yet using the library but working thought the API docs I don't see any mention of such options.

marzer commented 2 years ago

Nope, there's no way to do that currently. It has been vaguely on my to-do list for a while, but I'm unlikely to get time to add the plumbing for this any time soon. I believe toml11 has something for that though; if you're deserializing a lot of different struct types you may find that library a bit more flexible in this area.

CJCombrink commented 2 years ago

Thanks for the pointer. I have looked at toml11. But, I like the API of this library a lot more. I can probably get around this "limitation" for the sake of the cleaner API and then hope in future I can throw away boilerplate code when this gets added.

I am used to JSON for Modern C++ and this interface is quite familiar. Perhaps there are some lessens to be learned from their support for Custom data sources in my code or this library. (I am guessing you are familiar with this nlohmann-json)

marzer commented 2 years ago

(I am guessing you are familiar with this nlohmann-json)

Yup, the way that library handles this particular bit of functionality is very nice. One thing to be mindful of though is that TOML is chiefly a config language, whereas JSON is also aimed at serialization, so being able to convert between TOML and structs of arbitrary complexity isn't stricly a design goal I'm ever going to work towards.

Having said that, I would still like to make it easier for obvious common cases like points, vectors et cetera - I've had to come up with my own workarounds for that in my own code that uses this library 😅

bobfang1992 commented 1 year ago

I wonder how this is possible without proper reflection support in C++.

marzer commented 1 year ago

It's not in the general case; it always boils down to manually-written operator overloads or ADL tricks. You can use template machinery (e.g. SFINAE, partial specialisation) to cover some predefined cases pretty easily, though.