SimTime Class Reference

int64-based, fixed-point simulation time. More...

#include <simtime.h>

List of all members.

Public Member Functions

double dbl () const
int64 inUnit (int exponent) const
SimTime trunc (int exponent) const
SimTime remainderForUnit (int exponent) const
void split (int exponent, int64 &outValue, SimTime &outRemainder) const
std::string str () const
char * str (char *buf) const
int64 raw () const
const SimTimesetRaw (int64 l)
Constructors

 SimTime ()
 SimTime (double d)
 SimTime (cPar &d)
 SimTime (int64 significand, int exponent)
 SimTime (const SimTime &x)

Static Public Member Functions

static const SimTime getMaxTime ()
static int64 getScale ()
static int getScaleExp ()
static void setScaleExp (int e)
static const SimTime parse (const char *s)
static const SimTime parse (const char *s, const char *&endp)
static char * ttoa (char *buf, int64 t, int scaleexp, char *&endp)

Arithmetic operations



const SimTimeoperator= (double d)
const SimTimeoperator= (const cPar &d)
const SimTimeoperator= (const SimTime &x)
template<typename T >
const SimTimeoperator= (T d)
const SimTimeoperator+= (const SimTime &x)
const SimTimeoperator-= (const SimTime &x)
const SimTimeoperator*= (double d)
const SimTimeoperator/= (double d)
const SimTimeoperator*= (const cPar &p)
const SimTimeoperator/= (const cPar &p)
template<typename T >
const SimTimeoperator*= (T d)
template<typename T >
const SimTimeoperator/= (T d)
bool operator== (const SimTime &x) const
bool operator!= (const SimTime &x) const
bool operator< (const SimTime &x) const
bool operator> (const SimTime &x) const
bool operator<= (const SimTime &x) const
bool operator>= (const SimTime &x) const
SimTime operator- () const
const SimTime operator+ (const SimTime &x, const SimTime &y)
const SimTime operator- (const SimTime &x, const SimTime &y)
const SimTime operator* (const SimTime &x, double d)
const SimTime operator* (double d, const SimTime &x)
const SimTime operator/ (const SimTime &x, double d)
double operator/ (double d, const SimTime &x)
double operator/ (const SimTime &x, const SimTime &y)
const SimTime operator* (const SimTime &x, const cPar &p)
const SimTime operator* (const cPar &p, const SimTime &x)
const SimTime operator/ (const SimTime &x, const cPar &p)

Detailed Description

int64-based, fixed-point simulation time.

Precision is determined by a scale exponent, which is global (shared by all SimTime instances), and falls in the range -18..0. For example, a scale exponent of -6 means microsecond precision.

Conversions and arithmetic operations are provided specifically for double and cPar, and via template functions for integer types. Performance note: conversion from double uses the scale stored as double, and conversion from integer types uses the scale stored as int64; the former eliminates an int64-to-double conversion, and the latter allows the compiler to optimize expressions like if (time>0) to a trivial integer operation, without floating-point or int64 multiplication.

The underlying raw 64-bit integer is also made accessible.


Constructor & Destructor Documentation

SimTime::SimTime ( double  d  )  [inline]

Initialize simulation time from a double-precision number.

This constructor is recommended if the value is the result of some computation done in double. For integer-based computations and time constants, the SimTime(int64 x, int exponent) constructor is usually a better choice, because it does not have rounding errors caused by double-to-integer conversion.

SimTime::SimTime ( cPar d  )  [inline]

Initialize simulation time from a module or channel parameter.

It uses conversion to double. It currently does not check the measurement unit of the parameter ( property), although this may change in future releases.

SimTime::SimTime ( int64  significand,
int  exponent 
)

Initialize simulation time from a significand and a base-10 exponent.

The purpose of this constructor is to allow one to specify precise constants, i.e. without conversion errors incurred by the double constructor. Use an integer (-3 for millisec, etc) or elements of the SimTimeUnit enum (SIMTIME_S, SIMTIME_MS, etc) for the exponent. An error will be thrown if the resulting value cannot be represented (overflow) or it cannot be represented precisely (precision loss) with the current scale exponent.

Examples: Simtime(152, SIMTIME_MS) -> 0.152s; Simtime(100, -5) -> 0.001s; Simtime(5, 2) -> 500s;


Member Function Documentation

double SimTime::dbl (  )  const [inline]

Converts simulation time to double.

Note that conversion to and from double may lose precision. We do not provide implicit conversion to double as it would conflict with other overloaded operators, and would cause ambiguities during compilation.

Referenced by cWeightedStdDev::collect(), cStdDev::collect(), cStatistic::collect(), cLongHistogram::collect(), cWeightedStdDev::collect2(), exponential(), normal(), cOutVector::record(), cComponent::recordScalar(), cOutVector::recordWithTimestamp(), truncnormal(), and uniform().

static int64 SimTime::getScale (  )  [inline, static]

Returns the time resolution as the number of units per second, e.g.

for microsecond resolution it returns 1000000.

static int SimTime::getScaleExp (  )  [inline, static]

Returns the scale exponent, which is an integer in the range -18..0.

For example, for microsecond resolution it returns -6.

int64 SimTime::inUnit ( int  exponent  )  const

Converts the simulation time to the time unit given with its base-10 exponent, and returns the result as an integer.

If the conversion results in precision loss, the result will be truncated. Use an integer (-3 for millisec, etc.) or elements of the SimTimeUnit enum (SIMTIME_S, SIMTIME_MS, etc) for the exponent. If the return type is not wide enough to hold the result, an error will be thrown.

Examples: 1.7ms in us --> 1700; 3.8ms in s --> 3; -3.8ms in s --> -3; 999ms in s --> 0 261ms in 100ms --> 2;

static const SimTime SimTime::parse ( const char *  s,
const char *&  endp 
) [static]

Converts a prefix of the given string to simulation time, up to the first character that cannot occur in simulation time strings: not (digit or letter or "." or "+" or "-" or whitespace).

Throws an error if there is an error during conversion of the prefix. If the prefix is empty (whitespace only), then 0 is returned, and endp is set equal to s; otherwise, endp is set to point to the first character that was not converted.

static const SimTime SimTime::parse ( const char *  s  )  [static]

Converts the given string to simulation time.

Throws an error if there is an error during conversion. Accepted format is: <number> or (<number><unit>)+.

SimTime SimTime::remainderForUnit ( int  exponent  )  const [inline]

Returns a simtime that is the difference between the simulation time and its truncated value (see trunc()).

That is, t == t.trunc(exp) + t.remainderforUnit(exp) for any exp.

static void SimTime::setScaleExp ( int  e  )  [static]

Sets the scale exponent, and thus the resolution of time.

Accepted values are -18..0; for example, setScaleExp(-6) selects microsecond resolution. Normally, the scale exponent is set from the configuration file (omnetpp.ini) on simulation startup.

The scale exponent can only be set ONCE, and cannot be changed afterwards. Any attempt to change it after it got initialized will result in a runtime error. Reason: the simtime exponent is a global variable, and changing it would silently change the value of existing SimTime variables.

void SimTime::split ( int  exponent,
int64 &  outValue,
SimTime outRemainder 
) const

Convenience method: splits the simulation time into a whole and a fractional part with regard to a time unit, specified with its base-10 exponent.

t.split(exponent, outValue, outRemainder) is equivalent to:

 outValue = t.inUnit(exponent);
 outRemainder = t.remainderForUnit(exponent);
 
char* SimTime::str ( char *  buf  )  const [inline]

Converts to a string.

Use this variant over str() where performance is critical. The result is placed somewhere in the given buffer (pointer is returned), but for performance reasons, not necessarily at the buffer's beginning. Please read the documentation of ttoa() for the minimum required buffer size.

References ttoa().

SimTime SimTime::trunc ( int  exponent  )  const [inline]

Returns a new simulation time that is truncated to the precision of the unit specified by the base-10 exponent parameter.

Use an integer (-3 for millisec, etc.) or elements of the SimTimeUnit enum (SIMTIME_S, SIMTIME_MS, etc) for the exponent.

Examples: 3750ms truncated to s --> 3; -3750ms truncated to s --> -3

static char* SimTime::ttoa ( char *  buf,
int64  t,
int  scaleexp,
char *&  endp 
) [static]

Utility function to convert a 64-bit fixed point number into a string buffer.

scaleexp must be in the -18..0 range, and the buffer must be at least 64 bytes long. A pointer to the result string will be returned. A pointer to the terminating '\0' will be returned in endp.

ATTENTION: For performance reasons, the returned pointer will point *somewhere* into the buffer, but NOT necessarily at the beginning.

Referenced by str().


The documentation for this class was generated from the following file:
Generated on Tue Dec 2 11:16:30 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3