16 #ifndef __OMNETPP_SIMTIME_H 17 #define __OMNETPP_SIMTIME_H 22 #include "platdep/intlimits.h" 23 #include "simkerneldefs.h" 30 #define INT64_MAX_DBL 9.22337203685e18 72 static int64_t dscale;
74 static double invfscale;
75 static int64_t maxseconds;
78 static const int SCALEEXP_UNINITIALIZED = 0xffff;
89 static void resetScale() {scaleexp = SCALEEXP_UNINITIALIZED;}
92 template<
typename T>
void assertInited(T d) {
if (scaleexp==SCALEEXP_UNINITIALIZED) initError(d);}
93 void initError(
double d);
95 bool haveSameSign(int64_t a, int64_t b) {
return (a^b) >= 0; }
97 int64_t toInt64(
double i64) {
98 #ifndef USE_OMNETPP4x_FINGERPRINTS 99 i64 = floor(i64 + 0.5);
101 if (fabs(i64) > INT64_MAX_DBL)
102 rangeErrorInt64(i64);
106 void setSeconds(int64_t sec) {
107 if (sec > maxseconds)
108 rangeErrorSeconds(sec);
112 void checkedAdd(
const SimTime& x) {
114 bool sameSign = haveSameSign(t, x.t);
116 if (sameSign && !haveSameSign(t, x.t))
120 void checkedSub(
const SimTime& x) {
122 bool differentSign = !haveSameSign(t, x.t);
124 if (differentSign && haveSameSign(t, x.t))
125 overflowSubtracting(x);
128 void checkedMul(int64_t x);
129 void rangeErrorInt64(
double i64);
130 void rangeErrorSeconds(int64_t x);
131 void overflowAdding(
const SimTime& x);
132 void overflowSubtracting(
const SimTime& x);
133 void overflowNegating();
137 std::string format(
int prec=getScaleExp(),
const char *decimalSep=
".",
const char *digitSep=
"",
bool addUnits=
false,
const char *beforeUnit=
nullptr,
const char *afterUnit=
nullptr)
const;
192 const SimTime& operator=(
double d) {assertInited(d); t=toInt64(fscale*d);
return *
this;}
193 const SimTime& operator=(
short d) {assertInited(d); setSeconds(d);
return *
this;}
194 const SimTime& operator=(
int d) {assertInited(d); setSeconds(d);
return *
this;}
195 const SimTime& operator=(
long d) {assertInited(d); setSeconds(d);
return *
this;}
196 const SimTime& operator=(
long long d) {assertInited(d); setSeconds(d);
return *
this;}
197 const SimTime& operator=(
unsigned short d) {assertInited(d); setSeconds(d);
return *
this;}
198 const SimTime& operator=(
unsigned int d) {assertInited(d); setSeconds(d);
return *
this;}
199 const SimTime& operator=(
unsigned long d) {assertInited(d); setSeconds(d);
return *
this;}
200 const SimTime& operator=(
unsigned long long d) {assertInited(d); setSeconds(d);
return *
this;}
202 bool operator==(
const SimTime& x)
const {
return t==x.t;}
203 bool operator!=(
const SimTime& x)
const {
return t!=x.t;}
204 bool operator< (
const SimTime& x)
const {
return t<x.t;}
205 bool operator> (
const SimTime& x)
const {
return t>x.t;}
206 bool operator<=(
const SimTime& x)
const {
return t<=x.t;}
207 bool operator>=(
const SimTime& x)
const {
return t>=x.t;}
209 SimTime operator-()
const {
SimTime x; x.t = -t;
if (x.t==INT64_MIN) x.overflowNegating();
return x;}
211 const SimTime& operator+=(
const SimTime& x) {checkedAdd(x);
return *
this;}
212 const SimTime& operator-=(
const SimTime& x) {checkedSub(x);
return *
this;}
216 const SimTime& operator*=(
double d) {t=toInt64(t*d);
return *
this;}
217 const SimTime& operator*=(
short d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
218 const SimTime& operator*=(
int d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
219 const SimTime& operator*=(
long d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
220 const SimTime& operator*=(
long long d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
221 const SimTime& operator*=(
unsigned short d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
222 const SimTime& operator*=(
unsigned int d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
223 const SimTime& operator*=(
unsigned long d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
224 const SimTime& operator*=(
unsigned long long d) {
if (checkmul) checkedMul(d);
else t*=d;
return *
this;}
227 const SimTime& operator/=(
double d) {t=toInt64(t/d);
return *
this;}
228 const SimTime& operator/=(
short d) {t/=d;
return *
this;}
229 const SimTime& operator/=(
int d) {t/=d;
return *
this;}
230 const SimTime& operator/=(
long d) {t/=d;
return *
this;}
231 const SimTime& operator/=(
long long d) {t/=d;
return *
this;}
232 const SimTime& operator/=(
unsigned short d) {t/=d;
return *
this;}
233 const SimTime& operator/=(
unsigned int d) {t/=d;
return *
this;}
234 const SimTime& operator/=(
unsigned long d) {t/=d;
return *
this;}
235 const SimTime& operator/=(
unsigned long long d) {t/=d;
return *
this;}
271 friend double operator/(
const SimTime& x,
const SimTime& y) {
return (
double)x.
raw() / (double)y.
raw(); }
273 friend double operator/(
double x,
const SimTime& y) {
return x / y.
dbl(); }
274 friend double operator/(
short x,
const SimTime& y) {
return (
long long)x / y; }
275 friend double operator/(
int x,
const SimTime& y) {
return (
long long)x / y; }
276 friend double operator/(
long x,
const SimTime& y) {
return (
long long)x / y; }
277 friend double operator/(
long long x,
const SimTime& y);
278 friend double operator/(
unsigned short x,
const SimTime& y) {
return (
unsigned long long)x / y; }
279 friend double operator/(
unsigned int x,
const SimTime& y) {
return (
unsigned long long)x / y; }
280 friend double operator/(
unsigned long x,
const SimTime& y) {
return (
unsigned long long)x / y; }
281 friend double operator/(
unsigned long long x,
const SimTime& y);
282 friend double operator/(
const cPar& p,
const SimTime& x);
301 double dbl()
const {
return t*invfscale;}
351 std::string
str()
const {
char buf[64];
return str(buf);}
360 char *
str(
char *buf)
const {
char *endp;
return SimTime::ttoa(buf, t, getScaleExp(), endp);}
368 std::string ustr()
const;
381 int64_t
raw()
const {
return t;}
418 static void setScaleExp(
int e);
425 static const SimTime parse(
const char *s);
436 static const SimTime parse(
const char *s,
const char *&endp);
447 static char *ttoa(
char *buf, int64_t t,
int scaleexp,
char *&endp);
451 inline std::ostream& operator<<(std::ostream& os,
const SimTime& x)
453 char buf[64];
char *endp;
SimTime(cPar &d)
Definition: simtime.h:163
SimTime trunc(SimTimeUnit unit) const
Definition: simtime.h:324
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
SimTime remainderForUnit(SimTimeUnit unit) const
Definition: simtime.h:332
Represents a module or channel parameter.
Definition: cpar.h:68
char * str(char *buf) const
Definition: simtime.h:360
SimTime()
Definition: simtime.h:145
static const SimTime ZERO
Definition: simtime.h:86
SimTime(const SimTime &x)
Definition: simtime.h:185
int64_t raw() const
Definition: simtime.h:381
static const SimTime getMaxTime()
Definition: simtime.h:392
std::string str() const
Definition: simtime.h:351
static int getScaleExp()
Definition: simtime.h:404
bool isZero() const
Definition: simtime.h:293
double dbl() const
Definition: simtime.h:301
const SimTime & setRaw(int64_t l)
Definition: simtime.h:386
static char * ttoa(char *buf, int64_t t, int scaleexp, char *&endp)
SimTimeUnit
Enum for simulation time base-10 exponents.
Definition: simtime.h:36
Definition: cabstracthistogram.h:21
static int64_t getScale()
Definition: simtime.h:398
SimTime(double d)
Definition: simtime.h:155