Public Types | Public Member Functions | Static Public Member Functions | List of all members
util::quantities::concepts::Interval< Q, Cat > Struct Template Reference

An interval (duration, length, distance) between two quantity points. More...

#include <intervals.h>

Inheritance diagram for util::quantities::concepts::Interval< Q, Cat >:
util::quantities::concepts::details::WithCategory< Cat >

Public Types

using category_base_t = details::WithCategory< Cat >
 
using traits_t = typename category_base_t::traits_t
 Traits of the category. More...
 
template<typename OC , typename Type = void>
using enable_if_compatible_t = std::enable_if_t< category_base_t::template category_compatible_with< OC >(), Type >
 
using interval_t = Interval< Q, Cat >
 This type. More...
 
Types from the base quantity
using quantity_t = Q
 
using category_t = typename category_base_t::category_t
 Quantity the interval is based on. More...
 
template<typename R >
using scaled_quantity_t = rescale< quantity_t, R >
 A quantity in the same unit, but possibly a different scale. More...
 
using value_t = typename quantity_t::value_t
 Type of the stored value. More...
 
using unit_t = typename quantity_t::unit_t
 Description of the scaled unit. More...
 
using baseunit_t = typename quantity_t::baseunit_t
 Description of the unscaled unit. More...
 
template<typename OQ , typename OI >
using other_interval_t = Interval< OQ, category_t >
 An interval based on a different quantity but with the same category. More...
 
- Public Types inherited from util::quantities::concepts::details::WithCategory< Cat >
using category_t = Cat
 The category of this object. More...
 
using traits_t = category_traits< category_t >
 Traits of this category. More...
 

Public Member Functions

 Interval ()=default
 Constructor: value is left uninitialized. More...
 
constexpr Interval (value_t v)
 Constructor: takes a value in the intended representation. More...
 
template<typename... Args>
constexpr Interval (Quantity< Args... > const &q)
 Constructor: converts from a quantity. More...
 
template<typename IV , typename std::enable_if_t< is_interval_v< IV >> * = nullptr>
constexpr Interval (IV iv)
 Constructor: converts from another interval. More...
 
constexpr quantity_t const & quantity () const
 Returns the value of the interval as a quantity. More...
 
constexpr operator value_t () const
 Conversion to the base quantity. More...
 
template<typename IV >
constexpr IV convertInto () const
 Convert this interval into the specified one. More...
 
Asymmetric operand arithmetic operations

These arithmetic operations take care of preserving the interval unit through them. Not all possible (or reasonable) operations are supported yet. Some operations that may be symmetric (like multiplication by a scalar) are implemented as free functions rather than methods.

Note
These operations are essentially provided by convenience. There are many cases (corner and not) where the implicit conversion to the base type kicks in. This implementation does not aim to prevent that from happening, but requests are welcome to add features to allow that not to happen, with some care of the user.
The addition and subtraction of intervals is conceptually symmetric. This leaves open the question whether the result of a + b should be of the type of a or the one of b, and whether having b + a of a different type than a + b is acceptable. Here we decide it's not, and therefore the operation a + b (and a - b as well) are supported only if a and b have the same type. On the other end, if either a or b are not an interval but rather a simple quantity, it is clear that the result must still be an interval and of the same type as the interval operand. We are in this case allowed to accept quantities of any unit scale.
constexpr interval_t operator+ (interval_t const other) const
 
constexpr interval_t operator- (interval_t const other) const
 
template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, value_toperator/ (Interval< OQ, OC > const denom) const
 Division by an interval, returns a pure number. More...
 
template<typename R >
interval_toperator+= (scaled_quantity_t< R > const other)
 Add a quantity (possibly converted) to this one. More...
 
template<typename OQ , typename OC >
enable_if_compatible_t< Interval< OQ, OC >, interval_t & > operator+= (Interval< OQ, OC > const other)
 Add the other interval (possibly converted) to this one. More...
 
template<typename R >
interval_toperator-= (scaled_quantity_t< R > const other)
 Subtract a quantity (possibly converted) from this one. More...
 
template<typename OQ , typename OC >
enable_if_compatible_t< Interval< OQ, OC >, interval_t & > operator-= (Interval< OQ, OC > const other)
 Subtract the other interval (possibly converted) from this one. More...
 
template<typename T >
interval_toperator*= (T factor)
 Scale this interval by a factor. More...
 
template<typename T >
interval_toperator/= (T factor)
 Scale the interval dividing it by a quotient. More...
 
constexpr interval_t operator+ () const
 Returns an interval with same value. More...
 
constexpr interval_t operator- () const
 Returns an interval with same value but the sign flipped. More...
 
constexpr interval_t abs () const
 Returns an interval with the absolute value of this one. More...
 
Comparisons.

Comparisons with plain numbers are managed by implicit conversion. More care is needed for quantities. Comparisons between two quantity instances a and b work this way:

  • if a and b do not have the same unit, they are not comparable
  • if a and b have the same unit, one is converted to the other and the comparison is performed there
  • if a and b have the same scaled unit, their values are compared directly

Value storage types are compared according to C++ rules.

template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, booloperator== (Interval< OQ, OC > const other) const
 
template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, booloperator!= (Interval< OQ, OC > const other) const
 
template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, booloperator>= (Interval< OQ, OC > const other) const
 
template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, booloperator> (Interval< OQ, OC > const other) const
 
template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, booloperator<= (Interval< OQ, OC > const other) const
 
template<typename OQ , typename OC >
constexpr enable_if_compatible_t< Interval< OQ, OC >, booloperator< (Interval< OQ, OC > const other) const
 

Static Public Member Functions

template<typename U >
static interval_t castFrom (U value)
 Returns a new interval initialized with the specified value. More...
 
Access to the scaled unit.
template<typename OU >
static constexpr bool sameBaseUnitAs ()
 Returns whether objects of type OU have the same base unit as this. More...
 
template<typename OU >
static constexpr bool sameUnitAs ()
 Returns whether objects of type OU have same unit and scale as this. More...
 
- Static Public Member Functions inherited from util::quantities::concepts::details::WithCategory< Cat >
static constexpr category_t category ()
 Returns an instance of the category of this object. More...
 
static constexpr bool hasCategoryName ()
 Returns whether this category has a name. More...
 
static std::string categoryName ()
 Returns the name of the category of this object. More...
 
template<typename OC >
static constexpr bool same_category_as ()
 Returns whether the type OC belongs to category_t. More...
 
template<typename OC >
static constexpr bool same_category_as (OC const &)
 
template<typename OC >
static constexpr bool category_compatible_with ()
 Returns whether OC has a category compatible with this one. More...
 
template<typename OC >
static constexpr bool category_compatible_with (OC const &)
 

Detailed Description

template<typename Q, typename Cat = NoCategory>
struct util::quantities::concepts::Interval< Q, Cat >

An interval (duration, length, distance) between two quantity points.


Template Parameters
Qquantity the interval is based on

An interval shares most of the concepts of a Quantity, but it interacts only with other intervals rather than with bare Quantity objects, with the exception of construction. In this sense, the relation between Interval and Quantity is similar to the one between Quantity and its base type (Quantity::value_t).

In addition, an interval can be added to a quantity point (Point) to translate it, and it can be obtained as difference between two quantity points.

Definition at line 114 of file intervals.h.

Member Typedef Documentation

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::baseunit_t = typename quantity_t::baseunit_t

Description of the unscaled unit.

Definition at line 152 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::category_base_t = details::WithCategory<Cat>

Definition at line 116 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::category_t = typename category_base_t::category_t

Quantity the interval is based on.

The category this point belongs to.

Definition at line 139 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
template<typename OC , typename Type = void>
using util::quantities::concepts::Interval< Q, Cat >::enable_if_compatible_t = std::enable_if_t <category_base_t::template category_compatible_with<OC>(), Type>

Definition at line 125 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::interval_t = Interval<Q, Cat>

This type.

Definition at line 129 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OI >
using util::quantities::concepts::Interval< Q, Cat >::other_interval_t = Interval<OQ, category_t>

An interval based on a different quantity but with the same category.

Definition at line 156 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::quantity_t = Q

Definition at line 136 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
template<typename R >
using util::quantities::concepts::Interval< Q, Cat >::scaled_quantity_t = rescale<quantity_t, R>

A quantity in the same unit, but possibly a different scale.

Definition at line 143 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::traits_t = typename category_base_t::traits_t

Traits of the category.

Definition at line 119 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::unit_t = typename quantity_t::unit_t

Description of the scaled unit.

Definition at line 149 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
using util::quantities::concepts::Interval< Q, Cat >::value_t = typename quantity_t::value_t

Type of the stored value.

Definition at line 146 of file intervals.h.

Constructor & Destructor Documentation

template<typename Q, typename Cat = NoCategory>
util::quantities::concepts::Interval< Q, Cat >::Interval ( )
explicitdefault

Constructor: value is left uninitialized.

template<typename Q, typename Cat = NoCategory>
constexpr util::quantities::concepts::Interval< Q, Cat >::Interval ( value_t  v)
inlineexplicit

Constructor: takes a value in the intended representation.

Definition at line 167 of file intervals.h.

167 : Interval(quantity_t{ v }) {}
Interval()=default
Constructor: value is left uninitialized.
template<typename Q, typename Cat = NoCategory>
template<typename... Args>
constexpr util::quantities::concepts::Interval< Q, Cat >::Interval ( Quantity< Args... > const &  q)
inline

Constructor: converts from a quantity.

Template Parameters
OQtype of the quantity
Parameters
qquantity to be converted from

The quantity is required to be in the same unit as this interval (unit scale may differ). The value in q is converted from its native scale into the one of this interval.

Definition at line 180 of file intervals.h.

template<typename Q, typename Cat = NoCategory>
template<typename IV , typename std::enable_if_t< is_interval_v< IV >> * = nullptr>
constexpr util::quantities::concepts::Interval< Q, Cat >::Interval ( IV  iv)
inline

Constructor: converts from another interval.

Template Parameters
Itype of the other interval
Parameters
ivinterval to be converted from

Intervals are required to be in the same unit (unit scale may differ). The value in iv is converted from its native scale into the one of this interval.

Definition at line 195 of file intervals.h.

195 : Interval(quantity_t { iv.quantity() }) {}
Interval()=default
Constructor: value is left uninitialized.

Member Function Documentation

template<typename Q, typename Cat = NoCategory>
constexpr interval_t util::quantities::concepts::Interval< Q, Cat >::abs ( ) const
inline

Returns an interval with the absolute value of this one.

Definition at line 329 of file intervals.h.

329 { return interval_t(quantity().abs()); }
Interval< Q, Cat > interval_t
This type.
Definition: intervals.h:129
constexpr quantity_t const & quantity() const
Returns the value of the interval as a quantity.
Definition: intervals.h:198
constexpr interval_t abs() const
Returns an interval with the absolute value of this one.
Definition: intervals.h:329
template<typename Q, typename Cat = NoCategory>
template<typename U >
static interval_t util::quantities::concepts::Interval< Q, Cat >::castFrom ( value)
inlinestatic

Returns a new interval initialized with the specified value.

Template Parameters
Utype to initialize the quantity with
Parameters
valuethe value to initialize the interval with
Returns
a new Interval object initialized with value

The value is cast into value_t via static_cast().

Definition at line 400 of file intervals.h.

401  { return interval_t{ static_cast<value_t>(value) }; }
Interval< Q, Cat > interval_t
This type.
Definition: intervals.h:129
typename quantity_t::value_t value_t
Type of the stored value.
Definition: intervals.h:146
template<typename Q, typename Cat = NoCategory>
template<typename IV >
constexpr IV util::quantities::concepts::Interval< Q, Cat >::convertInto ( ) const
inline

Convert this interval into the specified one.

Definition at line 389 of file intervals.h.

389 { return IV(*this); }
template<typename Q, typename Cat = NoCategory>
constexpr util::quantities::concepts::Interval< Q, Cat >::operator value_t ( ) const
inlineexplicit

Conversion to the base quantity.

Definition at line 204 of file intervals.h.

205  { return value(); }
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, bool> util::quantities::concepts::Interval< Q, Cat >::operator!= ( Interval< OQ, OC > const  other) const
inline

Definition at line 360 of file intervals.h.

361  { return quantity_t::operator!=(other.quantity()); }
constexpr bool operator!=(Quantity< Args... > const a, Point< Q, Cat, IV > const b) noexcept
Definition: intervals.h:869
template<typename Q, typename Cat = NoCategory>
template<typename T >
interval_t& util::quantities::concepts::Interval< Q, Cat >::operator*= ( factor)
inline

Scale this interval by a factor.

Definition at line 314 of file intervals.h.

315  { quantity_t::operator*= (factor); return *this; }
template<typename Q, typename Cat = NoCategory>
constexpr interval_t util::quantities::concepts::Interval< Q, Cat >::operator+ ( interval_t const  other) const
inline

Returns an interval sum of this and other (must have exactly the same unit and scale).

Definition at line 276 of file intervals.h.

277  { return interval_t{ quantity() + other.quantity() }; }
Interval< Q, Cat > interval_t
This type.
Definition: intervals.h:129
constexpr quantity_t const & quantity() const
Returns the value of the interval as a quantity.
Definition: intervals.h:198
template<typename Q, typename Cat = NoCategory>
constexpr interval_t util::quantities::concepts::Interval< Q, Cat >::operator+ ( ) const
inline

Returns an interval with same value.

Definition at line 323 of file intervals.h.

323 { return interval_t(quantity()); }
Interval< Q, Cat > interval_t
This type.
Definition: intervals.h:129
constexpr quantity_t const & quantity() const
Returns the value of the interval as a quantity.
Definition: intervals.h:198
template<typename Q, typename Cat = NoCategory>
template<typename R >
interval_t& util::quantities::concepts::Interval< Q, Cat >::operator+= ( scaled_quantity_t< R > const  other)
inline

Add a quantity (possibly converted) to this one.

Definition at line 292 of file intervals.h.

293  { quantity_t::operator+= (other); return *this; }
DoubleProduct & operator+=(DoubleProduct &left, DoubleProduct const &right)
Definition: ToyProducts.h:103
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
enable_if_compatible_t<Interval<OQ, OC>, interval_t&> util::quantities::concepts::Interval< Q, Cat >::operator+= ( Interval< OQ, OC > const  other)
inline

Add the other interval (possibly converted) to this one.

Definition at line 298 of file intervals.h.

299  { return operator+= (other.quantity()); }
interval_t & operator+=(scaled_quantity_t< R > const other)
Add a quantity (possibly converted) to this one.
Definition: intervals.h:292
template<typename Q, typename Cat = NoCategory>
constexpr interval_t util::quantities::concepts::Interval< Q, Cat >::operator- ( interval_t const  other) const
inline

Returns an interval difference of this and other (must have exactly the same unit and scale).

Definition at line 281 of file intervals.h.

282  { return interval_t{ quantity() - other.quantity() }; }
Interval< Q, Cat > interval_t
This type.
Definition: intervals.h:129
constexpr quantity_t const & quantity() const
Returns the value of the interval as a quantity.
Definition: intervals.h:198
template<typename Q, typename Cat = NoCategory>
constexpr interval_t util::quantities::concepts::Interval< Q, Cat >::operator- ( ) const
inline

Returns an interval with same value but the sign flipped.

Definition at line 326 of file intervals.h.

326 { return interval_t(-quantity()); }
Interval< Q, Cat > interval_t
This type.
Definition: intervals.h:129
constexpr quantity_t const & quantity() const
Returns the value of the interval as a quantity.
Definition: intervals.h:198
template<typename Q, typename Cat = NoCategory>
template<typename R >
interval_t& util::quantities::concepts::Interval< Q, Cat >::operator-= ( scaled_quantity_t< R > const  other)
inline

Subtract a quantity (possibly converted) from this one.

Definition at line 303 of file intervals.h.

304  { quantity_t::operator-= (other); return *this; }
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
enable_if_compatible_t<Interval<OQ, OC>, interval_t&> util::quantities::concepts::Interval< Q, Cat >::operator-= ( Interval< OQ, OC > const  other)
inline

Subtract the other interval (possibly converted) from this one.

Definition at line 309 of file intervals.h.

310  { return operator-= (other.quantity()); }
interval_t & operator-=(scaled_quantity_t< R > const other)
Subtract a quantity (possibly converted) from this one.
Definition: intervals.h:303
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, value_t> util::quantities::concepts::Interval< Q, Cat >::operator/ ( Interval< OQ, OC > const  denom) const
inline

Division by an interval, returns a pure number.

Definition at line 287 of file intervals.h.

288  { return quantity() / denom.quantity(); }
constexpr quantity_t const & quantity() const
Returns the value of the interval as a quantity.
Definition: intervals.h:198
template<typename Q, typename Cat = NoCategory>
template<typename T >
interval_t& util::quantities::concepts::Interval< Q, Cat >::operator/= ( factor)
inline

Scale the interval dividing it by a quotient.

Definition at line 319 of file intervals.h.

320  { quantity_t::operator/= (factor); return *this; }
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, bool> util::quantities::concepts::Interval< Q, Cat >::operator< ( Interval< OQ, OC > const  other) const
inline

Definition at line 380 of file intervals.h.

381  { return quantity_t::operator<(other.quantity()); }
constexpr bool operator<(Quantity< Args... > const a, Point< Q, Cat, IV > const b) noexcept
Definition: intervals.h:889
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, bool> util::quantities::concepts::Interval< Q, Cat >::operator<= ( Interval< OQ, OC > const  other) const
inline

Definition at line 375 of file intervals.h.

376  { return quantity_t::operator<=(other.quantity()); }
constexpr bool operator<=(Quantity< Args... > const a, Point< Q, Cat, IV > const b) noexcept
Definition: intervals.h:879
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, bool> util::quantities::concepts::Interval< Q, Cat >::operator== ( Interval< OQ, OC > const  other) const
inline

Definition at line 355 of file intervals.h.

356  { return quantity_t::operator==(other.quantity()); }
constexpr bool operator==(Quantity< Args... > const a, Point< Q, Cat, IV > const b) noexcept
Definition: intervals.h:859
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, bool> util::quantities::concepts::Interval< Q, Cat >::operator> ( Interval< OQ, OC > const  other) const
inline

Definition at line 370 of file intervals.h.

371  { return quantity_t::operator>(other.quantity()); }
constexpr bool operator>(Quantity< Args... > const a, Point< Q, Cat, IV > const b) noexcept
Definition: intervals.h:909
template<typename Q, typename Cat = NoCategory>
template<typename OQ , typename OC >
constexpr enable_if_compatible_t<Interval<OQ, OC>, bool> util::quantities::concepts::Interval< Q, Cat >::operator>= ( Interval< OQ, OC > const  other) const
inline

Definition at line 365 of file intervals.h.

366  { return quantity_t::operator>=(other.quantity()); }
constexpr bool operator>=(Quantity< Args... > const a, Point< Q, Cat, IV > const b) noexcept
Definition: intervals.h:899
template<typename Q, typename Cat = NoCategory>
constexpr quantity_t const& util::quantities::concepts::Interval< Q, Cat >::quantity ( ) const
inline

Returns the value of the interval as a quantity.

Definition at line 198 of file intervals.h.

198 { return *this; }
template<typename Q, typename Cat = NoCategory>
template<typename OU >
static constexpr bool util::quantities::concepts::Interval< Q, Cat >::sameBaseUnitAs ( )
inlinestatic

Returns whether objects of type OU have the same base unit as this.

Definition at line 229 of file intervals.h.

230  { return quantity_t::template sameBaseUnitAs<OU>(); }
template<typename Q, typename Cat = NoCategory>
template<typename OU >
static constexpr bool util::quantities::concepts::Interval< Q, Cat >::sameUnitAs ( )
inlinestatic

Returns whether objects of type OU have same unit and scale as this.

Definition at line 234 of file intervals.h.

235  { return quantity_t::template sameUnitAs<OU>(); }

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