OMNeT++ API 6.1
Discrete Event Simulation Library
SimTime Class Reference

Description

int64_t-based, base-10 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_t; the former eliminates an int64_t-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_t multiplication.

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

Public Member Functions

Constructors
 SimTime ()
 
 SimTime (double d)
 
 SimTime (short d)
 
 SimTime (int d)
 
 SimTime (long d)
 
 SimTime (long long d)
 
 SimTime (unsigned short d)
 
 SimTime (unsigned int d)
 
 SimTime (unsigned long d)
 
 SimTime (unsigned long long d)
 
 SimTime (const cPar &d)
 
 SimTime (double value, SimTimeUnit unit, bool allowRounding=false)
 
 SimTime (short d, SimTimeUnit unit)
 
 SimTime (int d, SimTimeUnit unit)
 
 SimTime (long d, SimTimeUnit unit)
 
 SimTime (long long d, SimTimeUnit unit)
 
 SimTime (unsigned short d, SimTimeUnit unit)
 
 SimTime (unsigned int d, SimTimeUnit unit)
 
 SimTime (unsigned long d, SimTimeUnit unit)
 
 SimTime (unsigned long long d, SimTimeUnit unit)
 
 SimTime (const SimTime &x)
 

Static Public Attributes

static const SimTime ZERO
 

Arithmetic operations

SimTimeoperator= (const SimTime &x)=default
 
SimTimeoperator= (const cPar &d)
 
SimTimeoperator= (double d)
 
SimTimeoperator= (short d)
 
SimTimeoperator= (int d)
 
SimTimeoperator= (long d)
 
SimTimeoperator= (long long d)
 
SimTimeoperator= (unsigned short d)
 
SimTimeoperator= (unsigned int d)
 
SimTimeoperator= (unsigned long d)
 
SimTimeoperator= (unsigned long long 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 SimTimeoperator+= (const SimTime &x)
 
const SimTimeoperator-= (const SimTime &x)
 
const SimTimeoperator*= (double d)
 
const SimTimeoperator*= (short d)
 
const SimTimeoperator*= (int d)
 
const SimTimeoperator*= (long d)
 
const SimTimeoperator*= (long long d)
 
const SimTimeoperator*= (unsigned short d)
 
const SimTimeoperator*= (unsigned int d)
 
const SimTimeoperator*= (unsigned long d)
 
const SimTimeoperator*= (unsigned long long d)
 
const SimTimeoperator*= (const cPar &p)
 
const SimTimeoperator/= (double d)
 
const SimTimeoperator/= (short d)
 
const SimTimeoperator/= (int d)
 
const SimTimeoperator/= (long d)
 
const SimTimeoperator/= (long long d)
 
const SimTimeoperator/= (unsigned short d)
 
const SimTimeoperator/= (unsigned int d)
 
const SimTimeoperator/= (unsigned long d)
 
const SimTimeoperator/= (unsigned long long d)
 
const SimTimeoperator/= (const cPar &p)
 
const friend SimTime operator+ (const SimTime &x, const SimTime &y)
 
const friend SimTime operator- (const SimTime &x, const SimTime &y)
 
const friend SimTime operator* (const SimTime &x, double d)
 
const friend SimTime operator* (const SimTime &x, short d)
 
const friend SimTime operator* (const SimTime &x, int d)
 
const friend SimTime operator* (const SimTime &x, long d)
 
const friend SimTime operator* (const SimTime &x, long long d)
 
const friend SimTime operator* (const SimTime &x, unsigned short d)
 
const friend SimTime operator* (const SimTime &x, unsigned int d)
 
const friend SimTime operator* (const SimTime &x, unsigned long d)
 
const friend SimTime operator* (const SimTime &x, unsigned long long d)
 
const friend SimTime operator* (const SimTime &x, const cPar &p)
 
const friend SimTime operator* (double d, const SimTime &x)
 
const friend SimTime operator* (short d, const SimTime &x)
 
const friend SimTime operator* (int d, const SimTime &x)
 
const friend SimTime operator* (long d, const SimTime &x)
 
const friend SimTime operator* (long long d, const SimTime &x)
 
const friend SimTime operator* (unsigned short d, const SimTime &x)
 
const friend SimTime operator* (unsigned int d, const SimTime &x)
 
const friend SimTime operator* (unsigned long d, const SimTime &x)
 
const friend SimTime operator* (unsigned long long d, const SimTime &x)
 
const friend SimTime operator* (const cPar &p, const SimTime &x)
 
const friend SimTime operator/ (const SimTime &x, double d)
 
const friend SimTime operator/ (const SimTime &x, short d)
 
const friend SimTime operator/ (const SimTime &x, int d)
 
const friend SimTime operator/ (const SimTime &x, long d)
 
const friend SimTime operator/ (const SimTime &x, long long d)
 
const friend SimTime operator/ (const SimTime &x, unsigned short d)
 
const friend SimTime operator/ (const SimTime &x, unsigned int d)
 
const friend SimTime operator/ (const SimTime &x, unsigned long d)
 
const friend SimTime operator/ (const SimTime &x, unsigned long long d)
 
const friend SimTime operator/ (const SimTime &x, const cPar &p)
 
double operator/ (const SimTime &x, const SimTime &y)
 
double operator/ (double x, const SimTime &y)
 
double operator/ (short x, const SimTime &y)
 
double operator/ (int x, const SimTime &y)
 
double operator/ (long x, const SimTime &y)
 
SIM_API double operator/ (long long x, const SimTime &y)
 
double operator/ (unsigned short x, const SimTime &y)
 
double operator/ (unsigned int x, const SimTime &y)
 
double operator/ (unsigned long x, const SimTime &y)
 
SIM_API double operator/ (unsigned long long x, const SimTime &y)
 
SIM_API double operator/ (const cPar &p, const SimTime &x)
 

Misc operations and utilities

bool isZero () const
 
double dbl () const
 
int64_t inUnit (SimTimeUnit unit) const
 
SimTime trunc (SimTimeUnit unit) const
 
SimTime remainderForUnit (SimTimeUnit unit) const
 
void split (SimTimeUnit unit, int64_t &outValue, SimTime &outRemainder) const
 
std::string str () const
 
char * str (char *buf) const
 
std::string ustr () const
 
std::string ustr (SimTimeUnit unit) const
 
int64_t raw () const
 
const SimTimesetRaw (int64_t l)
 
static SimTime fromRaw (int64_t l)
 
static const SimTime getMaxTime ()
 
static int64_t 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 t, int scaleexp, char *&endp)
 

Constructor & Destructor Documentation

◆ SimTime() [1/7]

SimTime ( )
inline

Initialize to zero.

◆ SimTime() [2/7]

SimTime ( double  d)
inline

Initialize simulation time from a double-precision number. The value is understood in seconds. This constructor is recommended if the value is the result of some computation done in double. See also the SimTime(double x, int exponent) constructor.

◆ SimTime() [3/7]

SimTime ( short  d)
inline

This and the subsequent constructors initialize simulation time from an integer number of seconds.

◆ SimTime() [4/7]

SimTime ( const cPar d)
inline

Initialize simulation time from a module or channel parameter via a double or intval_t conversion, depending on the parameter's type. It currently does not check the measurement unit of the parameter (@unit NED property), although this may change in future releases.

◆ SimTime() [5/7]

SimTime ( double  value,
SimTimeUnit  unit,
bool  allowRounding = false 
)

Initialize simulation time from a value specified in the given unit. This constructor allows one to specify non-integer constants more conveniently and possibly more precisely than the plain double constructor. By default, i.e. when invoked with allowRounding=false, an effort is made to ensure that the simulation time precisely represents the given value. (If it cannot, an exception will be thrown.) With allowRounding=true, rounding errors are allowed.

Note that the unit parameter actually represents a base-10 exponent, so the constructor will also work correctly for values not in the SimTimeUnit enum.

Example: SimTime(3.5, SIMTIME_MS) -> 3500us

◆ SimTime() [6/7]

SimTime ( short  d,
SimTimeUnit  unit 
)
inline

This and the subsequent constructors initialize simulation time from an integer specified in the given unit. The intended purpose of this constructor is to allow specifying precise constants in model code. (The functions throws an an exception if the value cannot be precisely represented as simulation time.)

Note that the unit parameter actually represents a base-10 exponent, so the constructor also accepts integers not in the SimTimeUnit enum.

◆ SimTime() [7/7]

SimTime ( const SimTime x)
inline

Copy constructor.

Member Function Documentation

◆ isZero()

bool isZero ( ) const
inline

Returns true if this simulation time is zero, false otherwise. This is more efficient than comparing the variable to a (double) 0.0, and shorter than comparing against SimTime::ZERO.

Referenced by cPacket::isReceptionEnd().

◆ dbl()

double dbl ( ) const
inline

Converts simulation time (in seconds) to a 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 cStatistic::collect(), cStatistic::collectWeighted(), omnetpp::exponential(), cComponent::exponential(), omnetpp::normal(), cComponent::normal(), cOutVector::record(), cComponent::recordScalar(), cOutVector::recordWithTimestamp(), omnetpp::truncnormal(), cComponent::truncnormal(), omnetpp::uniform(), and cComponent::uniform().

◆ inUnit()

int64_t inUnit ( SimTimeUnit  unit) const

Converts the simulation time to the given time unit, discarding the fractional part (i.e. rounding towards zero). 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 ms --> 3; -3.8ms in ms --> -3; 999ms in s --> 0

◆ trunc()

SimTime trunc ( SimTimeUnit  unit) const
inline

Returns a new simulation time that is truncated (rounded towards zero) to the precision of the specified time unit.

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

◆ remainderForUnit()

SimTime remainderForUnit ( SimTimeUnit  unit) const
inline

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

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

◆ split()

void split ( SimTimeUnit  unit,
int64_t &  outValue,
SimTime outRemainder 
) const

Convenience method: splits the simulation time into a whole and a fractional part with regard to a time unit. t.split(exponent, outValue, outRemainder) is equivalent to:

outValue = t.inUnit(unit);
outRemainder = t.remainderForUnit(unit);

◆ str() [1/2]

std::string str ( ) const
inline

Converts the time to a numeric string. The number expresses the simulation time precisely (including all significant digits), in seconds. The measurement unit (seconds) is not part of the string.

References SimTime::str().

Referenced by SimTime::str().

◆ str() [2/2]

char* str ( char *  buf) const
inline

Converts to a string in the same way as str() does. 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 SimTime::ttoa().

◆ ustr() [1/2]

std::string ustr ( ) const

Converts the time to a numeric string with unit in the same format as ustr(SimTimeUnit) does, but chooses the time unit automatically. The function tries to choose the "natural" time unit, e.g. 0.0033 is returned as "3.3ms".

◆ ustr() [2/2]

std::string ustr ( SimTimeUnit  unit) const

Converts the time to a numeric string in the given time unit. The unit can be "s", "ms", "us", "ns", "ps", "fs", or "as". The result represents the simulation time precisely (includes all significant digits), and the unit is appended.

◆ raw()

int64_t raw ( ) const
inline

Returns the underlying 64-bit integer.

Referenced by omnetpp::ceil(), omnetpp::div(), omnetpp::fabs(), omnetpp::floor(), and omnetpp::fmod().

◆ fromRaw()

static SimTime fromRaw ( int64_t  l)
inlinestatic

Creates a SimTime by setting its underlying 64-bit integer.

◆ setRaw()

const SimTime& setRaw ( int64_t  l)
inline

Directly sets the underlying 64-bit integer.

Referenced by omnetpp::ceil(), omnetpp::floor(), omnetpp::fmod(), and SimTime::getMaxTime().

◆ getMaxTime()

static const SimTime getMaxTime ( )
inlinestatic

Returns the largest simulation time that can be represented using the present scale exponent.

References SimTime::setRaw().

◆ getScale()

static int64_t getScale ( )
inlinestatic

Returns the time resolution as the number of units per second, e.g. for microsecond resolution it returns 1000000.

Referenced by omnetpp::ceil(), and omnetpp::floor().

◆ getScaleExp()

static int getScaleExp ( )
inlinestatic

Returns the scale exponent, which is an integer in the range -18..0. For example, for microsecond resolution it returns -6.

◆ setScaleExp()

static void 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.

◆ parse() [1/2]

static const 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>)+.

◆ parse() [2/2]

static const 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.

◆ ttoa()

static char* ttoa ( char *  buf,
int64_t  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 SimTime::str().

Member Data Documentation

◆ ZERO

const SimTime ZERO
static

Represents the zero simulation time. Using SimTime::ZERO may be faster than writing SimTime() or conversion from 0.0, because the constructor call may be spared.


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