simutil.h Source File

API: simutil.h Source File
API
simutil.h
1//==========================================================================
2// SIMUTIL.H - part of
3// OMNeT++/OMNEST
4// Discrete System Simulation in C++
5//
6//==========================================================================
7
8/*--------------------------------------------------------------*
9 Copyright (C) 1992-2017 Andras Varga
10 Copyright (C) 2006-2017 OpenSim Ltd.
11
12 This file is distributed WITHOUT ANY WARRANTY. See the file
13 `license' for details on this and other legal matters.
14*--------------------------------------------------------------*/
15
16#ifndef __OMNETPP_SIMUTIL_H
17#define __OMNETPP_SIMUTIL_H
18
19#include <cmath>
20#include <string> // for std::string
21#include <typeinfo> // for type_info
22#include <type_traits> // is_integer
23#include "simkerneldefs.h"
24
25namespace omnetpp {
26
27// forward declarations
28class cObject;
29class cComponent;
30
35
36// helpers for checked_int_cast
37SIM_API void intCastError(const std::string& num, const char *errmsg=nullptr);
38SIM_API void intCastError(const std::string& num, const cObject *context, const char *errmsg=nullptr);
39
45template<typename ToInt, typename FromInt>
46ToInt checked_int_cast(FromInt x, const char *errmsg=nullptr)
47{
48 static_assert(std::is_integral<ToInt>::value && std::is_integral<FromInt>::value, "checked_int_cast expects integers");
49 ToInt res = x;
50 if ((x<0) != (res<0) || x-res != 0) // note: x!=res would result in warning: signed-unsigned comparison
51 omnetpp::intCastError(std::to_string(x), errmsg);
52 return res;
53}
54
60template<typename ToInt, typename FromInt>
61ToInt checked_int_cast(FromInt x, const cObject *context, const char *errmsg=nullptr)
62{
63 static_assert(std::is_integral<ToInt>::value && std::is_integral<FromInt>::value, "checked_int_cast expects integers");
64 ToInt res = x;
65 if ((x<0) != (res<0) || x-res != 0) // note: x!=res would result in warning: signed-unsigned comparison
66 omnetpp::intCastError(std::to_string(x), context, errmsg);
67 return res;
68}
69
75template<typename ToInt>
76ToInt checked_int_cast(double d, const char *errmsg=nullptr)
77{
78 static_assert(std::is_integral<ToInt>::value, "checked_int_cast expects integer template argument");
79 ToInt res = d;
80 if ((double)res != std::trunc(d))
81 omnetpp::intCastError(std::to_string(d), errmsg);
82 return res;
83}
84
88SIM_API const char *opp_typename(const std::type_info& t);
89
97SIM_API int64_t opp_get_monotonic_clock_nsecs(); // in gettime.cc
98
106SIM_API int64_t opp_get_monotonic_clock_usecs(); // in gettime.cc
107
109
110#if __cplusplus >= 202002L // only with C++20 or later
111
123struct opp_fmtstr {
124 const char *str;
125 size_t len;
126};
127
133constexpr opp_fmtstr operator""_fmt(const char *s, size_t n) { return {s, n}; }
134
135#endif // C++20
136
137} // namespace omnetpp
138
139#endif
140
141
Common base for module and channel classes.
Definition ccomponent.h:51
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition cobject.h:93
SIM_API const char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.
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...
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...
ToInt checked_int_cast(FromInt x, 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:46