Skip to content

Numeric scalar conversions inside basic_node::get_value API #419

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

Merged
merged 3 commits into from
Oct 27, 2024

Conversation

fktn-k
Copy link
Owner

@fktn-k fktn-k commented Oct 27, 2024

This PR adds numeric scalar conversions, such as float to int, inside the basic_node::get_value function.

fkyaml::node n = 1.23; // type == node_type::FLOAT
int i = n.get_value<int>(); // i == 1 (`double` is internally converted to `int`)
bool b = n.get_value<bool>(); // b == true (`double` is internally converted to `bool`)

This change was suggested here by @ARessegetesStery to provide easier and more natural access methods to YAML node values in a similar way in which C++ handles primitives.

The conversions follows these rules:

  • If the YAML node value is a null (node_type::NULL_OBJECT), the value can be converted to:
    • false (boolean)
    • 0 (integer)
    • 0.0 (floating point)
  • If the YAML node value is a boolean (node_type::BOOLEAN), the value can be converted to:
    • 1 /*true*/ or 0 /*false*/ (integer)
    • 1.0 /*true*/ or 0.0 /*false*/ (floating point)
  • If the YAML node value is a integer (node_type::INTEGER), the value can be converted to:
    • true /*non-0*/ or false /*0*/ (boolean)
    • static_casted floating point value (floating point)
  • If the YAML node value is a floating point (node_type::FLOAT), the value can be converted to:
    • true /*non-0*/ or false /*0*/ (boolean)
    • static_casted integer value (integer)

If all the above are not applied, the basic_node::get_value function throws a type_error exception, which is unchanged.


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 Oct 27, 2024
@fktn-k fktn-k added this to the Release v0.3.14 milestone Oct 27, 2024
@fktn-k fktn-k self-assigned this Oct 27, 2024
Copy link

github-actions bot commented Oct 27, 2024

:octocat: Upload Coverage Event Notification

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

Name fkYAML_coverage.pr419.zip
ID 2109644331
URL https://github.com/fktn-k/fkYAML/actions/runs/11543477348/artifacts/2109644331

@fktn-k fktn-k merged commit 4b2490c into develop Oct 27, 2024
171 checks passed
@fktn-k fktn-k deleted the internal_numeric_scalar_conversions_in_from_node branch October 27, 2024 20:29
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