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

Mapping key/value access in range based for loops #458

Merged
merged 7 commits into from
Jan 7, 2025

Conversation

fktn-k
Copy link
Owner

@fktn-k fktn-k commented Jan 6, 2025

This PR adds a new basic_node class API map_items() which allows using iterator member functions, key() and value(), in range-based for loops.
For, since iterator's operator*() returns a reference to a mapping value node, it was impossible to iterate over mapping node referring both to keys and values just as described in the discussion #400.
The map_items() function returns a helper range object which allows accessing those functions in range-based for loops like this:

fkyaml::node mapping = {{"a", 123}, {"b", nullptr}};
for (auto it : mapping.map_items()) {
    std::cout << "key: " << it.key() << "value: " << it.value() << std::endl;
}

// output:
// key: a, value: 123
// key: b, value: null

Furthermore, when this library is compiled with C++17 or better, structured bindings are also usable with mappings.

fkyaml::node mapping = {{"a", 123}, {"b", nullptr}};
for (auto& [key, value] : mapping.map_items()) {
    std::cout << "key: " << key << "value: " << value << std::endl;
}

// output:
// key: a, value: 123
// key: b, value: null

See the API reference page for map_items() for more details.


Pull Request Checklist

Read the CONTRIBUTING.md file for detailed information.

  • Changes are described in the pull request or in a referenced issue.
  • The test suite compiles and runs without any error.
  • The code coverage on your branch is 100%.
  • The documentation is updated if you added/changed a feature.

Please don't

  • The C++11 support varies between different compilers and versions. Please note the list of supported compilers. Some compilers like GCC 4.7 (and earlier), Clang 3.3 (and earlier), or Microsoft Visual Studio 13.0 and earlier are known not to work due to missing or incomplete C++11 support. Please refrain from proposing changes that work around these compiler's limitations with #ifdefs or other means.
  • Please refrain from proposing changes that would break YAML specifications. If you propose a conformant extension of YAML to be supported by the library, please motivate this extension.
  • Please do not open pull requests that address multiple issues.

@fktn-k fktn-k added the new feature new feature possibly with public API changes label Jan 6, 2025
@fktn-k fktn-k added this to the Release v0.4.1 milestone Jan 6, 2025
@fktn-k fktn-k self-assigned this Jan 6, 2025
Copy link

github-actions bot commented Jan 6, 2025

:octocat: Upload Coverage Event Notification

Coverage data has been uploaded for the commit 7ce4c5af4faaabc3cca9c86910d15b85cf7b609e.
You can download the artifact which contains the same file uploaded to the Coveralls and its HTML version.

Name fkYAML_coverage.pr458.zip
ID 2392986599
URL https://github.com/fktn-k/fkYAML/actions/runs/12641975785/artifacts/2392986599

@fktn-k fktn-k force-pushed the feature/400_mappings_in_range_based_for_loops branch from 881ed26 to 688433d Compare January 6, 2025 21:20
@fktn-k fktn-k force-pushed the feature/400_mappings_in_range_based_for_loops branch 2 times, most recently from 170fa5c to b8a61ac Compare January 6, 2025 23:16
@fktn-k fktn-k force-pushed the feature/400_mappings_in_range_based_for_loops branch from b8a61ac to 7ce4c5a Compare January 6, 2025 23:17
@fktn-k fktn-k changed the title Mappings in range based for loops Mapping key/value access in range based for loops Jan 6, 2025
@fktn-k fktn-k merged commit 36ca93e into develop Jan 7, 2025
163 checks passed
@fktn-k fktn-k deleted the feature/400_mappings_in_range_based_for_loops branch January 7, 2025 03:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature new feature possibly with public API changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant