Unit Conversion and Dimensional Analysis Library  2.3.0
A compile-time c++14 unit conversion library
Public Types | Public Member Functions | Protected Types | List of all members
units::unit_t< Units, T, NonLinearScale > Class Template Reference

Container for values which represent quantities of a given unit. More...

#include <units.h>

Inheritance diagram for units::unit_t< Units, T, NonLinearScale >:
units::linear_scale< double >

Public Types

typedef NonLinearScale< T > non_linear_scale_type
 Type of the non-linear scale of the unit_t (e.g. linear_scale)
 
typedef T underlying_type
 Type of the underlying storage of the unit_t (e.g. double)
 
typedef T value_type
 Synonym for underlying type. May be removed in future versions. Prefer underlying_type.
 
typedef Units unit_type
 Type of unit the unit_t represents (e.g. meters)
 

Public Member Functions

constexpr unit_t ()=default
 default constructor.
 
template<class... Args>
constexpr unit_t (const T value, const Args &... args) noexcept
 constructor More...
 
template<class Ty , class = typename std::enable_if<traits::is_dimensionless_unit<Units>::value && std::is_arithmetic<Ty>::value>::type>
constexpr unit_t (const Ty value) noexcept
 constructor More...
 
template<class Rep , class Period , class = std::enable_if_t<std::is_arithmetic<Rep>::value && traits::is_ratio<Period>::value>>
constexpr unit_t (const std::chrono::duration< Rep, Period > &value) noexcept
 chrono constructor More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr unit_t (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) noexcept
 copy constructor (converting) More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
unit_toperator= (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) noexcept
 assignment More...
 
template<class Ty , class = std::enable_if_t<traits::is_dimensionless_unit<Units>::value && std::is_arithmetic<Ty>::value>>
unit_toperator= (const Ty &rhs) noexcept
 assignment More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool operator< (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 less-than More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool operator<= (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 less-than or equal More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool operator> (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 greater-than More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool operator>= (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 greater-than or equal More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs, std::enable_if_t< std::is_floating_point< T >::value||std::is_floating_point< Ty >::value, int > = 0>
constexpr bool operator== (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 equality More...
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs, std::enable_if_t< std::is_integral< T >::value &&std::is_integral< Ty >::value, int > = 0>
constexpr bool operator== (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool operator!= (const unit_t< UnitsRhs, Ty, NlsRhs > &rhs) const noexcept
 inequality More...
 
constexpr underlying_type value () const noexcept
 unit value More...
 
template<typename Ty , class = std::enable_if_t<std::is_arithmetic<Ty>::value>>
constexpr Ty to () const noexcept
 unit value More...
 
template<typename Ty , class = std::enable_if_t<std::is_arithmetic<Ty>::value>>
constexpr Ty toLinearized () const noexcept
 linearized unit value More...
 
template<class U >
constexpr unit_t< U > convert () const noexcept
 conversion More...
 
template<class Ty , std::enable_if_t< traits::is_dimensionless_unit< Units >::value &&std::is_arithmetic< Ty >::value, int > = 0>
constexpr operator Ty () const noexcept
 implicit type conversion. More...
 
template<class Ty , std::enable_if_t<!traits::is_dimensionless_unit< Units >::value &&std::is_arithmetic< Ty >::value, int > = 0>
constexpr operator Ty () const noexcept
 explicit type conversion. More...
 
template<typename U = Units, std::enable_if_t< units::traits::is_convertible_unit< U, unit< std::ratio< 1 >, category::time_unit >>::value, int > = 0>
constexpr operator std::chrono::nanoseconds () const noexcept
 chrono implicit type conversion. More...
 
constexpr const char * name () const noexcept
 returns the unit name
 
constexpr const char * abbreviation () const noexcept
 returns the unit abbreviation
 
- Public Member Functions inherited from units::linear_scale< double >
constexpr linear_scale ()=default
 default constructor.

 
constexpr linear_scale (const linear_scale &)=default
 
linear_scaleoperator= (const linear_scale &)=default
 

Protected Types

using nls = NonLinearScale< T >
 

Additional Inherited Members

- Public Attributes inherited from units::linear_scale< double >
double m_value
 linearized value.

 

Detailed Description

template<class Units, typename T = double, template< typename > class NonLinearScale = linear_scale>
class units::unit_t< Units, T, NonLinearScale >

Container for values which represent quantities of a given unit.

Stores a value which represents a quantity in the given units. Unit containers (except scalar values) are not convertible to built-in c++ types, in order to provide type safety in dimensional analysis. Unit containers are implicitly convertible to other compatible unit container types. Unit containers support various types of arithmetic operations, depending on their scale type.

The value of a unit_t can only be changed on construction, or by assignment from another unit_t type. If necessary, the underlying value can be accessed using operator():

meter_t m(5.0);
double val = m(); // val == 5.0

.

Template Parameters
Unitsunit tag for which type of units the unit_t represents (e.g. meters)
Tunderlying type of the storage. Defaults to double.
NonLinearScaleoptional scale class for the units. Defaults to linear (i.e. does not scale the unit value). Examples of non-linear scales could be logarithmic, decibel, or richter scales. Non-linear scales must adhere to the non-linear-scale concept, i.e. is_nonlinear_scale<...>::value must be true.
See also

Constructor & Destructor Documentation

◆ unit_t() [1/4]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class... Args>
constexpr units::unit_t< Units, T, NonLinearScale >::unit_t ( const T  value,
const Args &...  args 
)
inlineexplicitconstexprnoexcept

constructor

constructs a new unit_t using the non-linear scale's constructor.

Parameters
[in]valueunit value magnitude.
[in]argsadditional constructor arguments are forwarded to the non-linear scale constructor. Which args are required depends on which scale is used. For the default (linear) scale, no additional args are necessary.

◆ unit_t() [2/4]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class Ty , class = typename std::enable_if<traits::is_dimensionless_unit<Units>::value && std::is_arithmetic<Ty>::value>::type>
constexpr units::unit_t< Units, T, NonLinearScale >::unit_t ( const Ty  value)
inlineconstexprnoexcept

constructor

enable implicit conversions from T types ONLY for linear scalar units

Parameters
[in]valuevalue of the unit_t

◆ unit_t() [3/4]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class Rep , class Period , class = std::enable_if_t<std::is_arithmetic<Rep>::value && traits::is_ratio<Period>::value>>
constexpr units::unit_t< Units, T, NonLinearScale >::unit_t ( const std::chrono::duration< Rep, Period > &  value)
inlineconstexprnoexcept

chrono constructor

enable implicit conversions from std::chrono::duration types ONLY for time units

Parameters
[in]valuevalue of the unit_t

◆ unit_t() [4/4]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr units::unit_t< Units, T, NonLinearScale >::unit_t ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs)
inlineconstexprnoexcept

copy constructor (converting)

performs implicit unit conversions if required.

Parameters
[in]rhsunit to copy.

Member Function Documentation

◆ convert()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class U >
constexpr unit_t<U> units::unit_t< Units, T, NonLinearScale >::convert ( ) const
inlineconstexprnoexcept

conversion

Converts to a different unit container. Units can be converted to other containers implicitly, but this can be used in cases where explicit notation of a conversion is beneficial, or where an r-value container is needed.

Template Parameters
Uunit (not unit_t) to convert to
Returns
a unit container with the specified units containing the equivalent value to *this.

◆ operator std::chrono::nanoseconds()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<typename U = Units, std::enable_if_t< units::traits::is_convertible_unit< U, unit< std::ratio< 1 >, category::time_unit >>::value, int > = 0>
constexpr units::unit_t< Units, T, NonLinearScale >::operator std::chrono::nanoseconds ( ) const
inlineconstexprnoexcept

chrono implicit type conversion.

only enabled for time unit types.

◆ operator Ty() [1/2]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class Ty , std::enable_if_t< traits::is_dimensionless_unit< Units >::value &&std::is_arithmetic< Ty >::value, int > = 0>
constexpr units::unit_t< Units, T, NonLinearScale >::operator Ty ( ) const
inlineconstexprnoexcept

implicit type conversion.

only enabled for scalar unit types.

◆ operator Ty() [2/2]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class Ty , std::enable_if_t<!traits::is_dimensionless_unit< Units >::value &&std::is_arithmetic< Ty >::value, int > = 0>
constexpr units::unit_t< Units, T, NonLinearScale >::operator Ty ( ) const
inlineexplicitconstexprnoexcept

explicit type conversion.

only enabled for non-dimensionless unit types.

◆ operator!=()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool units::unit_t< Units, T, NonLinearScale >::operator!= ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs) const
inlineconstexprnoexcept

inequality

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is not equal to the value of rhs.
Note
This may not be suitable for all applications when the underlying_type of unit_t is a double.

◆ operator<()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool units::unit_t< Units, T, NonLinearScale >::operator< ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs) const
inlineconstexprnoexcept

less-than

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is less than the value of rhs

◆ operator<=()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool units::unit_t< Units, T, NonLinearScale >::operator<= ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs) const
inlineconstexprnoexcept

less-than or equal

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is less than or equal to the value of rhs

◆ operator=() [1/2]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class Ty , class = std::enable_if_t<traits::is_dimensionless_unit<Units>::value && std::is_arithmetic<Ty>::value>>
unit_t& units::unit_t< Units, T, NonLinearScale >::operator= ( const Ty &  rhs)
inlinenoexcept

assignment

performs implicit conversions from built-in types ONLY for scalar units

Parameters
[in]rhsvalue to copy.

◆ operator=() [2/2]

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
unit_t& units::unit_t< Units, T, NonLinearScale >::operator= ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs)
inlinenoexcept

assignment

performs implicit unit conversions if required

Parameters
[in]rhsunit to copy.

◆ operator==()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs, std::enable_if_t< std::is_floating_point< T >::value||std::is_floating_point< Ty >::value, int > = 0>
constexpr bool units::unit_t< Units, T, NonLinearScale >::operator== ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs) const
inlineconstexprnoexcept

equality

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this exactly equal to the value of rhs.
Note
This may not be suitable for all applications when the underlying_type of unit_t is a double.

◆ operator>()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool units::unit_t< Units, T, NonLinearScale >::operator> ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs) const
inlineconstexprnoexcept

greater-than

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is greater than the value of rhs

◆ operator>=()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<class UnitsRhs , typename Ty , template< typename > class NlsRhs>
constexpr bool units::unit_t< Units, T, NonLinearScale >::operator>= ( const unit_t< UnitsRhs, Ty, NlsRhs > &  rhs) const
inlineconstexprnoexcept

greater-than or equal

compares the linearized value of two units. Performs unit conversions if necessary.

Parameters
[in]rhsright-hand side unit for the comparison
Returns
true IFF the value of this is greater than or equal to the value of rhs

◆ to()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<typename Ty , class = std::enable_if_t<std::is_arithmetic<Ty>::value>>
constexpr Ty units::unit_t< Units, T, NonLinearScale >::to ( ) const
inlineconstexprnoexcept

unit value

Returns
value of the unit converted to an arithmetic, non-safe type.

◆ toLinearized()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
template<typename Ty , class = std::enable_if_t<std::is_arithmetic<Ty>::value>>
constexpr Ty units::unit_t< Units, T, NonLinearScale >::toLinearized ( ) const
inlineconstexprnoexcept

linearized unit value

Returns
linearized value of unit which has a non-linear scale. For unit_t types with linear scales, this is equivalent to value.

◆ value()

template<class Units , typename T = double, template< typename > class NonLinearScale = linear_scale>
constexpr underlying_type units::unit_t< Units, T, NonLinearScale >::value ( ) const
inlineconstexprnoexcept

unit value

Returns
value of the unit in it's underlying, non-safe type.

The documentation for this class was generated from the following file: