Basis Unit Store

_images/ditaa-ca4ba4508bfa9d42a5cd23768fdb6f29d7956a5e.png
#include <xo/unit/bu_store.hpp>

namespace bu = xo::qty::detail::bu;

A xo::qty::bu_store is a small, constexpr, key-value store associating abbreviations with basis units. To satisfy the constexpr requirement, all unit abbreviations are irrevocably established from bu_store’s constructor.

The constant bu_abbrev_store contains a single instance of bu_store, recording all built-in units along with their associated abbreviations

map mass_table<<bu_dim_store>> {
bu::milligram => "mg"
bu::gram => "g"
bu::kilogram => "kg"
}

map distance_table<<bu_dim_store>> {
bu::millimeter => "mm"
bu::meter => "m"
bu::kilometer => "km"
}

map time_table<<bu_dim_store>> {
bu::millisecond => "ms"
bu::second => "s"
bu::minute => "min"
bu::hour => "hr"
}

object bu_abbrev_store<<bu_store>>
bu_abbrev_store : bu_abbrev_vv[dim::mass] = mass_table
bu_abbrev_store : bu_abbrev_vv[dim::distance] = distance_table
bu_abbrev_store : bu_abbrev_vv[dim::time] = time_table

bu_abbrev_store o-- mass_table
bu_abbrev_store o-- distance_table
bu_abbrev_store o-- time_table

basis-unit store

This class exists to support the implementation of natural_unit::abbrev().

Application code is not expected to interact directly with it.

Class

class bu_store

associate basis units with abbreviations

For example, this would be possible:

#include <xo/unit/bu_store.hpp>

namespace bu = using xo::qty::detail::bu;
using xo::qty::detail::bu_store;
using xo::qty::dim;
using xo::flatstring;

constexpr bu_store store;
static_assert(store.bu_abbrev(bu::minute) == flatstring("min"));
static_assert(store.bu_abbrev(bu::microgram) == flatstring("ug"));
group bu-store constructors

Functions

inline constexpr bu_store()

construct canonical instance containing all known basis units

group Bu-store-access-methods

Functions

inline bu_abbrev_type bu_abbrev(const basis_unit &bu) const

get basis-unit abbreviation at runtime

group Bu-store-implementation-methods

Functions

static inline bu_abbrev_type bu_fallback_abbrev(dim basis_dim, const scalefactor_ratio_type &scalefactor)

report fallback abbreviation for a basis unit.

Typically unused. Will be invoked only if a basis unit exists for which bu_store::bu_establish_abbrev was not called by bu_store’s constructor.

Tries to produce something unambiguous, while still supplying enough information to indicate what’s needed to resolve the problem.

Example

bu_store.bu_fallback_abbrev(dim::mass, scalefactor_ratio_type(1234,1))
  => "1234g"

inline void bu_establish_abbrev(const basis_unit &bu, const bu_abbrev_type &abbrev)

associate abbreviation abbrev with basis unit bu

Example:

// femtograms
bu_store.bu_establish_abbrev(detail::bu::mass_unit(1, 1000000000000000), "fg")

class bu_dim_store

store basis-unit abbreviations for a particular dimension

group bu-dim-store type traits

Typedefs

using entry_type = std::pair<scalefactor2x_ratio_type, bu_abbrev_type>
using native_scale_v = std::array<entry_type, max_bu_per_dim>
group bu-dim-store instance vars

Variables

std::size_t n_bu_ = 0
std::array<entry_type, max_bu_per_dim> bu_abbrev_v_

Constants

Provides dictionary of unit abbreviations

Application code is not expected to interact directly with bu_abbrev_store.

static detail::bu_store xo::qty::bu_abbrev_store = detail::bu_store()

global abbreviation store.

Note

Extending the contents of this store at runtime is not supported, in favor of preserving constexpr abbreviations.

Functions

bu_abbrev_type xo::qty::bu_abbrev(const basis_unit &bu)

get abbreviation for basis-unit bu

For example:

#include <xo/unit/bu_store.hpp>

namespace bu = xo::qty::bu;
using xo::qty::bu_abbrev;
using xo::flatstring;

static_assert(bu_abbrev(bu::kilogram) == xo::flatstring("kg"));