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

adding backward compatibility for string-to-double conversion #1284

Merged
merged 10 commits into from
Jan 12, 2024

Conversation

bailaC
Copy link
Contributor

@bailaC bailaC commented Jan 9, 2024

Adding backward compatibility for string to double conversion as mentioned in
#1257 (comment)

Copy link

codecov bot commented Jan 9, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (acbeeea) 47.82% compared to head (f319415) 47.85%.
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1284      +/-   ##
==========================================
+ Coverage   47.82%   47.85%   +0.03%     
==========================================
  Files          41       41              
  Lines        3473     3477       +4     
  Branches     1888     1889       +1     
==========================================
+ Hits         1661     1664       +3     
  Misses        446      446              
- Partials     1366     1367       +1     
Flag Coverage Δ
unittests 47.85% <71.42%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
hardware_interface/src/lexical_casts.cpp 50.00% <71.42%> (+10.00%) ⬆️

@bailaC bailaC marked this pull request as ready for review January 10, 2024 04:43
hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
Copy link
Member

@aprotyas aprotyas left a comment

Choose a reason for hiding this comment

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

Left a couple of comments.


Also, I think it would be cleaner to leave stod() alone and have two versions of impl::stod() instead. That way we don't end up changing the API contract when touching individual implementations… what do you think of this?

namespace hardware_interface
{

namespace impl
{
std::optional<double> stod(const std::string & s)
{
#if __cplusplus < 201703L
    // Impl with std::istringstream
    ...
    if (stream.fail() || !stream.eof())
    {
        return std::nullopt;
    }
    return result;
#else
    // Impl with std::from_chars
    ...
    if (parse_result.ec == std::errc())
    {
        return result_value;
    }
    return std::nullopt;
#endif
}
}  // namespace impl

double stod(const std::string & s)
{
    if (const auto result = impl::stod(s))
    {
        return *result;
    }
    throw std::invalid_argument("Failed converting string to real number");
}
}

hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
Copy link
Member

@aprotyas aprotyas left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of comments

hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
hardware_interface/src/lexical_casts.cpp Outdated Show resolved Hide resolved
Copy link
Member

@destogl destogl left a comment

Choose a reason for hiding this comment

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

This looks good to me and it seems that the tests are happy too :)

Copy link
Contributor

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

nice, works for me (on debian11 and ubuntu 22.04)

Copy link
Member

@saikishor saikishor left a comment

Choose a reason for hiding this comment

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

LGTM

@destogl destogl merged commit 477c85d into ros-controls:master Jan 12, 2024
13 checks passed
@destogl destogl deleted the backward-from-chars branch January 12, 2024 17:09
@destogl
Copy link
Member

destogl commented Jan 12, 2024

@Mergifyio backport iron humble

Copy link
Contributor

mergify bot commented Jan 12, 2024

backport iron humble

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jan 12, 2024
---------

Co-authored-by: Sai Kishor Kothakota <[email protected]>
Co-authored-by: Dr. Denis <[email protected]>
(cherry picked from commit 477c85d)
mergify bot pushed a commit that referenced this pull request Jan 12, 2024
---------

Co-authored-by: Sai Kishor Kothakota <[email protected]>
Co-authored-by: Dr. Denis <[email protected]>
(cherry picked from commit 477c85d)
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.

5 participants