LibSWOC++ 1.5.14
Solid Wall of C++
Loading...
Searching...
No Matches
swoc::Scalar< N, C, T > Class Template Reference

#include <Scalar.h>

Collaboration diagram for swoc::Scalar< N, C, T >:
Collaboration graph

Public Types

using Counter = C
 Type used to hold the count.
 
using Tag = T
 Tag for the scalar.
 

Public Member Functions

constexpr Scalar ()
 Default constructor.
 
constexpr Scalar (Counter n)
 Construct to have value that is n scaled units.
 
constexpr Scalar (self_type const &that)
 Copy constructor.
 
template<typename I>
constexpr Scalar (Scalar< N, I, T > const &that)
 Copy constructor.
 
template<intmax_t S, typename I>
constexpr Scalar (Scalar< S, I, T > const &that)
 
template<intmax_t S, typename I>
self_typeoperator= (Scalar< S, I, T > const &that)
 
self_typeoperator= (self_type const &that)
 Self type assignment.
 
self_typeassign (Counter n)
 
template<intmax_t S, typename I>
self_typeassign (Scalar< S, I, T > const &that)
 
constexpr Counter count () const
 The number of scale units.
 
constexpr Counter value () const
 The scaled value.
 
constexpr operator Counter () const
 User conversion to scaled value.
 
self_typeoperator+= (self_type const &that)
 
template<intmax_t S, typename I>
self_typeoperator+= (Scalar< S, I, T > const &that)
 
self_typeoperator++ ()
 Increment - increase count by 1.
 
self_type operator++ (int)
 Increment - increase count by 1.
 
self_typeoperator-- ()
 Decrement - decrease count by 1.
 
self_type operator-- (int)
 Decrement - decrease count by 1.
 
self_typeinc (Counter n)
 Increment by n.
 
self_typedec (Counter n)
 Decrement by n.
 
self_typeoperator-= (self_type const &that)
 
template<intmax_t S, typename I>
self_typeoperator-= (Scalar< S, I, T > const &that)
 
self_typeoperator*= (C n)
 Multiplication - multiple the count by n.
 
self_typeoperator/= (C n)
 Division - divide (rounding down) the count by n.
 
self_type operator() (Counter n) const
 Utility overload of the function operator to create instances at the same scale.
 
self_type plus (Counter n) const
 Return a value at the same scale with a count increased by n.
 
self_type minus (Counter n) const
 Return a value at the same scale with a count decreased by n.
 
template<intmax_t S, typename I>
auto operator+= (Scalar< S, I, T > const &that) -> self_type &
 
template<intmax_t S, typename I>
auto operator-= (Scalar< S, I, T > const &that) -> self_type &
 

Static Public Member Functions

static constexpr intmax_t scale ()
 Run time access to the scale (template arg N).
 

Static Public Attributes

static constexpr intmax_t SCALE = N
 Scaling factor - make it external accessible.
 

Protected Attributes

Counter _n
 Number of scale units.
 

Detailed Description

template<intmax_t N, typename C = int, typename T = tag::generic>
class swoc::Scalar< N, C, T >

A class to hold scaled values.

Instances of this class have a count and a scale. The "value" of the instance is count * scale. The scale is stored in the compiler in the class symbol table and so only the count is a run time value. An instance with a large scale can be assigned to an instance with a smaller scale and the conversion is done automatically. Conversions from a smaller to larger scale must be explicit using round_up and round_down. This prevents inadvertent changes in value. Because the scales are not the same these conversions can be lossy and the two conversions determine whether, in such a case, the result should be rounded up or down to the nearest scale value.

N sets the scale. C is the type used to hold the count, which is in units of N.

T is a "tag" type which is used only to distinguish the base metric for the scale. Scalar types that have different tags are not interoperable although they can be converted manually by converting to units and then explicitly constructing a new Scalar instance. This is by design. This can be ignored - if not specified then it defaults to a "generic" tag. The type can be (and usually is) defined in name only).

Note
This is modeled somewhat on std::chrono and serves a similar function for different and simpler cases (where the ratio is always an integer, never a fraction).
See also
round_up
round_down

Definition at line 172 of file Scalar.h.

Member Typedef Documentation

◆ Counter

template<intmax_t N, typename C = int, typename T = tag::generic>
using swoc::Scalar< N, C, T >::Counter = C

Type used to hold the count.

Definition at line 178 of file Scalar.h.

◆ Tag

template<intmax_t N, typename C = int, typename T = tag::generic>
using swoc::Scalar< N, C, T >::Tag = T

Tag for the scalar.

Definition at line 179 of file Scalar.h.

Constructor & Destructor Documentation

◆ Scalar() [1/2]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<typename I>
swoc::Scalar< N, C, T >::Scalar ( Scalar< N, I, T > const & that)
constexpr

Copy constructor.

Copy constructor for same scale.

◆ Scalar() [2/2]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
swoc::Scalar< N, C, T >::Scalar ( Scalar< S, I, T > const & that)
constexpr

Direct conversion constructor.

Note
Requires that S be an integer multiple of SCALE.

Member Function Documentation

◆ assign() [1/2]

template<intmax_t N, typename C = int, typename T = tag::generic>
self_type & swoc::Scalar< N, C, T >::assign ( Counter n)

Set the scaled count to n.

Parameters
nThe number of scale units.
Returns
this

This sets the count to be n, making the effective value SCALE*n. This is frequently useful for parsing, when the input is also in scaled units (e.g., the number of megabytes to use for a file).

◆ assign() [2/2]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
self_type & swoc::Scalar< N, C, T >::assign ( Scalar< S, I, T > const & that)

The value that is scaled appropriately.

Note
Requires the scale of that be an integer multiple of the scale of this. If this isn't the case then the round_up or round_down must be used to indicate the rounding direction.

◆ dec()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::dec ( Counter n)

Decrement by n.

Definition at line 913 of file Scalar.h.

◆ inc()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::inc ( Counter n)

Increment by n.

Definition at line 906 of file Scalar.h.

◆ minus()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::minus ( Counter n) const

Return a value at the same scale with a count decreased by n.

Definition at line 996 of file Scalar.h.

◆ operator()()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator() ( Counter n) const

Utility overload of the function operator to create instances at the same scale.

Definition at line 984 of file Scalar.h.

◆ operator*=()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator*= ( C n)

Multiplication - multiple the count by n.

Definition at line 920 of file Scalar.h.

◆ operator++() [1/2]

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator++ ( )

Increment - increase count by 1.

Definition at line 876 of file Scalar.h.

◆ operator++() [2/2]

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator++ ( int )

Increment - increase count by 1.

Definition at line 883 of file Scalar.h.

◆ operator+=() [1/3]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
self_type & swoc::Scalar< N, C, T >::operator+= ( Scalar< S, I, T > const & that)

Cross scale addition.

Template Parameters
SSource scale.
ISource raw type.
Parameters
thatValue.
Returns
this

that is scaled as needed to be added to this.

◆ operator+=() [2/3]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
auto swoc::Scalar< N, C, T >::operator+= ( Scalar< S, I, T > const & that) -> self_type &

Definition at line 651 of file Scalar.h.

◆ operator+=() [3/3]

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator+= ( self_type const & that)

Addition operator. The value is scaled from that to this.

Note
Requires the scale of that be an integer multiple of the scale of this. If this isn't the case then the scale_up or scale_down casts must be used to indicate the rounding direction.

Definition at line 643 of file Scalar.h.

◆ operator--() [1/2]

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator-- ( )

Decrement - decrease count by 1.

Definition at line 891 of file Scalar.h.

◆ operator--() [2/2]

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator-- ( int )

Decrement - decrease count by 1.

Definition at line 898 of file Scalar.h.

◆ operator-=() [1/3]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
self_type & swoc::Scalar< N, C, T >::operator-= ( Scalar< S, I, T > const & that)

Subtraction.

Template Parameters
SScale.
IRaw type.
Parameters
thatValue to subtract.
Returns
this

The value of that is subtracted from the value of this.

◆ operator-=() [2/3]

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
auto swoc::Scalar< N, C, T >::operator-= ( Scalar< S, I, T > const & that) -> self_type &

Definition at line 767 of file Scalar.h.

◆ operator-=() [3/3]

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator-= ( self_type const & that)

Subtraction operator. The value is scaled from that to this.

Note
Requires the scale of that be an integer multiple of the scale of this. If this isn't the case then the scale_up or scale_down casts must be used to indicate the rounding direction.

Definition at line 759 of file Scalar.h.

◆ operator/=()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::operator/= ( C n)

Division - divide (rounding down) the count by n.

Definition at line 963 of file Scalar.h.

◆ operator=()

template<intmax_t N, typename C = int, typename T = tag::generic>
template<intmax_t S, typename I>
self_type & swoc::Scalar< N, C, T >::operator= ( Scalar< S, I, T > const & that)

Assign value from that.

Template Parameters
SScale.
IIntegral type.
Parameters
thatSource value.
Returns
this.
Note
Requires the scale of that be an integer multiple of the scale of this. If this isn't the case then the round_up or round_down must be used to indicate the rounding direction.

◆ plus()

template<intmax_t N, typename C, typename T>
auto swoc::Scalar< N, C, T >::plus ( Counter n) const

Return a value at the same scale with a count increased by n.

Definition at line 990 of file Scalar.h.

Member Data Documentation

◆ _n

template<intmax_t N, typename C = int, typename T = tag::generic>
Counter swoc::Scalar< N, C, T >::_n
protected

Number of scale units.

Definition at line 395 of file Scalar.h.

◆ SCALE

template<intmax_t N, typename C = int, typename T = tag::generic>
intmax_t swoc::Scalar< N, C, T >::SCALE = N
staticconstexpr

Scaling factor - make it external accessible.

Definition at line 177 of file Scalar.h.


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