INET Framework for OMNeT++/OMNEST
inet::utils Namespace Reference

Namespaces

 filters
 

Functions

std::string ltostr (long i)
 Converts an integer to string. More...
 
std::string dtostr (double d)
 Converts a double to string. More...
 
double atod (const char *s)
 Converts string to double. More...
 
unsigned long atoul (const char *s)
 Converts string to unsigned long. More...
 
std::string stripnonalnum (const char *s)
 Removes non-alphanumeric characters from the given string. More...
 
std::string stringf (const char *fmt,...)
 Accepts a printf-like argument list, and returns the result in a string. More...
 
std::string vstringf (const char *fmt, va_list &args)
 Accepts a vprintf-like argument list, and returns the result in a string. More...
 
cObject * createOneIfClassIsKnown (const char *className, const char *defaultNamespace=getSimulation() ->getContext() ->getClassName())
 Like cObjectFactory::createOneIfClassIsKnown(), except it starts searching for the class in the given namespace. More...
 
cObject * createOne (const char *className, const char *defaultNamespace=getSimulation() ->getContext() ->getClassName())
 Like cObjectFactory::createOne(), except it starts searching for the class in the given namespace. More...
 
int roundUp (int numToRound, int multiple)
 Rounding up to the nearest multiple of a number. More...
 
template<typename T >
TdupPacketAndControlInfo (T *packet)
 Duplicate a packet together with its control info. More...
 
cNEDValue nedf_hasVisualizer (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_hasVisualizer,"bool hasVisualizer()","","Returns true if the visualizer feature is available")
 
cNEDValue nedf_hasModule (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_hasModule,"bool hasModule(string nedTypeName)","string","Returns true if the given NED type exists")
 
cNEDValue nedf_haveClass (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_haveClass,"bool haveClass(string className)","string","Returns true if the given C++ class exists")
 
cNEDValue nedf_moduleListByPath (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_moduleListByPath,"string moduleListByPath(string modulePath,...)","string","Returns a space-separated list of the modules at the given path(s). ""See cTopology::extractByModulePath().")
 
cNEDValue nedf_moduleListByNedType (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_moduleListByNedType,"string moduleListByNedType(string nedTypeName,...)","string","Returns a space-separated list of the modules with the given NED type(s). ""See cTopology::extractByNedTypeName().")
 
cNEDValue nedf_select (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_select,"any select(int index, ...)","misc","Returns the <index>th item from the rest of the argument list; numbering starts from 0.")
 
cNEDValue nedf_absPath (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_absPath,"string absPath(string modulePath)","string","Returns absolute path of given module")
 
cNEDValue nedf_firstAvailableOrEmpty (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_firstAvailableOrEmpty,"string firstAvailableOrEmpty(...)","misc","Accepts any number of strings, interprets them as NED type names ""(qualified or unqualified), and returns the first one that exists and ""its C++ implementation class is also available. Returns empty string if ""none of the types are available.")
 
cNEDValue nedf_nanToZero (cComponent *context, cNEDValue argv[], int argc)
 
 Define_NED_Function2 (nedf_nanToZero,"quantity nanToZero(quantity x)","math","Returns the argument if it is not NaN, otherwise returns 0.")
 

Function Documentation

INET_API double inet::utils::atod ( const char *  s)

Converts string to double.

Referenced by inet::REDDropper::initialize(), IndexedImageFigure::parse(), IndicatorLabelFigure::parse(), IndicatorTextFigure::parse(), ProgressMeterFigure::parse(), LinearGaugeFigure::parse(), ThermometerFigure::parse(), CounterFigure::parse(), and GaugeFigure::parse().

39 {
40  char *e;
41  double d = ::strtod(s, &e);
42  if (*e)
43  throw cRuntimeError("Invalid cast: '%s' cannot be interpreted as a double", s);
44  return d;
45 }
const value< double, units::C > e(1.602176487e-19)
value< double, units::s > s
Definition: Units.h:1049
INET_API unsigned long inet::utils::atoul ( const char *  s)

Converts string to unsigned long.

Referenced by inet::DelegateSignalConfigurator::configureDisplaySignal(), inet::IPv6RoutingTable::configureInterfaceFromXML(), inet::FigureRecorder::init(), and inet::WRRScheduler::initialize().

48 {
49  char *e;
50  unsigned long d = ::strtoul(s, &e, 10);
51  if (*e)
52  throw cRuntimeError("Invalid cast: '%s' cannot be interpreted as an unsigned long", s);
53  return d;
54 }
const value< double, units::C > e(1.602176487e-19)
value< double, units::s > s
Definition: Units.h:1049
INET_API cObject * inet::utils::createOne ( const char *  className,
const char *  defaultNamespace 
)

Like cObjectFactory::createOne(), except it starts searching for the class in the given namespace.

Referenced by inet::sctp::SCTPAssociation::cloneAssociation(), inet::tcp::TCPConnection::cloneListeningConnection(), inet::tcp::TCP_NSC::handleIpInputMessage(), inet::TCPSrvHostApp::handleMessage(), inet::sctp::SCTPAssociation::initAssociation(), inet::tcp::TCPConnection::initConnection(), inet::LifecycleController::processCommand(), and roundUp().

106 {
107  cObject *ret = createOneIfClassIsKnown(className, defaultNamespace);
108  if (!ret)
109  throw cRuntimeError("Class \"%s\" not found -- perhaps its code was not linked in, "
110  "or the class wasn't registered with Register_Class(), or in the case of "
111  "modules and channels, with Define_Module()/Define_Channel()", className);
112  return ret;
113 }
cObject * createOneIfClassIsKnown(const char *className, const char *defaultNamespace)
Like cObjectFactory::createOneIfClassIsKnown(), except it starts searching for the class in the given...
Definition: INETUtils.cc:89
INET_API cObject * inet::utils::createOneIfClassIsKnown ( const char *  className,
const char *  defaultNamespace 
)

Like cObjectFactory::createOneIfClassIsKnown(), except it starts searching for the class in the given namespace.

Referenced by createOne(), and roundUp().

90 {
91  std::string ns = defaultNamespace;
92  do {
93  std::string::size_type found = ns.rfind("::");
94  if (found == std::string::npos)
95  found = 0;
96  ns.erase(found);
97  std::string cn = found ? ns + "::" + className : className;
98  cObject *ret = cObjectFactory::createOneIfClassIsKnown(cn.c_str());
99  if (ret)
100  return ret;
101  } while (!ns.empty());
102  return nullptr;
103 }
cObject * createOneIfClassIsKnown(const char *className, const char *defaultNamespace)
Like cObjectFactory::createOneIfClassIsKnown(), except it starts searching for the class in the given...
Definition: INETUtils.cc:89
inet::utils::Define_NED_Function2 ( nedf_hasVisualizer  ,
"bool hasVisualizer()"  ,
""  ,
"Returns true if the visualizer feature is available"   
)
inet::utils::Define_NED_Function2 ( nedf_hasModule  ,
"bool hasModule(string nedTypeName)"  ,
"string"  ,
"Returns true if the given NED type exists"   
)
inet::utils::Define_NED_Function2 ( nedf_haveClass  ,
"bool haveClass(string className)"  ,
"string"  ,
"Returns true if the given C++ class exists"   
)
inet::utils::Define_NED_Function2 ( nedf_moduleListByPath  ,
"string moduleListByPath(string modulePath,...)"  ,
"string"  ,
"Returns a space-separated list of the modules at the given path(s). ""See cTopology::extractByModulePath()."   
)
inet::utils::Define_NED_Function2 ( nedf_moduleListByNedType  ,
"string moduleListByNedType(string nedTypeName,...)"  ,
"string"  ,
"Returns a space-separated list of the modules with the given NED type(s). ""See cTopology::extractByNedTypeName()."   
)
inet::utils::Define_NED_Function2 ( nedf_select  ,
"any select(int index, ...)"  ,
"misc"  ,
"Returns the <index>th item from the rest of the argument list; numbering starts from 0."   
)
inet::utils::Define_NED_Function2 ( nedf_absPath  ,
"string absPath(string modulePath)"  ,
"string"  ,
"Returns absolute path of given module"   
)
inet::utils::Define_NED_Function2 ( nedf_firstAvailableOrEmpty  ,
"string firstAvailableOrEmpty(...)"  ,
"misc"  ,
"Accepts any number of  strings,
interprets them as NED type names""  qualified or unqualified,
and returns the first one that exists and""its C++implementation class is also available.Returns empty string if""none of the types are available."   
)
inet::utils::Define_NED_Function2 ( nedf_nanToZero  ,
"quantity nanToZero(quantity x)"  ,
"math"  ,
"Returns the argument if it is not  NaN,
otherwise returns 0."   
)
INET_API std::string inet::utils::dtostr ( double  d)

Converts a double to string.

32 {
33  std::ostringstream os;
34  os << d;
35  return os.str();
36 }
template<typename T >
T* inet::utils::dupPacketAndControlInfo ( T packet)

Duplicate a packet together with its control info.

(cPacket's dup() ignores the control info, it will be nullptr in the returned copy).

Referenced by inet::OrdinalBasedDuplicator::handleMessage(), inet::ieee80211::Tx::radioTransmissionFinished(), and inet::ieee80211::Tx::transmitFrame().

86  {
87  T *copy = packet->dup();
88  if (cObject *ctrl = packet->getControlInfo())
89  copy->setControlInfo(ctrl->dup());
90  return copy;
91 }
compose< Wb, pow< m,-2 > > T
Definition: Units.h:781
INET_API std::string inet::utils::ltostr ( long  i)

Converts an integer to string.

Referenced by inet::DiffservUtil::colorToString(), and inet::DiffservUtil::dscpToString().

25 {
26  std::ostringstream os;
27  os << i;
28  return os.str();
29 }
cNEDValue inet::utils::nedf_absPath ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
151 {
152  if (argc != 1)
153  throw cRuntimeError("absPath(): must be one argument instead of %d argument(s)", argc);
154  const char *path = argv[0].stringValue();
155  switch (*path) {
156  case '.':
157  return context->getFullPath() + path;
158 
159  case '^':
160  return context->getFullPath() + '.' + path;
161 
162  default:
163  return argv[0];
164  }
165 }
cNEDValue inet::utils::nedf_firstAvailableOrEmpty ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
174 {
175  cRegistrationList *types = componentTypes.getInstance();
176  for (int i=0; i<argc; i++)
177  {
178  if (argv[i].getType() != cNEDValue::STR)
179  throw cRuntimeError("firstAvailable(): string arguments expected");
180  const char *name = argv[i].stringValue();
181  cComponentType *c;
182  c = dynamic_cast<cComponentType *>(types->lookup(name)); // by qualified name
183  if (c && c->isAvailable())
184  return argv[i];
185  c = dynamic_cast<cComponentType *>(types->find(name)); // by simple name
186  if (c && c->isAvailable())
187  return argv[i];
188  }
189  return "";
190 }
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
cNEDValue inet::utils::nedf_hasModule ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
48 {
49  cRegistrationList *types = componentTypes.getInstance();
50  if (argv[0].getType() != cNEDValue::STR)
51  throw cRuntimeError("hasModule(): string arguments expected");
52  const char *name = argv[0].stringValue();
53  cComponentType *c;
54  c = dynamic_cast<cComponentType *>(types->lookup(name)); // by qualified name
55  if (c && c->isAvailable())
56  return true;
57  c = dynamic_cast<cComponentType *>(types->find(name)); // by simple name
58  if (c && c->isAvailable())
59  return true;
60  return false;
61 }
const value< double, compose< units::m, pow< units::s,-1 > > > c(299792458)
cNEDValue inet::utils::nedf_hasVisualizer ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
33 {
34 #ifdef WITH_VISUALIZERS
35  return true;
36 #else
37  return false;
38 #endif
39 }
cNEDValue inet::utils::nedf_haveClass ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
70 {
71  return classes.getInstance()->lookup(argv[0].stringValue()) != nullptr;
72 }
#define stringValue()
Definition: NedFunctions.cc:24
cNEDValue inet::utils::nedf_moduleListByNedType ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
108 {
109  std::string modulenames;
110  cTopology topo;
111  std::vector<std::string> paths;
112  for (int i = 0; i < argc; i++)
113  paths.push_back(argv[i].stdstringValue());
114 
115  topo.extractByNedTypeName(paths);
116 
117  for (int i = 0; i < topo.getNumNodes(); i++) {
118  std::string path = topo.getNode(i)->getModule()->getFullPath();
119  if (modulenames.length() > 0)
120  modulenames = modulenames + " " + path;
121  else
122  modulenames = path;
123  }
124  return modulenames;
125 }
cNEDValue inet::utils::nedf_moduleListByPath ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
81 {
82  std::string modulenames;
83  cTopology topo;
84  std::vector<std::string> paths;
85  for (int i = 0; i < argc; i++)
86  paths.push_back(argv[i].stdstringValue());
87 
88  topo.extractByModulePath(paths);
89 
90  for (int i = 0; i < topo.getNumNodes(); i++) {
91  std::string path = topo.getNode(i)->getModule()->getFullPath();
92  if (modulenames.length() > 0)
93  modulenames = modulenames + " " + path;
94  else
95  modulenames = path;
96  }
97  return modulenames;
98 }
cNEDValue inet::utils::nedf_nanToZero ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
201 {
202  double x = argv[0].doubleValue();
203  const char *unit = argv[0].getUnit();
204  return std::isnan(x) ? cNEDValue(0, unit) : argv[0];
205 }
#define cNEDValue
Definition: NedFunctions.cc:23
pow< internal::none, 0 > unit
Definition: Units.h:60
cNEDValue inet::utils::nedf_select ( cComponent *  context,
cNEDValue  argv[],
int  argc 
)
135 {
136  long index = argv[0];
137  if (index < 0)
138  throw cRuntimeError("select(): negative index %ld", index);
139  if (index >= argc - 1)
140  throw cRuntimeError("select(): index=%ld is too large", index, argc - 1);
141  return argv[index + 1];
142 }
int inet::utils::roundUp ( int  numToRound,
int  multiple 
)
inline

Rounding up to the nearest multiple of a number.

Referenced by inet::IPv4Datagram::calculateHeaderByteLength(), and inet::GPSR::setGpsrOptionOnNetworkDatagram().

69 { return ((numToRound + multiple -1) / multiple) * multiple; }
INET_API std::string inet::utils::stringf ( const char *  fmt,
  ... 
)

Accepts a printf-like argument list, and returns the result in a string.

The limit is 1024 chars.

Referenced by inet::IPv4RoutingTable::printMulticastRoutingTable(), and inet::IPv4RoutingTable::printRoutingTable().

69 {
70  char buf[BUFLEN];
71  va_list va;
72  va_start(va, fmt);
73  vsnprintf(buf, BUFLEN, fmt, va);
74  buf[BUFLEN - 1] = '\0';
75  va_end(va);
76  return buf;
77 }
#define BUFLEN
Definition: INETUtils.cc:66
INET_API std::string inet::utils::stripnonalnum ( const char *  s)

Removes non-alphanumeric characters from the given string.

Referenced by inet::EtherEncap::initialize(), inet::ieee80211::Ieee80211AgentSTA::initialize(), inet::ieee80211::Ieee80211MgmtSTA::initialize(), inet::InterfaceEntry::InterfaceEntry(), and inet::ieee80211::Ieee80211Mac::isInterfaceRegistered().

57 {
58  std::string result;
59  for ( ; *s; s++)
60  if (isalnum(*s))
61  result += *s;
62 
63  return result;
64 }
value< double, units::s > s
Definition: Units.h:1049
INET_API std::string inet::utils::vstringf ( const char *  fmt,
va_list &  args 
)

Accepts a vprintf-like argument list, and returns the result in a string.

The limit is 1024 chars.

80 {
81  char buf[BUFLEN];
82  vsnprintf(buf, BUFLEN, fmt, args);
83  buf[BUFLEN - 1] = '\0';
84  return buf;
85 }
#define BUFLEN
Definition: INETUtils.cc:66