iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::Range< T > Class Template Reference

#include <nrt/Core/Typing/Range.H>

template<class T>
class nrt::Range< T >

A generic range class.

The Range class is used to represent a range from [min .. max]

Note
The operators of Range do not use type promotions (see Automatic type promotion for operations). This is to minimize template burden and also possible programmer confusion. For example, while in principle one could define Range<int> + float and return a Range<float>, here we do not define such an operator. The same result can be achieved by first converting the range and then adding the constant:
nrt::Range<int> r1(0, 100);
// NOT ALLOWED: nrt::Range<float> r2 = r1 + 1.23F;
nrt::Range<float> r2 = nrt::Range<float>(r1) + 1.23F; // OK, no hidden conversions, easy to read, explicit, nice
Operators on Range use range checking and clamping internally. Thus, be careful if using Range<byte>. For example:
nrt::Range<nrt::byte> r(nrt::byte(0), nrt::byte(250));
r += nrt::byte(100); // new range is 100-255
Examples:
tests/test-Component.C.

Definition at line 66 of file Range.H.

Public Member Functions

 Range ()
 Default constructor, range is [0 .. 0].
 
 Range (T const mini, T const maxi)
 Constructor.
 
 Range (Range< T > const &other)=default
 Copy constructor.
 
 Range (Range< T > &&other)=default
 Move constructor.
 
template<class U >
 Range (Range< U > const &other)
 Copy-conversion constructor, uses nrt::clamped_convert<T,U> internally. More...
 
Range< T > & operator= (Range< T > const &other)=default
 Assignment.
 
Range< T > & operator= (Range< T > &&other)=default
 Move assignment.
 
T const & min () const
 Return the minimum value.
 
T const & max () const
 Return the maximum value.
 
bool const empty () const
 Return whether min() == max()
 
void extend (T const val)
 Extend the range, if needed, so that it includes val.
 

Friends

class cereal::access
 

Related Functions

(Note that these are not member functions.)

template<class T >
Range< T > merge (Range< T > const &r1, Range< T > const &r2)
 Merge two ranges. More...
 
template<class T >
std::ostream & operator<< (std::ostream &out, Range< T > const &r)
 Stream out as "min-max". More...
 
template<class T >
std::istream & operator>> (std::istream &in, Range< T > &r)
 Stream in as "min-max". More...
 
template<class T >
void paramValToString (Range< T > const &val, std::string &result)
 Machine-readable output to a string, for use in nrt::Parameter: outputs min-max (e.g., 0-100) More...
 
template<class T >
void paramStringToVal (std::string const &valstring, Range< T > &result)
 Machine-readable input from a string, for use in nrt::Parameter: reads min-max (e.g., 0-100) More...
 
template<class T >
bool operator== (Range< T > const &range1, Range< T > const &range2)
 Equality test: Range<T> == Range<T> More...
 
template<class T >
bool operator!= (Range< T > const &range1, Range< T > const &range2)
 Inequality test: Range<T> != Range<T> More...
 
template<class T >
Range< T > operator+ (Range< T > const &range, T const &scalar)
 Add constant to both ends of a range: Range<T> + T. More...
 
template<class T >
Range< T > operator+ (T const &scalar, Range< T > const &range)
 Add constant to both ends of a range: T + Range<T> More...
 
template<class T >
Range< T > operator- (Range< T > const &range, T const &scalar)
 Subtract constant from both ends of a range: Range<T> - T. More...
 
template<class T >
Range< T > operator- (T const &scalar, Range< T > const &range)
 Subtract constant from both ends of a range: T - Range<T> More...
 
template<class T >
Range< T > operator/ (Range< T > const &range, T const &scalar)
 Divide both ends of a range by a factor: Range<T> / T. More...
 
template<class T >
Range< T > operator/ (T const &scalar, Range< T > const &range)
 Divide a factor by both ends of a range: T / Range<T> More...
 
template<class T >
Range< T > operator* (Range< T > const &range, T const &scalar)
 Multiply both ends of a range by a factor: Range<T> * T. More...
 
template<class T >
Range< T > operator* (T const &scalar, Range< T > const &range)
 Multiply a factor by both ends of a range: T * Range<T> More...
 
template<class T >
Range< T > & operator+= (Range< T > &range, T const &scalar)
 Add constant to both ends of a range: Range<T> += T. More...
 
template<class T >
Range< T > & operator-= (Range< T > &range, T const &scalar)
 Subtract constant from both ends of a range: Range<T> -= T. More...
 
template<class T >
Range< T > & operator*= (Range< T > &range, T const &scalar)
 Multiply both ends of a range by a factor: Range<T> *= T. More...
 
template<class T >
Range< T > & operator/= (Range< T > &range, T const &scalar)
 Divide both ends of a range by a factor: Range<T> /= T. More...
 

Constructor & Destructor Documentation

template<class T >
template<class U >
nrt::Range< T >::Range ( nrt::Range< U > const &  other)
inlineexplicit

Copy-conversion constructor, uses nrt::clamped_convert<T,U> internally.

Note that this constructor is explicit, i.e., you need to explicitly mention it. This is to avoid conversions to happen without a programmer being aware of it. For example:

void myFunc(nrt::Range<float> const & rng) { ... }
nrt::Range<int> r1(0, 100);
nrt::Range<float> r2(r1); // ok, explicit constructor call
myFunc(r2); // ok, no conversion necessary
//myFunc(r1) // NO: passing Range<int> to function that takes a Range<float> arg fails to compile
myFunc(nrt::Range<float>(r1)); // ok, nice and explicit; and you can thing about whether r1 should
// have been a Range<float> in the first place so you don't waste CPU
// doing this conversion (as opposed to just not being aware of the wasting)

Definition at line 60 of file RangeImpl.H.

Friends And Related Function Documentation

template<class T >
Range< T > merge ( Range< T > const &  r1,
Range< T > const &  r2 
)
related

Merge two ranges.

template<class T >
std::ostream & operator<< ( std::ostream &  out,
Range< T > const &  r 
)
related

Stream out as "min-max".

template<class T >
std::istream & operator>> ( std::istream &  in,
Range< T > &  r 
)
related

Stream in as "min-max".

template<class T >
void paramValToString ( Range< T > const &  val,
std::string &  result 
)
related

Machine-readable output to a string, for use in nrt::Parameter: outputs min-max (e.g., 0-100)

This is an overload for Range<T> of the function template in nrt/Core/Model/ParameterStringConversion.H.

template<class T >
void paramStringToVal ( std::string const &  valstring,
Range< T > &  result 
)
related

Machine-readable input from a string, for use in nrt::Parameter: reads min-max (e.g., 0-100)

Exceptions
nrt::exception::Exceptionif input is malformed. This is an overload for Range<T> of the function template in nrt/Core/Model/ParameterStringConversion.H.
template<class T >
bool operator== ( Range< T > const &  range1,
Range< T > const &  range2 
)
related

Equality test: Range<T> == Range<T>

template<class T >
bool operator!= ( Range< T > const &  range1,
Range< T > const &  range2 
)
related

Inequality test: Range<T> != Range<T>

template<class T >
Range< T > operator+ ( Range< T > const &  range,
T const &  scalar 
)
related

Add constant to both ends of a range: Range<T> + T.

template<class T >
Range< T > operator+ ( T const &  scalar,
Range< T > const &  range 
)
related

Add constant to both ends of a range: T + Range<T>

template<class T >
Range< T > operator- ( Range< T > const &  range,
T const &  scalar 
)
related

Subtract constant from both ends of a range: Range<T> - T.

template<class T >
Range< T > operator- ( T const &  scalar,
Range< T > const &  range 
)
related

Subtract constant from both ends of a range: T - Range<T>

template<class T >
Range< T > operator/ ( Range< T > const &  range,
T const &  scalar 
)
related

Divide both ends of a range by a factor: Range<T> / T.

template<class T >
Range< T > operator/ ( T const &  scalar,
Range< T > const &  range 
)
related

Divide a factor by both ends of a range: T / Range<T>

template<class T >
Range< T > operator* ( Range< T > const &  range,
T const &  scalar 
)
related

Multiply both ends of a range by a factor: Range<T> * T.

template<class T >
Range< T > operator* ( T const &  scalar,
Range< T > const &  range 
)
related

Multiply a factor by both ends of a range: T * Range<T>

template<class T >
Range< T > & operator+= ( Range< T > &  range,
T const &  scalar 
)
related

Add constant to both ends of a range: Range<T> += T.

template<class T >
Range< T > & operator-= ( Range< T > &  range,
T const &  scalar 
)
related

Subtract constant from both ends of a range: Range<T> -= T.

template<class T >
Range< T > & operator*= ( Range< T > &  range,
T const &  scalar 
)
related

Multiply both ends of a range by a factor: Range<T> *= T.

template<class T >
Range< T > & operator/= ( Range< T > &  range,
T const &  scalar 
)
related

Divide both ends of a range by a factor: Range<T> /= T.


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