16 #ifndef __OMNETPP_SIMUTIL_H 17 #define __OMNETPP_SIMUTIL_H 19 #include <type_traits> 27 #include "platdep/platmisc.h" 28 #include "simkerneldefs.h" 38 enum {CTX_NONE, CTX_BUILD, CTX_INITIALIZE, CTX_EVENT, CTX_REFRESHDISPLAY, CTX_FINISH, CTX_CLEANUP};
45 void intCastError(
const std::string& num,
const char *errmsg=
nullptr);
46 void intCastError(
const std::string& num,
const cObject *context,
const char *errmsg=
nullptr);
53 template<
typename ToInt,
typename FromInt>
56 static_assert(std::is_integral<ToInt>::value && std::is_integral<FromInt>::value,
"checked_int_cast expects integers");
58 if ((x<0) != (res<0) || x-res != 0)
59 intCastError(std::to_string(x), errmsg);
68 template<
typename ToInt,
typename FromInt>
71 static_assert(std::is_integral<ToInt>::value && std::is_integral<FromInt>::value,
"checked_int_cast expects integers");
73 if ((x<0) != (res<0) || x-res != 0)
74 intCastError(std::to_string(x), context, errmsg);
83 template<
typename ToInt>
86 static_assert(std::is_integral<ToInt>::value,
"checked_int_cast expects integer template argument");
87 if (d < std::numeric_limits<ToInt>::min() || d > std::numeric_limits<ToInt>::max())
88 intCastError(std::to_string(d), errmsg);
114 inline int opp_strcmp(
const char *,
const char *);
126 SIM_API
const char *
opp_typename(
const std::type_info& t);
149 SIM_API
const char *opp_demangle_typename(
const char *mangledName);
173 #define Enter_Method omnetpp::cMethodCallContextSwitcher __ctx(this); __ctx.methodCall 194 #define Enter_Method_Silent omnetpp::cMethodCallContextSwitcher __ctx(this); __ctx.methodCallSilent 244 void methodCall(
const char *methodFmt,...);
245 void methodCallSilent(
const char *methodFm,...);
246 void methodCallSilent();
264 int savedcontexttype;
282 return strcpy(s1, s2 ? s2 :
"");
287 return s ? strlen(s) : 0;
292 if (!s || !s[0])
return nullptr;
293 char *p =
new char[strlen(s)+1];
298 inline char *
opp_strdup(
const char *s,
int len)
300 if (!s || !s[0])
return nullptr;
301 char *p =
new char[len+1];
310 return s2 ? strcmp(s1,s2) : (*s1 ? 1 : 0);
312 return (s2 && *s2) ? -1 : 0;
316 inline void opp_appendindex(
char *s,
unsigned int i)
321 {*s++ =
'0'+i; *s++=
']'; *s=0;
return;}
323 {*s++ =
'0'+i/10; *s++=
'0'+i%10; *s++=
']'; *s=0;
return;}
328 inline std::string double_to_str(
double t)
330 #if __cplusplus >= 201103L 331 return std::to_string(t);
334 return gcvt(t,16,buf);
SIM_API int64_t opp_get_monotonic_clock_nsecs()
Returns a monotonic time in nanoseconds since some unspecified starting point. This clock is not affe...
Common base for module and channel classes.
Definition: ccomponent.h:48
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
SIM_API char * opp_strprettytrunc(char *dest, const char *src, unsigned maxlen)
Copies src string into dest, and if its length would exceed maxlen, it is truncated with an ellipsis...
char * opp_strdup(const char *)
Duplicates the string, using new char[]. For nullptr and empty strings it returns nullptr...
Definition: simutil.h:290
Internal class. May only be used via the Enter_Method() and Enter_Method_Silent() macros! ...
Definition: simutil.h:223
char * opp_strcpy(char *, const char *)
Same as the standard strcpy() function, except that nullptr as the second argument is treated as an e...
Definition: simutil.h:280
The constructor switches the context to the given component, and the destructor restores the original...
Definition: simutil.h:203
Definition: cabstracthistogram.h:21
The constructor switches the context type, and the destructor restores the original context type...
Definition: simutil.h:261
ToInt checked_int_cast(double d, const char *errmsg=nullptr)
Safe integer cast: it throws an exception if in case of an overflow, i.e. when if the target type can...
Definition: simutil.h:84
int opp_strcmp(const char *, const char *)
Same as the standard strcmp() function, except that nullptr is treated exactly as an empty string (""...
Definition: simutil.h:307
SIM_API int64_t opp_get_monotonic_clock_usecs()
Returns a monotonic time in microseconds since some unspecified starting point. This clock is not aff...
int opp_strlen(const char *)
Same as the standard strlen() function, except that it also accepts nullptr and returns 0 for it...
Definition: simutil.h:285
SIM_API const char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.
static int getDepth()
Definition: simutil.h:251