aurora-opensource / au

A C++14-compatible physical units library with no dependencies and a single-file delivery option. Emphasis on safety, accessibility, performance, and developer experience.
Apache License 2.0
329 stars 21 forks source link

Add labels for integer and rational magnitudes #331

Closed chiphogg closed 1 day ago

chiphogg commented 3 days ago

Think of this as "just enough of #85 to unblock #105". The fact that our common unit labels don't tell you the size of the unit (#105) has really, really been bugging me for a long time. Recently, I realized we don't need to do all of #85 to get it! Instead, all we need to do is:

  1. Build a mechanism that we can easily extend.

  2. Cover the most important use cases.

This PR creates the MagnitudeLabel trait mechanism (also accessible via a function/value interface as mag_label). We enumerate the various categories of magnitudes that we can label, defaulting to "unsupported". The first two supported categories are integers (that fit in std::uintmax_t), and rationals.

We also add a trait, has_exposed_slash, looking forward to the obvious use case of auto-generating labels for scaled units. Those labels will have the form "[M U]" for a unit of label "U" scaled by a magnitude of label "M". If has_exposed_slash is true for a given magnitude label, then we'll know to make this "[(M) U]" instead.

Finally, we move a couple of StringConstant-ish utilities into "string_constant.hh", so that we can use them in our implementation.

Helps #85.