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

[RFC] improve readability of time literals #9299

Merged
merged 2 commits into from
Apr 14, 2018
Merged

Conversation

bkueng
Copy link
Member

@bkueng bkueng commented Apr 13, 2018

We have various expressions like:

if (hrt_elapsed_time(&start_time) > 8000000)

The number of 0's are hard to read, and we cannot use 8e6, since this is of type double.
C++11 introduced user-defined literals, which allows to write the following:

if (hrt_elapsed_time(&start_time) > 8_s)

As an example, I adjusted the land_detector to use the new literals. Commander would be a good candidate as well. We could also be more verbose if desired, for instance 8_sec or even 8_seconds.

I consider this as a start towards using more units throughout the code, for all the SI units we use, not only time. A potential library for this is https://github.com/nholthaus/units, as pointed out by @dagar . Some notes about it:

  • Uses double by default as underlying type, but it can be customized
  • requires the <chrono> header
  • not specifically targeted towards embedded, but it's efficient as conversions are done at compile-time
  • only very limited literal support, so it's kind of orthogonal to this PR / it will be simple to adjust later on.

Thoughts?

@MaEtUgR @RomanBapst @Stifael

bkueng added 2 commits April 13, 2018 10:21
The goal is to improve the readability for expressions like:
if (hrt_elapsed_time(&start_time) > 8000000)

Available since C++11
@bkueng bkueng changed the title [RCF] improve readability of time literals [RFC] improve readability of time literals Apr 13, 2018
@Stifael
Copy link
Contributor

Stifael commented Apr 13, 2018

good idea.
the potential library is built on c++14 though.
What speaks against having our own operators?

@bkueng
Copy link
Member Author

bkueng commented Apr 13, 2018

What speaks against having our own operators?

IMO nothing, but once we grow & extend it and add for example a meter class, it will be less effort to just use an existing and already tested library.

@LorenzMeier LorenzMeier merged commit 123f11f into master Apr 14, 2018
@LorenzMeier LorenzMeier deleted the time_literals branch April 14, 2018 11:46
@MaEtUgR
Copy link
Member

MaEtUgR commented Apr 26, 2018

@bkueng You finally found a nice solution for the annoying trailing zeroes with all these microsecond values. Thanks a lot! I totally support the use of such useful language features.

MaEtUgR added a commit that referenced this pull request Nov 28, 2018
which was introduced in #9299
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