LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util::quantities Namespace Reference

Types of variables with a unit. More...

Namespaces

 concepts
 Infrastructure for the quantities library.
 
 datasize_literals
 Literal constants for data size quantities.
 
 details
 
 electromagnetism_literals
 Literal constants for quantities.
 
 electronics_literals
 Literal constants for electronics quantities.
 
 energy_literals
 Literal constants for energy quantities.
 
 frequency_literals
 Literal constants for frequency quantities.
 
 intervals
 
 points
 
 prefixes
 Definitions of additional prefixes.
 
 space_literals
 Literal constants for space quantities.
 
 time_literals
 Literal constants for time quantities.
 
 units
 Definitions of actual units.
 

Classes

struct  ExtraCharactersError
 String representing a quantity has spurious characters after the number. More...
 
struct  InvalidUnitPrefix
 String representing a quantity has unsupported unit prefix. More...
 
struct  MismatchingUnit
 String representing a quantity has incompatible unit. More...
 
struct  MissingUnit
 String representing a quantity has no unit. More...
 
struct  NoCategory
 
struct  ValueError
 String representing a quantity has an invalid number. More...
 

Typedefs

Data size

These time quantities are tied to util::quantities::units::Byte.

  • most general template, scaled_byte, allowing to choose both the scale of the unit (e.g. util::quantities::prefixes::mebi for mebibyte) and the type of the numerical representation
  • generic templates (e.g. byte_as), allowing to choose which numerical representation to use
  • unsigned integral number (e.g. byte), 64-bit on amd64 architecture, ready for use
template<typename R , typename T = unsigned long long int>
using scaled_byte = concepts::scaled_quantity< units::Byte, R, T >
 The most generic units::Byte-based quantity. More...
 
template<typename T = unsigned long long int>
using byte_as = scaled_byte< std::ratio< 1 >, T >
 Type of data size stored in bytes. More...
 
template<typename T = unsigned long long int>
using bytes_as = byte_as< T >
 Alias for common language habits. More...
 
using byte = byte_as<>
 Type of data size stored in bytes, in long long precision. More...
 
using bytes = byte
 Alias for common language habits. More...
 
template<typename T = unsigned long long int>
using kibibyte_as = concepts::rescale< byte_as< T >, prefixes::kibi >
 Type of data size stored in kibibytes. More...
 
template<typename T = unsigned long long int>
using kibibytes_as = kibibyte_as< T >
 Alias for common language habits. More...
 
using kibibyte = kibibyte_as<>
 Type of data size stored in kibibytes, in long long precision. More...
 
using kibibytes = kibibyte
 Alias for common language habits. More...
 
template<typename T = unsigned long long int>
using mebibyte_as = concepts::rescale< byte_as< T >, prefixes::mebi >
 Type of data size stored in mebibytes. More...
 
template<typename T = unsigned long long int>
using mebibytes_as = mebibyte_as< T >
 Alias for common language habits. More...
 
using mebibyte = mebibyte_as<>
 Type of data size stored in mebibytes, in long long precision. More...
 
using mebibytes = mebibyte
 Alias for common language habits. More...
 
template<typename T = unsigned long long int>
using gibibyte_as = concepts::rescale< byte_as< T >, prefixes::gibi >
 Type of data size stored in gibibytes. More...
 
template<typename T = unsigned long long int>
using gibibytes_as = gibibyte_as< T >
 Alias for common language habits. More...
 
using gibibyte = gibibyte_as<>
 Type of data size stored in pebibytes, in long long precision. More...
 
using gibibytes = gibibyte
 Alias for common language habits. More...
 
template<typename T = unsigned long long int>
using tebibyte_as = concepts::rescale< byte_as< T >, prefixes::tebi >
 Type of data size stored in tebibytes. More...
 
template<typename T = unsigned long long int>
using tebibytes_as = tebibyte_as< T >
 Alias for common language habits. More...
 
using tebibyte = tebibyte_as<>
 Type of data size stored in tebibytes, in long long precision. More...
 
using tebibytes = tebibyte
 Alias for common language habits. More...
 
template<typename T = unsigned long long int>
using pebibyte_as = concepts::rescale< byte_as< T >, prefixes::pebi >
 Type of data size stored in pebibytes. More...
 
template<typename T = unsigned long long int>
using pebibytes_as = pebibyte_as< T >
 Alias for common language habits. More...
 
using pebibyte = pebibyte_as<>
 Type of data size stored in pebibytes, in long long precision. More...
 
using pebibytes = pebibyte
 Alias for common language habits. More...
 
template<typename T = unsigned long long int>
using exbibyte_as = concepts::rescale< byte_as< T >, prefixes::exbi >
 Type of data size stored in exbibytes. More...
 
template<typename T = unsigned long long int>
using exbibytes_as = exbibyte_as< T >
 Alias for common language habits. More...
 
using exbibyte = exbibyte_as<>
 Type of data size stored in exbibytes, in long long precision. More...
 
using exbibytes = exbibyte
 Alias for common language habits. More...
 
Charge quantities

These charge quantities are tied to util::quantities::units::Coulomb. A few options are provided:

  • most general template, scaled_coulomb, allowing to choose both the scale of the unit (e.g. std::pico for picocoulomb) and the type of the numerical representation
  • generic templates (e.g. coulomb_as), allowing to choose which numerical representation to use
  • double precision (e.g. coulomb), ready for use
template<typename R , typename T = double>
using scaled_coulomb = concepts::scaled_quantity< units::Coulomb, R, T >
 The most generic units::Coulomb-based quantity. More...
 
template<typename T = double>
using coulomb_as = scaled_coulomb< std::ratio< 1 >, T >
 Type of charge stored in coulomb. More...
 
using coulomb = coulomb_as<>
 Type of charge stored in coulombs, in double precision. More...
 
template<typename T = double>
using millicoulomb_as = concepts::rescale< coulomb_as< T >, std::milli >
 Type of charge stored in millicoulomb. More...
 
using millicoulomb = millicoulomb_as<>
 Type of charge stored in millicoulomb, in double precision. More...
 
template<typename T = double>
using microcoulomb_as = concepts::rescale< coulomb_as< T >, std::micro >
 Type of charge stored in microcoulomb. More...
 
using microcoulomb = microcoulomb_as<>
 Type of charge stored in microcoulomb, in double precision. More...
 
template<typename T = double>
using nanocoulomb_as = concepts::rescale< coulomb_as< T >, std::nano >
 Type of charge stored in nanocoulomb. More...
 
using nanocoulomb = nanocoulomb_as<>
 Type of charge stored in nanocoulomb, in double precision. More...
 
template<typename T = double>
using picocoulomb_as = concepts::rescale< coulomb_as< T >, std::pico >
 Type of charge stored in picocoulomb. More...
 
using picocoulomb = picocoulomb_as<>
 Type of charge stored in picocoulomb, in double precision. More...
 
template<typename T = double>
using femtocoulomb_as = concepts::rescale< coulomb_as< T >, std::femto >
 Type of charge stored in femtocoulomb. More...
 
using femtocoulomb = femtocoulomb_as<>
 Type of charge stored in femtocoulomb, in double precision. More...
 
Electric potential quantities

These potential quantities are tied to util::quantities::units::Volt. A few options are provided:

  • most general template, scaled_volt, allowing to choose both the scale of the unit (e.g. std::kilo for kilovolt) and the type of the numerical representation
  • generic templates (e.g. volt_as), allowing to choose which numerical representation to use
  • double precision (e.g. volt), ready for use
template<typename R , typename T = double>
using scaled_volt = concepts::scaled_quantity< units::Volt, R, T >
 The most generic units::Volt-based quantity. More...
 
template<typename T = double>
using volt_as = scaled_volt< std::ratio< 1 >, T >
 Type of potential stored in volt. More...
 
using volt = volt_as<>
 Type of potential stored in volts, in double precision. More...
 
template<typename T = double>
using millivolt_as = concepts::rescale< volt_as< T >, std::milli >
 Type of potential stored in millivolt. More...
 
using millivolt = millivolt_as<>
 Type of potential stored in millivolt, in double precision. More...
 
template<typename T = double>
using microvolt_as = concepts::rescale< volt_as< T >, std::micro >
 Type of potential stored in microvolt. More...
 
using microvolt = microvolt_as<>
 Type of potential stored in microvolt, in double precision. More...
 
template<typename T = double>
using kilovolt_as = concepts::rescale< volt_as< T >, std::kilo >
 Type of potential stored in kilovolt. More...
 
using kilovolt = kilovolt_as<>
 Type of potential stored in kilovolt, in double precision. More...
 
template<typename T = double>
using megavolt_as = concepts::rescale< volt_as< T >, std::mega >
 Type of potential stored in megavolt. More...
 
using megavolt = megavolt_as<>
 Type of potential stored in megavolt, in double precision. More...
 
template<typename T = double>
using gigavolt_as = concepts::rescale< volt_as< T >, std::giga >
 Type of potential stored in gigavolt. More...
 
using gigavolt = gigavolt_as<>
 Type of potential stored in gigavolt, in double precision. More...
 
Ticks

These tick quantities are tied to util::quantities::units::Tick. A few options are provided:

  • generic template (tick_as), allowing to choose which numerical representation to use
  • unsigned integer (tick), based on std::ptrdiff_t, ready for use

For this unit in particular, additional options are provided to accommodate the custom of using the unit in plural form: ticks_as and ticks are exactly equivalent to the singular-named counterparts.

template<typename T = std::ptrdiff_t>
using tick_as = concepts::scaled_quantity< units::Tick, std::ratio< 1 >, T >
 Tick number, represented by the specified type T. More...
 
template<typename T = tick_as<>::value_t>
using ticks_as = tick_as< T >
 Alias for common language habits. More...
 
using tick = tick_as<>
 Tick number, represented by std::ptrdiff_t. More...
 
using ticks = tick
 Alias for common language habits. More...
 
using tick_f = tick_as< float >
 Tick number, represented by float. More...
 
using ticks_f = tick_f
 Alias for common language habits. More...
 
using tick_d = tick_as< double >
 Tick number, represented by double. More...
 
using ticks_d = tick_d
 Alias for common language habits. More...
 
ADC counts

These ADC count quantities are tied to util::quantities::units::Counts. A few options are provided:

  • generic template (counts_as), allowing to choose which numerical representation to use
  • unsigned integer (counts), based on signed short int, ready for use
template<typename T = signed short int>
using counts_as = concepts::scaled_quantity< units::Counts, std::ratio< 1 >, T >
 Number of ADC counts, represented by the specified type T. More...
 
using counts = counts_as<>
 Number of ADC counts, represented by signed short int. More...
 
using counts_f = counts_as< float >
 Number of ADC counts, represented by float. More...
 
Particle energy quantities

These energy quantities are tied to util::quantities::units::ElectronVolt. A few options are provided:

  • most general template, scaled_electronvolt, allowing to choose both the scale of the unit (e.g. std::exa for exaelectronvolt) and the type of the numerical representation
  • generic templates (e.g. electronvolt_as), allowing to choose which numerical representation to use
  • double precision (e.g. electronvolt), ready for use
template<typename R , typename T = double>
using scaled_electronvolt = concepts::scaled_quantity< units::ElectronVolt, R, T >
 The most generic units::ElectronVolt-based quantity. More...
 
template<typename T = double>
using electronvolt_as = scaled_electronvolt< std::ratio< 1 >, T >
 Type of energy stored in electronvolt. More...
 
using electronvolt = electronvolt_as<>
 Type of energy stored in electronvolts, in double precision. More...
 
template<typename T = double>
using microelectronvolt_as = concepts::rescale< electronvolt_as< T >, std::micro >
 Type of energy stored in microelectronvolt. More...
 
using microelectronvolt = microelectronvolt_as<>
 Type of energy stored in microelectronvolt, in double precision. More...
 
template<typename T = double>
using millielectronvolt_as = concepts::rescale< electronvolt_as< T >, std::milli >
 Type of energy stored in millielectronvolt. More...
 
using millielectronvolt = millielectronvolt_as<>
 Type of energy stored in millielectronvolt, in double precision. More...
 
template<typename T = double>
using kiloelectronvolt_as = concepts::rescale< electronvolt_as< T >, std::kilo >
 Type of energy stored in kiloelectronvolt. More...
 
using kiloelectronvolt = kiloelectronvolt_as<>
 Type of energy stored in kiloelectronvolt, in double precision. More...
 
template<typename T = double>
using megaelectronvolt_as = concepts::rescale< electronvolt_as< T >, std::mega >
 Type of energy stored in megaelectronvolt. More...
 
using megaelectronvolt = megaelectronvolt_as<>
 Type of energy stored in megaelectronvolt, in double precision. More...
 
template<typename T = double>
using gigaelectronvolt_as = concepts::rescale< electronvolt_as< T >, std::giga >
 Type of energy stored in gigaelectronvolt. More...
 
using gigaelectronvolt = gigaelectronvolt_as<>
 Type of energy stored in gigaelectronvolt, in double precision. More...
 
template<typename T = double>
using teraelectronvolt_as = concepts::rescale< electronvolt_as< T >, std::tera >
 Type of energy stored in teraelectronvolt. More...
 
using teraelectronvolt = teraelectronvolt_as<>
 Type of energy stored in teraelectronvolt, in double precision. More...
 
Frequency quantities

These frequency quantities are tied to util::quantities::units::Hertz. A few options are provided:

  • most general template, scaled_hertz, allowing to choose both the scale of the unit (e.g. std::kilo for kilohertz) and the type of the numerical representation
  • generic template (e.g. hertz_as), allowing to choose which numerical representation to use
  • double precision (e.g. hertz), ready for use
template<typename R , typename T = double>
using scaled_hertz = concepts::scaled_quantity< units::Hertz, R, T >
 The most generic units::Hertz-based quantity. More...
 
template<typename T = double>
using hertz_as = scaled_hertz< std::ratio< 1 >, T >
 Type of frequency stored in hertz. More...
 
using hertz = hertz_as<>
 Type of frequency stored in hertz, in double precision. More...
 
template<typename T = double>
using kilohertz_as = concepts::rescale< hertz_as< T >, std::kilo >
 Type of frequency stored in kilohertz. More...
 
using kilohertz = kilohertz_as<>
 Type of frequency stored in kilohertz, in double precision. More...
 
template<typename T = double>
using megahertz_as = concepts::rescale< hertz_as< T >, std::mega >
 Type of frequency stored in megahertz. More...
 
using megahertz = megahertz_as<>
 Type of frequency stored in megahertz, in double precision. More...
 
template<typename T = double>
using gigahertz_as = concepts::rescale< hertz_as< T >, std::giga >
 Type of frequency stored in gigahertz. More...
 
using gigahertz = gigahertz_as<>
 Type of frequency stored in gigahertz, in double precision. More...
 
Time quantities

These time quantities are tied to util::quantities::units::Second. A few options are provided:

  • most general template, scaled_second, allowing to choose both the scale of the unit (e.g. std::milli for milliseconds) and the type of the numerical representation
  • generic templates (e.g. second_as), allowing to choose which numerical representation to use
  • double precision (e.g. second), ready for use

For this unit in particular, additional options are provided to accommodate the custom of using the unit in plural form: seconds_as and seconds are exactly equivalent to the singular-named counterparts.

template<typename R , typename T = double>
using scaled_second = concepts::scaled_quantity< units::Second, R, T >
 The most generic units::Second-based quantity. More...
 
template<typename T = double>
using second_as = scaled_second< std::ratio< 1 >, T >
 Type of time stored in seconds. More...
 
template<typename T = double>
using seconds_as = second_as< T >
 Alias for common language habits. More...
 
using second = second_as<>
 Type of time stored in seconds, in double precision. More...
 
using seconds = second
 Alias for common language habits. More...
 
template<typename T = double>
using millisecond_as = concepts::rescale< second_as< T >, std::milli >
 Type of time stored in milliseconds. More...
 
template<typename T = double>
using milliseconds_as = millisecond_as< T >
 Alias for common language habits. More...
 
using millisecond = millisecond_as<>
 Type of time stored in milliseconds, in double precision. More...
 
using milliseconds = millisecond
 Alias for common language habits. More...
 
template<typename T = double>
using microsecond_as = concepts::rescale< second_as< T >, std::micro >
 Type of time stored in microseconds. More...
 
template<typename T = double>
using microseconds_as = microsecond_as< T >
 Alias for common language habits. More...
 
using microsecond = microsecond_as<>
 Type of time stored in microseconds, in double precision. More...
 
using microseconds = microsecond
 Alias for common language habits. More...
 
template<typename T = double>
using nanosecond_as = concepts::rescale< second_as< T >, std::nano >
 Type of time stored in nanoseconds. More...
 
template<typename T = double>
using nanoseconds_as = nanosecond_as< T >
 Alias for common language habits. More...
 
using nanosecond = nanosecond_as<>
 Type of time stored in nanoseconds, in double precision. More...
 
using nanoseconds = nanosecond
 Alias for common language habits. More...
 
template<typename T = double>
using picosecond_as = concepts::rescale< second_as< T >, std::pico >
 Type of time stored in picoseconds. More...
 
template<typename T = double>
using picoseconds_as = picosecond_as< T >
 Alias for common language habits. More...
 
using picosecond = picosecond_as<>
 Type of time stored in picoseconds, in double precision. More...
 
using picoseconds = picosecond
 Alias for common language habits. More...
 
Linear space quantities

These space quantities are tied to util::quantities::units::Meter. A few options are provided:

  • most general template, scaled_meter, allowing to choose both the scale of the unit (e.g. std::milli for millimeters) and the type of the numerical representation
  • generic templates (e.g. meter_as), allowing to choose which numerical representation to use
  • double precision (e.g. meter), ready for use

For this unit in particular, additional options are provided to accommodate the custom of using the unit in plural form: meters_as and meters are exactly equivalent to the singular-named counterparts.

template<typename R , typename T = double>
using scaled_meter = concepts::scaled_quantity< units::Meter, R, T >
 The most generic units::Meter-based quantity. More...
 
template<typename T = double>
using meter_as = scaled_meter< std::ratio< 1 >, T >
 Type of space stored in meters. More...
 
template<typename T = double>
using meters_as = meter_as< T >
 Alias for common language habits. More...
 
using meter = meter_as<>
 Type of space stored in meters, in double precision. More...
 
using meters = meter
 Alias for common language habits. More...
 
template<typename T = double>
using kilometer_as = concepts::rescale< meter_as< T >, std::kilo >
 Type of space stored in kilometers. More...
 
template<typename T = double>
using kilometers_as = kilometer_as< T >
 Alias for common language habits. More...
 
using kilometer = kilometer_as<>
 Type of space stored in kilometers, in double precision. More...
 
using kilometers = kilometer
 Alias for common language habits. More...
 
template<typename T = double>
using centimeter_as = concepts::rescale< meter_as< T >, std::centi >
 Type of space stored in centimeters. More...
 
template<typename T = double>
using centimeters_as = centimeter_as< T >
 Alias for common language habits. More...
 
using centimeter = centimeter_as<>
 Type of space stored in centimeters, in double precision. More...
 
using centimeters = centimeter
 Alias for common language habits. More...
 
template<typename T = double>
using millimeter_as = concepts::rescale< meter_as< T >, std::milli >
 Type of space stored in millimeters. More...
 
template<typename T = double>
using millimeters_as = millimeter_as< T >
 Alias for common language habits. More...
 
using millimeter = millimeter_as<>
 Type of space stored in millimeters, in double precision. More...
 
using millimeters = millimeter
 Alias for common language habits. More...
 
template<typename T = double>
using micrometer_as = concepts::rescale< meter_as< T >, std::micro >
 Type of space stored in micrometers. More...
 
template<typename T = double>
using micrometers_as = micrometer_as< T >
 Alias for common language habits. More...
 
using micrometer = micrometer_as<>
 Type of space stored in micrometers, in double precision. More...
 
using micrometers = micrometer
 Alias for common language habits. More...
 
template<typename T = double>
using nanometer_as = concepts::rescale< meter_as< T >, std::nano >
 Type of space stored in nanometers. More...
 
template<typename T = double>
using nanometers_as = nanometer_as< T >
 Alias for common language habits. More...
 
using nanometer = nanometer_as<>
 Type of space stored in nanometers, in double precision. More...
 
using nanometers = nanometer
 Alias for common language habits. More...
 
template<typename T = double>
using picometer_as = concepts::rescale< meter_as< T >, std::pico >
 Type of space stored in picometers. More...
 
template<typename T = double>
using picometers_as = picometer_as< T >
 Alias for common language habits. More...
 
using picometer = picometer_as<>
 Type of space stored in picometers, in double precision. More...
 
using picometers = picometer
 Alias for common language habits. More...
 
template<typename T = double>
using femtometer_as = concepts::rescale< meter_as< T >, std::femto >
 Type of space stored in femtometers. More...
 
template<typename T = double>
using femtometers_as = femtometer_as< T >
 Alias for common language habits. More...
 
using femtometer = femtometer_as<>
 Type of space stored in femtometers, in double precision. More...
 
using femtometers = femtometer
 Alias for common language habits. More...
 

Functions

template<typename IV >
IV makeInterval (std::string_view s, bool unitOptional=false)
 Returns an interval of the specified type parsed from a string. More...
 
template<typename IV >
IV makeInterval (std::string const &s, bool unitOptional=false)
 Returns an interval of the specified type parsed from a string. More...
 
template<typename IV >
IV makeInterval (char const *s, bool unitOptional=false)
 Returns an interval of the specified type parsed from a string. More...
 
template<typename PT >
PT makePoint (std::string_view s, bool unitOptional=false)
 Returns a point of the specified type parsed from a string. More...
 
template<typename PT >
PT makePoint (std::string const &s, bool unitOptional=false)
 Returns a point of the specified type parsed from a string. More...
 
template<typename PT >
PT makePoint (char const *s, bool unitOptional=false)
 Returns a point of the specified type parsed from a string. More...
 
template<typename Quantity >
Quantity makeQuantity (std::string_view s, bool unitOptional=false)
 Returns a quantity of the specified type parsed from a string. More...
 
template<typename Quantity >
Quantity makeQuantity (std::string const &s, bool unitOptional=false)
 Returns a quantity of the specified type parsed from a string. More...
 
template<typename Quantity >
Quantity makeQuantity (char const *s, bool unitOptional=false)
 Returns a quantity of the specified type parsed from a string. More...
 

Detailed Description

Types of variables with a unit.

This library uses the following concepts, vaguely inspired by Boost Units library:

  • dimension is a category of measurements, e.g. time; there is no representation of this concept in this library
  • unit is a scale to represent a dimension, e.g. seconds; there is no
  • representation for the generic unit concept in this library, but each
  • unit needs its own unique class representing it; an example of this
  • simple class is util::quantities::UnitBase
  • scaled unit is a unit with a factor modifying its scale; e.g., microsecond; this is represented in this library as ScaledUnit
  • quantity is a value interpreted via a specific scaled unit, e.g. 12.3 microseconds; this is represented here by the Quantity class.

Differently from Boost Units library, this one does not provide dimensional analysis (hence the lack of need for the "dimension" concept.

Usage examples

The following examples use units of time, whose definitions are actually provided elsewhere in this library (see lardataalg/Utilities/quantites/spacetime.h and the unit util::quantities::units::Second).

void drifting(double driftSpace, double driftVelocity) {
using namespace util::quantities::time_literals; // [1]
{ driftSpace / driftVelocity };
std::cout << "Drift time: " << driftTime << std::endl; // writes: "xxx us"
// double the velocity...
driftTime /= 2.0;
std::cout << "halved!! => " << driftTime << std::endl;
// add 350 ns of offset
driftTime += 0.350; // ERROR!! what is 0.350?
driftTime += 350_ns; // needs [1]; convert to microseconds and adds
std::cout << "delayed! => " << driftTime << std::endl;
// ...
auto dual = driftTime * 2.0; // dual is still `microsecond`
double half = driftTime / 2.0; // the result is converted to `double`
// and looses its unit forever
double lateTime = 500;
driftTime = lateTime; // ERROR! which unit is `lateTime` in?
driftTime = microsecond(lateTime); // assign from `lateTime` us
} // drifting()

Organization of the code

The code of this library is entirely under this namespace (util::quantities). It is organised in a few subspaces:

  • concepts contains all the infrastructure for the library, including the definition of classes representing a unit, a quantity, and more
    • details: implementation details (users should not bother with them)
  • units contains the definitions of actual units (e.g. seconds, ampere...)

The file lardataalg/Utilities/quantities.h does not contain the definition of any actual quantity nor unit. Quantity libraries are expected to include this header, extend the namespace units to include the units they need, and finally adding the quantites needed directly in this very namespace.

An example of adding the definition of "ampere":

namespace util::quantities {
namespace units {
struct ampere {
static constexpr auto symbol = "A"sv;
static constexpr auto name = "ampere"sv;
}; // ampere
} // namespace units
template <typename T = double>
using ampere_as = concepts::Quantity<concepts::ScaledUnit<units::ampere>, T>;
using ampere = ampere_as<>;
template <typename T = double>
using milliampere_as = concepts::rescale<ampere_as<T>, std::milli>;
using milliampere = milliampere_as<>;
} // namespace util::quantities
Note
The suffix sv converts the literal string into a std::string_view, which is one of the few string types which can be defined at compile time (constexpr) and have a decently complete interface.

Another example may be data size:

namespace util::quantities {
namespace units {
struct byte {
static constexpr auto symbol = "B"sv;
static constexpr auto name = "byte"sv;
}; // byte
} // namespace units
namespace binary {
// Prefix for binary 2^20 (1048576).
using mebi = std::ratio<(1U << 20), 1U>;
} // namespace binary
template <typename T = unsigned long long>
using byte_as = concepts::Quantity<concepts::ScaledUnit<units::byte>, T>;
using byte = byte_as<>;
template <typename T = unsigned long long>
using mebibyte_as = concepts::rescale<byte_as<T>, binary::mebi>;
using mebibyte = mebibyte_as<>;
} // namespace util::quantities

This example mostly works, with the following exceptions:

  • the prefix is not known to the infrastructure and it will not be printed (it will show as question marks)
  • especially when using an integral base type, a lot of care must be put in the rounding: for example, 2000000 bytes will be converted into 1 mebibyte, and if converted back it will show 1048576 bytes.

Typedef Documentation

using util::quantities::byte = typedef byte_as<>

Type of data size stored in bytes, in long long precision.

Definition at line 94 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::byte_as = typedef scaled_byte<std::ratio<1>, T>

Type of data size stored in bytes.

Definition at line 87 of file datasize.h.

using util::quantities::bytes = typedef byte

Alias for common language habits.

Definition at line 97 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::bytes_as = typedef byte_as<T>

Alias for common language habits.

Definition at line 91 of file datasize.h.

Type of space stored in centimeters, in double precision.

Definition at line 441 of file spacetime.h.

template<typename T = double>
using util::quantities::centimeter_as = typedef concepts::rescale<meter_as<T>, std::centi>

Type of space stored in centimeters.

Definition at line 434 of file spacetime.h.

Alias for common language habits.

Definition at line 444 of file spacetime.h.

template<typename T = double>
using util::quantities::centimeters_as = typedef centimeter_as<T>

Alias for common language habits.

Definition at line 438 of file spacetime.h.

Type of charge stored in coulombs, in double precision.

Definition at line 77 of file electromagnetism.h.

template<typename T = double>
using util::quantities::coulomb_as = typedef scaled_coulomb<std::ratio<1>, T>

Type of charge stored in coulomb.

Definition at line 74 of file electromagnetism.h.

Number of ADC counts, represented by signed short int.

Definition at line 112 of file electronics.h.

template<typename T = signed short int>
using util::quantities::counts_as = typedef concepts::scaled_quantity<units::Counts, std::ratio<1>, T>

Number of ADC counts, represented by the specified type T.

Definition at line 109 of file electronics.h.

using util::quantities::counts_f = typedef counts_as<float>

Number of ADC counts, represented by float.

Definition at line 115 of file electronics.h.

Type of energy stored in electronvolts, in double precision.

Definition at line 72 of file energy.h.

template<typename T = double>
using util::quantities::electronvolt_as = typedef scaled_electronvolt<std::ratio<1>, T>

Type of energy stored in electronvolt.

Definition at line 69 of file energy.h.

Type of data size stored in exbibytes, in long long precision.

Definition at line 196 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::exbibyte_as = typedef concepts::rescale<byte_as<T>, prefixes::exbi>

Type of data size stored in exbibytes.

Definition at line 189 of file datasize.h.

Alias for common language habits.

Definition at line 199 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::exbibytes_as = typedef exbibyte_as<T>

Alias for common language habits.

Definition at line 193 of file datasize.h.

Type of charge stored in femtocoulomb, in double precision.

Definition at line 127 of file electromagnetism.h.

template<typename T = double>
using util::quantities::femtocoulomb_as = typedef concepts::rescale<coulomb_as<T>, std::femto>

Type of charge stored in femtocoulomb.

Definition at line 124 of file electromagnetism.h.

Type of space stored in femtometers, in double precision.

Definition at line 526 of file spacetime.h.

template<typename T = double>
using util::quantities::femtometer_as = typedef concepts::rescale<meter_as<T>, std::femto>

Type of space stored in femtometers.

Definition at line 519 of file spacetime.h.

Alias for common language habits.

Definition at line 529 of file spacetime.h.

template<typename T = double>
using util::quantities::femtometers_as = typedef femtometer_as<T>

Alias for common language habits.

Definition at line 523 of file spacetime.h.

Type of data size stored in pebibytes, in long long precision.

Definition at line 145 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::gibibyte_as = typedef concepts::rescale<byte_as<T>, prefixes::gibi>

Type of data size stored in gibibytes.

Definition at line 138 of file datasize.h.

Alias for common language habits.

Definition at line 148 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::gibibytes_as = typedef gibibyte_as<T>

Alias for common language habits.

Definition at line 142 of file datasize.h.

Type of energy stored in gigaelectronvolt, in double precision.

Definition at line 122 of file energy.h.

template<typename T = double>
using util::quantities::gigaelectronvolt_as = typedef concepts::rescale<electronvolt_as<T>, std::giga>

Type of energy stored in gigaelectronvolt.

Definition at line 119 of file energy.h.

Type of frequency stored in gigahertz, in double precision.

Definition at line 108 of file frequency.h.

template<typename T = double>
using util::quantities::gigahertz_as = typedef concepts::rescale<hertz_as<T>, std::giga>

Type of frequency stored in gigahertz.

Definition at line 105 of file frequency.h.

Type of potential stored in gigavolt, in double precision.

Definition at line 210 of file electromagnetism.h.

template<typename T = double>
using util::quantities::gigavolt_as = typedef concepts::rescale<volt_as<T>, std::giga>

Type of potential stored in gigavolt.

Definition at line 207 of file electromagnetism.h.

using util::quantities::hertz = typedef hertz_as<>

Type of frequency stored in hertz, in double precision.

Definition at line 78 of file frequency.h.

template<typename T = double>
using util::quantities::hertz_as = typedef scaled_hertz<std::ratio<1>, T>

Type of frequency stored in hertz.

Definition at line 75 of file frequency.h.

Type of data size stored in kibibytes, in long long precision.

Definition at line 111 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::kibibyte_as = typedef concepts::rescale<byte_as<T>, prefixes::kibi>

Type of data size stored in kibibytes.

Definition at line 104 of file datasize.h.

Alias for common language habits.

Definition at line 114 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::kibibytes_as = typedef kibibyte_as<T>

Alias for common language habits.

Definition at line 108 of file datasize.h.

Type of energy stored in kiloelectronvolt, in double precision.

Definition at line 102 of file energy.h.

template<typename T = double>
using util::quantities::kiloelectronvolt_as = typedef concepts::rescale<electronvolt_as<T>, std::kilo>

Type of energy stored in kiloelectronvolt.

Definition at line 99 of file energy.h.

Type of frequency stored in kilohertz, in double precision.

Definition at line 88 of file frequency.h.

template<typename T = double>
using util::quantities::kilohertz_as = typedef concepts::rescale<hertz_as<T>, std::kilo>

Type of frequency stored in kilohertz.

Definition at line 85 of file frequency.h.

Type of space stored in kilometers, in double precision.

Definition at line 424 of file spacetime.h.

template<typename T = double>
using util::quantities::kilometer_as = typedef concepts::rescale<meter_as<T>, std::kilo>

Type of space stored in kilometers.

Definition at line 417 of file spacetime.h.

Alias for common language habits.

Definition at line 427 of file spacetime.h.

template<typename T = double>
using util::quantities::kilometers_as = typedef kilometer_as<T>

Alias for common language habits.

Definition at line 421 of file spacetime.h.

Type of potential stored in kilovolt, in double precision.

Definition at line 190 of file electromagnetism.h.

template<typename T = double>
using util::quantities::kilovolt_as = typedef concepts::rescale<volt_as<T>, std::kilo>

Type of potential stored in kilovolt.

Definition at line 187 of file electromagnetism.h.

Type of data size stored in mebibytes, in long long precision.

Definition at line 128 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::mebibyte_as = typedef concepts::rescale<byte_as<T>, prefixes::mebi>

Type of data size stored in mebibytes.

Definition at line 121 of file datasize.h.

Alias for common language habits.

Definition at line 131 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::mebibytes_as = typedef mebibyte_as<T>

Alias for common language habits.

Definition at line 125 of file datasize.h.

Type of energy stored in megaelectronvolt, in double precision.

Definition at line 112 of file energy.h.

template<typename T = double>
using util::quantities::megaelectronvolt_as = typedef concepts::rescale<electronvolt_as<T>, std::mega>

Type of energy stored in megaelectronvolt.

Definition at line 109 of file energy.h.

Type of frequency stored in megahertz, in double precision.

Definition at line 98 of file frequency.h.

template<typename T = double>
using util::quantities::megahertz_as = typedef concepts::rescale<hertz_as<T>, std::mega>

Type of frequency stored in megahertz.

Definition at line 95 of file frequency.h.

Type of potential stored in megavolt, in double precision.

Definition at line 200 of file electromagnetism.h.

template<typename T = double>
using util::quantities::megavolt_as = typedef concepts::rescale<volt_as<T>, std::mega>

Type of potential stored in megavolt.

Definition at line 197 of file electromagnetism.h.

using util::quantities::meter = typedef meter_as<>

Type of space stored in meters, in double precision.

Definition at line 407 of file spacetime.h.

template<typename T = double>
using util::quantities::meter_as = typedef scaled_meter<std::ratio<1>, T>

Type of space stored in meters.

Definition at line 400 of file spacetime.h.

Alias for common language habits.

Definition at line 410 of file spacetime.h.

template<typename T = double>
using util::quantities::meters_as = typedef meter_as<T>

Alias for common language habits.

Definition at line 404 of file spacetime.h.

Type of charge stored in microcoulomb, in double precision.

Definition at line 97 of file electromagnetism.h.

template<typename T = double>
using util::quantities::microcoulomb_as = typedef concepts::rescale<coulomb_as<T>, std::micro>

Type of charge stored in microcoulomb.

Definition at line 94 of file electromagnetism.h.

Type of energy stored in microelectronvolt, in double precision.

Definition at line 82 of file energy.h.

template<typename T = double>
using util::quantities::microelectronvolt_as = typedef concepts::rescale<electronvolt_as<T>, std::micro>

Type of energy stored in microelectronvolt.

Definition at line 79 of file energy.h.

Type of space stored in micrometers, in double precision.

Definition at line 475 of file spacetime.h.

template<typename T = double>
using util::quantities::micrometer_as = typedef concepts::rescale<meter_as<T>, std::micro>

Type of space stored in micrometers.

Definition at line 468 of file spacetime.h.

Alias for common language habits.

Definition at line 478 of file spacetime.h.

template<typename T = double>
using util::quantities::micrometers_as = typedef micrometer_as<T>

Alias for common language habits.

Definition at line 472 of file spacetime.h.

Type of time stored in microseconds, in double precision.

Definition at line 116 of file spacetime.h.

template<typename T = double>
using util::quantities::microsecond_as = typedef concepts::rescale<second_as<T>, std::micro>

Type of time stored in microseconds.

Definition at line 109 of file spacetime.h.

Alias for common language habits.

Definition at line 119 of file spacetime.h.

template<typename T = double>
using util::quantities::microseconds_as = typedef microsecond_as<T>

Alias for common language habits.

Definition at line 113 of file spacetime.h.

Type of potential stored in microvolt, in double precision.

Definition at line 180 of file electromagnetism.h.

template<typename T = double>
using util::quantities::microvolt_as = typedef concepts::rescale<volt_as<T>, std::micro>

Type of potential stored in microvolt.

Definition at line 177 of file electromagnetism.h.

Type of charge stored in millicoulomb, in double precision.

Definition at line 87 of file electromagnetism.h.

template<typename T = double>
using util::quantities::millicoulomb_as = typedef concepts::rescale<coulomb_as<T>, std::milli>

Type of charge stored in millicoulomb.

Definition at line 84 of file electromagnetism.h.

Type of energy stored in millielectronvolt, in double precision.

Definition at line 92 of file energy.h.

template<typename T = double>
using util::quantities::millielectronvolt_as = typedef concepts::rescale<electronvolt_as<T>, std::milli>

Type of energy stored in millielectronvolt.

Definition at line 89 of file energy.h.

Type of space stored in millimeters, in double precision.

Definition at line 458 of file spacetime.h.

template<typename T = double>
using util::quantities::millimeter_as = typedef concepts::rescale<meter_as<T>, std::milli>

Type of space stored in millimeters.

Definition at line 451 of file spacetime.h.

Alias for common language habits.

Definition at line 461 of file spacetime.h.

template<typename T = double>
using util::quantities::millimeters_as = typedef millimeter_as<T>

Alias for common language habits.

Definition at line 455 of file spacetime.h.

Type of time stored in milliseconds, in double precision.

Definition at line 99 of file spacetime.h.

template<typename T = double>
using util::quantities::millisecond_as = typedef concepts::rescale<second_as<T>, std::milli>

Type of time stored in milliseconds.

Definition at line 92 of file spacetime.h.

Alias for common language habits.

Definition at line 102 of file spacetime.h.

template<typename T = double>
using util::quantities::milliseconds_as = typedef millisecond_as<T>

Alias for common language habits.

Definition at line 96 of file spacetime.h.

Type of potential stored in millivolt, in double precision.

Definition at line 170 of file electromagnetism.h.

template<typename T = double>
using util::quantities::millivolt_as = typedef concepts::rescale<volt_as<T>, std::milli>

Type of potential stored in millivolt.

Definition at line 167 of file electromagnetism.h.

Type of charge stored in nanocoulomb, in double precision.

Definition at line 107 of file electromagnetism.h.

template<typename T = double>
using util::quantities::nanocoulomb_as = typedef concepts::rescale<coulomb_as<T>, std::nano>

Type of charge stored in nanocoulomb.

Definition at line 104 of file electromagnetism.h.

Type of space stored in nanometers, in double precision.

Definition at line 492 of file spacetime.h.

template<typename T = double>
using util::quantities::nanometer_as = typedef concepts::rescale<meter_as<T>, std::nano>

Type of space stored in nanometers.

Definition at line 485 of file spacetime.h.

Alias for common language habits.

Definition at line 495 of file spacetime.h.

template<typename T = double>
using util::quantities::nanometers_as = typedef nanometer_as<T>

Alias for common language habits.

Definition at line 489 of file spacetime.h.

Type of time stored in nanoseconds, in double precision.

Definition at line 133 of file spacetime.h.

template<typename T = double>
using util::quantities::nanosecond_as = typedef concepts::rescale<second_as<T>, std::nano>

Type of time stored in nanoseconds.

Definition at line 126 of file spacetime.h.

Alias for common language habits.

Definition at line 136 of file spacetime.h.

template<typename T = double>
using util::quantities::nanoseconds_as = typedef nanosecond_as<T>

Alias for common language habits.

Definition at line 130 of file spacetime.h.

Type of data size stored in pebibytes, in long long precision.

Definition at line 179 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::pebibyte_as = typedef concepts::rescale<byte_as<T>, prefixes::pebi>

Type of data size stored in pebibytes.

Definition at line 172 of file datasize.h.

Alias for common language habits.

Definition at line 182 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::pebibytes_as = typedef pebibyte_as<T>

Alias for common language habits.

Definition at line 176 of file datasize.h.

Type of charge stored in picocoulomb, in double precision.

Definition at line 117 of file electromagnetism.h.

template<typename T = double>
using util::quantities::picocoulomb_as = typedef concepts::rescale<coulomb_as<T>, std::pico>

Type of charge stored in picocoulomb.

Definition at line 114 of file electromagnetism.h.

Type of space stored in picometers, in double precision.

Definition at line 509 of file spacetime.h.

template<typename T = double>
using util::quantities::picometer_as = typedef concepts::rescale<meter_as<T>, std::pico>

Type of space stored in picometers.

Definition at line 502 of file spacetime.h.

Alias for common language habits.

Definition at line 512 of file spacetime.h.

template<typename T = double>
using util::quantities::picometers_as = typedef picometer_as<T>

Alias for common language habits.

Definition at line 506 of file spacetime.h.

Type of time stored in picoseconds, in double precision.

Definition at line 150 of file spacetime.h.

template<typename T = double>
using util::quantities::picosecond_as = typedef concepts::rescale<second_as<T>, std::pico>

Type of time stored in picoseconds.

Definition at line 143 of file spacetime.h.

Alias for common language habits.

Definition at line 153 of file spacetime.h.

template<typename T = double>
using util::quantities::picoseconds_as = typedef picosecond_as<T>

Alias for common language habits.

Definition at line 147 of file spacetime.h.

template<typename R , typename T = unsigned long long int>
using util::quantities::scaled_byte = typedef concepts::scaled_quantity<units::Byte, R, T>

The most generic units::Byte-based quantity.

Definition at line 80 of file datasize.h.

template<typename R , typename T = double>
using util::quantities::scaled_coulomb = typedef concepts::scaled_quantity<units::Coulomb, R, T>

The most generic units::Coulomb-based quantity.

Definition at line 67 of file electromagnetism.h.

template<typename R , typename T = double>
using util::quantities::scaled_electronvolt = typedef concepts::scaled_quantity<units::ElectronVolt, R, T>

The most generic units::ElectronVolt-based quantity.

Definition at line 62 of file energy.h.

template<typename R , typename T = double>
using util::quantities::scaled_hertz = typedef concepts::scaled_quantity<units::Hertz, R, T>

The most generic units::Hertz-based quantity.

Definition at line 68 of file frequency.h.

template<typename R , typename T = double>
using util::quantities::scaled_meter = typedef concepts::scaled_quantity<units::Meter, R, T>

The most generic units::Meter-based quantity.

Definition at line 393 of file spacetime.h.

template<typename R , typename T = double>
using util::quantities::scaled_second = typedef concepts::scaled_quantity<units::Second, R, T>

The most generic units::Second-based quantity.

Definition at line 68 of file spacetime.h.

template<typename R , typename T = double>
using util::quantities::scaled_volt = typedef concepts::scaled_quantity<units::Volt, R, T>

The most generic units::Volt-based quantity.

Definition at line 150 of file electromagnetism.h.

Type of time stored in seconds, in double precision.

Definition at line 82 of file spacetime.h.

template<typename T = double>
using util::quantities::second_as = typedef scaled_second<std::ratio<1>, T>

Type of time stored in seconds.

Definition at line 75 of file spacetime.h.

Alias for common language habits.

Definition at line 85 of file spacetime.h.

template<typename T = double>
using util::quantities::seconds_as = typedef second_as<T>

Alias for common language habits.

Definition at line 79 of file spacetime.h.

Type of data size stored in tebibytes, in long long precision.

Definition at line 162 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::tebibyte_as = typedef concepts::rescale<byte_as<T>, prefixes::tebi>

Type of data size stored in tebibytes.

Definition at line 155 of file datasize.h.

Alias for common language habits.

Definition at line 165 of file datasize.h.

template<typename T = unsigned long long int>
using util::quantities::tebibytes_as = typedef tebibyte_as<T>

Alias for common language habits.

Definition at line 159 of file datasize.h.

Type of energy stored in teraelectronvolt, in double precision.

Definition at line 132 of file energy.h.

template<typename T = double>
using util::quantities::teraelectronvolt_as = typedef concepts::rescale<electronvolt_as<T>, std::tera>

Type of energy stored in teraelectronvolt.

Definition at line 129 of file energy.h.

using util::quantities::tick = typedef tick_as<>

Tick number, represented by std::ptrdiff_t.

Definition at line 73 of file electronics.h.

template<typename T = std::ptrdiff_t>
using util::quantities::tick_as = typedef concepts::scaled_quantity<units::Tick, std::ratio<1>, T>

Tick number, represented by the specified type T.

Definition at line 66 of file electronics.h.

using util::quantities::tick_d = typedef tick_as<double>

Tick number, represented by double.

Definition at line 85 of file electronics.h.

using util::quantities::tick_f = typedef tick_as<float>

Tick number, represented by float.

Definition at line 79 of file electronics.h.

using util::quantities::ticks = typedef tick

Alias for common language habits.

Definition at line 76 of file electronics.h.

template<typename T = tick_as<>::value_t>
using util::quantities::ticks_as = typedef tick_as<T>

Alias for common language habits.

Definition at line 70 of file electronics.h.

Alias for common language habits.

Definition at line 88 of file electronics.h.

Alias for common language habits.

Definition at line 82 of file electronics.h.

using util::quantities::volt = typedef volt_as<>

Type of potential stored in volts, in double precision.

Definition at line 160 of file electromagnetism.h.

template<typename T = double>
using util::quantities::volt_as = typedef scaled_volt<std::ratio<1>, T>

Type of potential stored in volt.

Definition at line 157 of file electromagnetism.h.

Function Documentation

template<typename IV >
IV util::quantities::makeInterval ( std::string_view  s,
bool  unitOptional = false 
)

Returns an interval of the specified type parsed from a string.

Template Parameters
IVthe type of quantity interval to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity interval of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value
See also
util::makeQuantity()

This function behaves like util::makeQuantity().

Definition at line 1270 of file intervals.h.

Referenced by util::quantities::NoCategory::name().

1271 {
1272  using quantity_t = typename IV::quantity_t;
1273  return {util::quantities::makeQuantity<quantity_t>(s, unitOptional)};
1274 } // util::quantities::makeInterval(string_view)
template<typename IV >
IV util::quantities::makeInterval ( std::string const &  s,
bool  unitOptional = false 
)

Returns an interval of the specified type parsed from a string.

Template Parameters
IVthe type of quantity interval to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity interval of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value
See also
util::makeQuantity()

This function behaves like util::makeQuantity().

Definition at line 1278 of file intervals.h.

1279 {
1280  using quantity_t = typename IV::quantity_t;
1281  return {util::quantities::makeQuantity<quantity_t>(s, unitOptional)};
1282 } // util::quantities::makeInterval(string)
template<typename IV >
IV util::quantities::makeInterval ( char const *  s,
bool  unitOptional = false 
)

Returns an interval of the specified type parsed from a string.

Template Parameters
IVthe type of quantity interval to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity interval of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value
See also
util::makeQuantity()

This function behaves like util::makeQuantity().

Definition at line 1286 of file intervals.h.

1287 {
1288  using quantity_t = typename IV::quantity_t;
1289  return {util::quantities::makeQuantity<quantity_t>(s, unitOptional)};
1290 } // util::quantities::makeInterval(C-string)
template<typename PT >
PT util::quantities::makePoint ( std::string_view  s,
bool  unitOptional = false 
)

Returns a point of the specified type parsed from a string.

Template Parameters
PTthe type of quantity point to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity point of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value
See also
util::makeQuantity()

This function behaves like util::makeQuantity().

Definition at line 1294 of file intervals.h.

Referenced by util::quantities::NoCategory::name().

1295 {
1296  using quantity_t = typename PT::quantity_t;
1297  return {util::quantities::makeQuantity<quantity_t>(s, unitOptional)};
1298 } // util::quantities::makePoint(string_view)
template<typename PT >
PT util::quantities::makePoint ( std::string const &  s,
bool  unitOptional = false 
)

Returns a point of the specified type parsed from a string.

Template Parameters
PTthe type of quantity point to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity point of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value
See also
util::makeQuantity()

This function behaves like util::makeQuantity().

Definition at line 1302 of file intervals.h.

1303 {
1304  using quantity_t = typename PT::quantity_t;
1305  return {util::quantities::makeQuantity<quantity_t>(s, unitOptional)};
1306 } // util::quantities::makePoint(string)
template<typename PT >
PT util::quantities::makePoint ( char const *  s,
bool  unitOptional = false 
)

Returns a point of the specified type parsed from a string.

Template Parameters
PTthe type of quantity point to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity point of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value
See also
util::makeQuantity()

This function behaves like util::makeQuantity().

Definition at line 1310 of file intervals.h.

1311 {
1312  using quantity_t = typename PT::quantity_t;
1313  return {util::quantities::makeQuantity<quantity_t>(s, unitOptional)};
1314 } // util::quantities::makePoint(C-string)
template<typename Quantity >
Quantity util::quantities::makeQuantity ( std::string_view  s,
bool  unitOptional = false 
)

Returns a quantity of the specified type parsed from a string.

Template Parameters
Quantitythe quantity to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value (including an unrecognised unit prefix)

A quantity of type Quantity is returned, whose value is interpreted from the content of s. The standard format includes a real number, a space and a unit symbol. If unitOptional is false, that unit is required, otherwise it is optional and defaults to the unit and scale in Quantity. The base unit in s, when present, must exactly match the base unit of Quantity; the scale may differ, in which case the proper conversion is applied.

Example:

auto const t = util::quantities::makeQuantity<microsecond>("7 ms");

will assign to t the value 7000.

Definition at line 1582 of file quantities.h.

Referenced by util::quantities::concepts::to_string().

1583 {
1584  return util::quantities::makeQuantity<Quantity>(std::string{s.begin(), s.end()}, unitOptional);
1585 } // util::quantities::makeQuantity(string_view)
template<typename Quantity >
Quantity util::quantities::makeQuantity ( std::string const &  s,
bool  unitOptional = false 
)

Returns a quantity of the specified type parsed from a string.

Template Parameters
Quantitythe quantity to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value (including an unrecognised unit prefix)

A quantity of type Quantity is returned, whose value is interpreted from the content of s. The standard format includes a real number, a space and a unit symbol. If unitOptional is false, that unit is required, otherwise it is optional and defaults to the unit and scale in Quantity. The base unit in s, when present, must exactly match the base unit of Quantity; the scale may differ, in which case the proper conversion is applied.

Example:

auto const t = util::quantities::makeQuantity<microsecond>("7 ms");

will assign to t the value 7000.

Definition at line 1549 of file quantities.h.

References util::quantities::concepts::to_string(), and value.

1550 {
1551  //
1552  // all this function is horrible;
1553  // some redesign is needed...
1554  //
1555  using value_t = typename Quantity::value_t;
1556 
1557  auto const [num_s, factor] = details::readUnit<Quantity>(s, unitOptional);
1558 
1559  char* parseEnd = nullptr;
1560  auto const value = static_cast<value_t>(std::strtod(num_s.c_str(), &parseEnd));
1561  const char* send = num_s.c_str() + num_s.length();
1562  if (parseEnd == num_s.c_str()) {
1563  throw ValueError("Could not convert '" + num_s + "' into a number!");
1564  }
1565  while (parseEnd != send) {
1566  if (!std::isblank(static_cast<unsigned char>(*parseEnd))) {
1567  throw ExtraCharactersError("Spurious characters after value " + std::to_string(value) +
1568  " in '" + num_s + "' ('" + std::string(parseEnd, send - parseEnd) +
1569  "')\n");
1570  }
1571  ++parseEnd;
1572  } // while
1573 
1574  //
1575  // create and return the quantity
1576  //
1577  return Quantity{static_cast<value_t>(value * factor)};
1578 } // util::quantities::makeQuantity(string_view)
std::string to_string(Quantity< Args... > const &q)
Definition: quantities.h:933
double value
Definition: spectrum.C:18
template<typename Quantity >
Quantity util::quantities::makeQuantity ( char const *  s,
bool  unitOptional = false 
)

Returns a quantity of the specified type parsed from a string.

Template Parameters
Quantitythe quantity to be returned
Parameters
sthe string to be parsed
unitOptional(default: false) whether unit is not required in s
Returns
a quantity of the specified type parsed from a string
Exceptions
MissingUnits does not contain the required unit
ValueErrorthe numerical value in s is not parseable
ExtraCharactersErrorspurious characters after the numeric value (including an unrecognised unit prefix)

A quantity of type Quantity is returned, whose value is interpreted from the content of s. The standard format includes a real number, a space and a unit symbol. If unitOptional is false, that unit is required, otherwise it is optional and defaults to the unit and scale in Quantity. The base unit in s, when present, must exactly match the base unit of Quantity; the scale may differ, in which case the proper conversion is applied.

Example:

auto const t = util::quantities::makeQuantity<microsecond>("7 ms");

will assign to t the value 7000.

Definition at line 1589 of file quantities.h.

1590 {
1591  return util::quantities::makeQuantity<Quantity>(std::string_view{s}, unitOptional);
1592 } // util::quantities::makeQuantity(string)