Classes | Namespaces | Typedefs | Functions | Variables
quantities.h File Reference

Numeric variable proxies with embedded unit of measurement. More...

#include "lardataalg/Utilities/constexpr_math.h"
#include "larcorealg/CoreUtils/MetaUtils.h"
#include "larcorealg/CoreUtils/StdUtils.h"
#include "boost/integer/common_factor_rt.hpp"
#include <ostream>
#include <map>
#include <string>
#include <string_view>
#include <regex>
#include <ratio>
#include <limits>
#include <functional>
#include <type_traits>
#include <cctype>

Go to the source code of this file.

Classes

struct  util::quantities::concepts::details::invert_ratio< R >
 
struct  util::quantities::concepts::details::ratio_simplifier< R >
 
struct  util::quantities::concepts::details::has_unit< U >
 Trait: true_type if U is a ScaledUnit-based object. More...
 
struct  util::quantities::concepts::details::is_quantity< Q >
 Trait: true_type if Q is a Quantity specialization. More...
 
struct  util::quantities::concepts::details::has_quantity< Q >
 Trait: true_type if Q is a Quantity-based object. More...
 
struct  util::quantities::concepts::details::quantity_value_type< T >
 
struct  util::quantities::concepts::details::is_value_compatible_with< T, Q >
 
struct  util::quantities::concepts::details::has_value_compatible_with< T, U >
 
class  util::quantities::concepts::details::numeric_limits< Q >
 Limits of a quantity are the same as the underlying type. More...
 
struct  util::quantities::concepts::UnitBase
 
struct  util::quantities::concepts::Prefix< R >
 
struct  util::quantities::concepts::ScaledUnit< U, R >
 
struct  util::quantities::concepts::Quantity< Unit, T >
 A value measured in the specified unit. More...
 
struct  util::quantities::MissingUnit
 String representing a quantity has no unit. More...
 
struct  util::quantities::InvalidUnitPrefix
 String representing a quantity has unsupported unit prefix. More...
 
struct  util::quantities::MismatchingUnit
 String representing a quantity has incompatible unit. More...
 
struct  util::quantities::ValueError
 String representing a quantity has an invalid number. More...
 
struct  util::quantities::ExtraCharactersError
 String representing a quantity has spurious characters after the number. More...
 
struct  util::quantities::concepts::details::invert_ratio< std::ratio< Num, Den > >
 
struct  util::quantities::concepts::details::ratio_simplifier< std::ratio< Num, Den > >
 
struct  util::quantities::concepts::details::has_unit_impl< U, Enable >
 
struct  util::quantities::concepts::details::has_unit_impl < U, std::enable_if_t< util::always_true_v< typename U::unit_t > > >
 
struct  util::quantities::concepts::details::has_unit< U >
 Trait: true_type if U is a ScaledUnit-based object. More...
 
struct  util::quantities::concepts::details::is_quantity< Q >
 Trait: true_type if Q is a Quantity specialization. More...
 
struct  util::quantities::concepts::details::is_quantity< Quantity< Args... > >
 
struct  util::quantities::concepts::details::has_quantity_impl< Q, Enable >
 
struct  util::quantities::concepts::details::has_quantity_impl < Q, std::enable_if_t< util::always_true_v< typename Q::quantity_t > > >
 
struct  util::quantities::concepts::details::has_quantity< Q >
 Trait: true_type if Q is a Quantity-based object. More...
 
struct  util::quantities::concepts::details::quantity_value_type_impl< T, typename >
 
struct  util::quantities::concepts::details::always_true_v< typename T::value_t >
 
struct  util::quantities::concepts::details::quantity_value_type< T >
 
struct  util::quantities::concepts::details::is_value_compatible_with< T, Q >
 
struct  util::quantities::concepts::details::has_value_compatible_with< T, U >
 
class  util::quantities::concepts::details::numeric_limits< Q >
 Limits of a quantity are the same as the underlying type. More...
 
struct  std::hash< util::quantities::concepts::Quantity< Args... > >
 Hash function of a quantity is delegated to its value. More...
 
class  std::numeric_limits< util::quantities::concepts::Quantity< Unit, T > >
 Limits of a quantity are the same as the underlying type. More...
 
class  std::numeric_limits < util::quantities::concepts::Quantity< Unit, T > const >
 
class  std::numeric_limits < util::quantities::concepts::Quantity< Unit, T > volatile >
 
class  std::numeric_limits < util::quantities::concepts::Quantity< Unit, T > const volatile >
 

Namespaces

 util::quantities
 Types of variables with a unit.
 
 util::quantities::concepts
 Infrastructure for the quantities library.
 
 util::quantities::concepts::details
 
 util::quantities::prefixes
 Definitions of additional prefixes.
 
 util::quantities::units
 Definitions of actual units.
 
 util::quantities::details
 

Typedefs

template<typename R >
using util::quantities::concepts::details::invert_t = typename invert_ratio< R >::type
 
template<typename R >
using util::quantities::concepts::details::simplify_ratio = typename ratio_simplifier< R >::type
 
template<typename T >
using util::quantities::concepts::details::quantity_value_t = typename quantity_value_type< T >::type
 
template<typename ARatio , typename BRatio >
using util::quantities::concepts::simplified_ratio_multiply = details::simplify_ratio< std::ratio_multiply< ARatio, BRatio >>
 A ratio product (like std::ratio_multiply) with simplified terms. More...
 
template<typename NumRatio , typename DenRatio >
using util::quantities::concepts::simplified_ratio_divide = details::simplify_ratio< std::ratio_divide< NumRatio, DenRatio >>
 A ratio division (like std::ratio_divide) with simplified terms. More...
 
template<typename Unit , typename Ratio , typename T >
using util::quantities::concepts::scaled_quantity = Quantity< ScaledUnit< Unit, Ratio >, T >
 Alias for a quantity based on a scaled unit. More...
 
template<typename Q , typename R , typename T = typename Q::value_t>
using util::quantities::concepts::rescale = Quantity< ScaledUnit< typename Q::unit_t::baseunit_t, R >, T >
 Type of a quantity like Q, but with a different unit scale R. More...
 

Functions

template<typename Ratio , typename Value >
static constexpr auto util::quantities::concepts::details::applyRatioToValue (Value &&v)
 Applies the specified Ratio to the value in v. More...
 
template<typename U , typename R >
std::ostream & util::quantities::concepts::operator<< (std::ostream &out, ScaledUnit< U, R > const &unit)
 
template<typename... Args>
std::ostream & util::quantities::concepts::operator<< (std::ostream &out, Quantity< Args... > const q)
 
template<typename... Args>
std::string util::quantities::concepts::to_string (ScaledUnit< Args... > const &unit)
 
template<typename... Args>
std::string util::quantities::concepts::to_string (Quantity< Args... > const &q)
 
template<typename Quantity >
std::pair< std::string, typename Quantity::value_t > util::quantities::details::readUnit (std::string const &str, bool unitOptional=false)
 Parses the unit of a string representing a Quantity. More...
 
Arithmetic operations on Quantity

These operations, as well as the ones implemented as member functions, are provided for convenience.

Here the symmetric operations are defined, where different operands can be swapped.

template<typename U , typename T >
constexpr Quantity< U, T > util::quantities::concepts::operator+ (Quantity< U, T > const q, T shift)=delete
 
template<typename U , typename T >
constexpr Quantity< U, T > util::quantities::concepts::operator+ (T shift, Quantity< U, T > const q)=delete
 
template<typename U , typename T >
constexpr Quantity< U, T > util::quantities::concepts::operator- (Quantity< U, T > const q, T shift)=delete
 
template<typename U , typename T >
constexpr Quantity< U, T > util::quantities::concepts::operator- (T shift, Quantity< U, T > const q)=delete
 
template<typename U , typename T , typename OT >
constexpr std::enable_if_t< Quantity< U, T >::template is_compatible_value_v< OT >, Quantity< U, T > > util::quantities::concepts::operator* (Quantity< U, T > const q, OT factor)
 Multiplication with a scalar. More...
 
template<typename U , typename T , typename OT >
constexpr std::enable_if_t< Quantity< U, T >::template is_compatible_value_v< OT >, Quantity< U, T > > util::quantities::concepts::operator* (OT factor, Quantity< U, T > const q)
 
template<typename AU , typename AT , typename BU , typename BT >
constexpr auto util::quantities::concepts::operator* (Quantity< AU, AT >, Quantity< BU, BT >) -> decltype(std::declval< AT >()*std::declval< BT >())=delete
 Multiplication between quantities is forbidden. More...
 
template<typename U , typename T , typename OT >
constexpr std::enable_if_t< Quantity< U, T >::template is_compatible_value_v< OT >, Quantity< U, T > > util::quantities::concepts::operator/ (Quantity< U, T > q, OT quot)
 
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. More...
 
template<typename Quantity >
Quantity util::quantities::makeQuantity (std::string const &s, bool unitOptional=false)
 
template<typename Quantity >
Quantity util::quantities::makeQuantity (char const *s, bool unitOptional=false)
 

Variables

template<typename U >
constexpr bool util::quantities::concepts::details::has_unit_v = has_unit<U>()
 Trait: true if U is a ScaledUnit-based object. More...
 
template<typename Q >
constexpr bool util::quantities::concepts::details::is_quantity_v = is_quantity<Q>()
 Trait: true if Q is a Quantity specialization. More...
 
template<typename Q >
constexpr bool util::quantities::concepts::details::has_quantity_v = has_quantity<Q>()
 Trait: true if Q is a Quantity-based object. More...
 
template<typename T , typename Q >
constexpr bool util::quantities::concepts::details::is_value_compatible_with_v = is_value_compatible_with<T, Q>()
 Trait: true if the type T is compatible with the value of Q. More...
 
template<typename T , typename U >
constexpr bool util::quantities::concepts::details::has_value_compatible_with_v = has_value_compatible_with<T, U>()
 Trait: true if the value type of T is compatible with U's. More...
 

Detailed Description

Numeric variable proxies with embedded unit of measurement.

Author
Gianluca Petrillo (petri.nosp@m.llo@.nosp@m.slac..nosp@m.stan.nosp@m.ford..nosp@m.edu)
Date
October 30, 2018
See also
lardataalg/Utilities/quantites/spacetime.h

Infrastructure for a library for variables with a given unit of measurement.

Why not Boost::units?

My attempt to use Boost::units for the implementation (or directly) failed because the quantity in that library is bound to a dimension without a prefix modifier, e.g. always represents seconds, while we may need nanoseconds. Having a type representing a time in nanoseconds is not trivial, and having two types for representation of time in nanoseconds and microseconds respectively is plain complicate.

On the converse, that library provides arithmetic which is checked for dimensionality correctness. That is beyond the scope of this set of types.

Note
This code is C++17 only.

Definition in file quantities.h.