OMNeT++ Simulation Library  6.0.3
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 
25 namespace omnetpp {
26 
27 // forward declarations
28 class cObject;
29 class cComponent;
30 
36 // helpers for checked_int_cast
37 SIM_API void intCastError(const std::string& num, const char *errmsg=nullptr);
38 SIM_API void intCastError(const std::string& num, const cObject *context, const char *errmsg=nullptr);
39 
45 template<typename ToInt, typename FromInt>
46 ToInt 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 
60 template<typename ToInt, typename FromInt>
61 ToInt 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 
75 template<typename ToInt>
76 ToInt 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 
88 SIM_API const char *opp_typename(const std::type_info& t);
89 
97 SIM_API int64_t opp_get_monotonic_clock_nsecs(); // in gettime.cc
98 
106 SIM_API int64_t opp_get_monotonic_clock_usecs(); // in gettime.cc
107 
110 } // namespace omnetpp
111 
112 #endif
113 
114 
omnetpp::opp_get_monotonic_clock_usecs
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...
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::checked_int_cast
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:76
omnetpp::opp_get_monotonic_clock_nsecs
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...
omnetpp::opp_typename
const SIM_API char * opp_typename(const std::type_info &t)
Returns the name of a C++ type, correcting the quirks of various compilers.