Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support std::optional members for TOML11_DEFINE_CONVERSION_NON_INTRUSIVE #260

Merged
merged 1 commit into from
Jul 24, 2024

Conversation

ken-matsui
Copy link
Contributor

This PR enables support for std::optional members for TOML11_DEFINE_CONVERSION_NON_INTRUSIVE. If a user-defined struct has a std::optional member and a TOML value doesn't have the key, the key-value will be std::nullopt. Similarly, if the struct's member holds std::nullopt, the conversion to TOML value will not include the key-value.

struct my_toml
{
  int a;
  std::optional<int> b;
};

TOML11_DEFINE_CONVERSION_NON_INTRUSIVE(my_toml, a, b)

const toml::value v = toml::parse_str("a = 42");
const auto foo = toml::get<my_toml>(v);

assert(foo.a == 42);
assert(!foo.b.has_value());

@ken-matsui ken-matsui force-pushed the support-optional-conversion branch 2 times, most recently from a297e30 to 1da36d6 Compare July 23, 2024 16:03
Copy link
Owner

@ToruNiina ToruNiina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great feature!

However, there is one thing I would like us to consider. I've left a comment, so could you check it?

include/toml11/conversion.hpp Outdated Show resolved Hide resolved
@ken-matsui ken-matsui force-pushed the support-optional-conversion branch from 1da36d6 to 94202a4 Compare July 23, 2024 16:47
@ken-matsui ken-matsui force-pushed the support-optional-conversion branch from 94202a4 to 6d4d953 Compare July 23, 2024 17:04
@ToruNiina ToruNiina merged commit 455fadb into ToruNiina:main Jul 24, 2024
144 checks passed
github-actions bot pushed a commit that referenced this pull request Jul 24, 2024
Support std::optional members for TOML11_DEFINE_CONVERSION_NON_INTRUSIVE 455fadb
@ken-matsui ken-matsui deleted the support-optional-conversion branch July 24, 2024 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants