simutil.h

00001 //==========================================================================
00002 //  SIMUTIL.H - part of
00003 //                     OMNeT++/OMNEST
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Utility functions
00008 //
00009 //==========================================================================
00010 
00011 /*--------------------------------------------------------------*
00012   Copyright (C) 1992-2008 Andras Varga
00013   Copyright (C) 2006-2008 OpenSim Ltd.
00014 
00015   This file is distributed WITHOUT ANY WARRANTY. See the file
00016   `license' for details on this and other legal matters.
00017 *--------------------------------------------------------------*/
00018 
00019 #ifndef __SIMUTIL_H
00020 #define __SIMUTIL_H
00021 
00022 #include <string.h>  // for strlen, etc.
00023 #include <stdarg.h>  // for va_list
00024 #include <stdio.h>   // for sprintf
00025 #include <stdlib.h>  // for gcvt
00026 #include <typeinfo>  // for type_info
00027 #include <string>    // for std::string
00028 #include "simkerneldefs.h"
00029 #include "platdep/platmisc.h" // for gcvt, etc
00030 #include "errmsg.h"
00031 
00032 NAMESPACE_BEGIN
00033 
00034 // forward declarations
00035 class cComponent;
00036 
00037 // logically belongs to csimulation.h but must be here because of declaration order
00038 enum {CTX_NONE, CTX_BUILD, CTX_INITIALIZE, CTX_EVENT, CTX_FINISH, CTX_CLEANUP};
00039 
00040 
00041 #ifdef USE_DOUBLE_SIMTIME
00042 
00047 
00055 SIM_API double strToSimtime(const char *str);
00056 
00068 SIM_API double strToSimtime0(const char *&str);
00069 
00075 SIM_API char *simtimeToStr(double t, char *dest=NULL);
00076 
00082 SIM_API char *simtimeToStrShort(double t, char *buf=NULL);
00084 #endif //USE_DOUBLE_SIMTIME
00085 
00086 
00101 inline int opp_strlen(const char *);
00102 
00107 inline char *opp_strdup(const char *);
00108 
00113 inline char *opp_strcpy(char *,const char *);
00114 
00119 inline int opp_strcmp(const char *, const char *);
00120 
00126 SIM_API char *opp_strprettytrunc(char *dest, const char *src, unsigned maxlen);
00127 
00129 
00134 
00140 SIM_API void opp_error(OppErrorCode errcode,...);
00141 
00148 SIM_API void opp_error(const char *msg,...);
00149 
00160 SIM_API void opp_warning(OppErrorCode errcode,...);
00161 
00172 SIM_API void opp_warning(const char *msg,...);
00173 
00179 SIM_API void opp_terminate(OppErrorCode errcode,...);
00180 
00187 SIM_API void opp_terminate(const char *msg,...);
00189 
00190 // INTERNAL: returns the name of a C++ type, correcting the quirks of various compilers.
00191 SIM_API const char *opp_typename(const std::type_info& t);
00192 
00193 
00213 #define Enter_Method cMethodCallContextSwitcher __ctx(this); __ctx.methodCall
00214 
00231 #define Enter_Method_Silent cMethodCallContextSwitcher __ctx(this); __ctx.methodCallSilent
00232 
00240 class SIM_API cContextSwitcher
00241 {
00242   protected:
00243     cComponent *callerContext;
00244   public:
00248     cContextSwitcher(const cComponent *newContext);
00249 
00253     ~cContextSwitcher();
00254 };
00255 
00260 class SIM_API cMethodCallContextSwitcher : public cContextSwitcher
00261 {
00262   public:
00266     cMethodCallContextSwitcher(const cComponent *newContext);
00267 
00271     ~cMethodCallContextSwitcher();
00272 
00277     void methodCall(const char *methodFmt,...);
00278     void methodCallSilent(const char *methodFm,...);
00279     void methodCallSilent();
00280 };
00281 
00289 class SIM_API cContextTypeSwitcher
00290 {
00291   private:
00292     int savedcontexttype;
00293 
00294   public:
00298     cContextTypeSwitcher(int contexttype);
00299 
00303     ~cContextTypeSwitcher();
00304 };
00305 
00306 //==========================================================================
00307 //=== Implementation of utility functions:
00308 
00309 #ifndef _STRINGUTIL_H_   // avoid clash with similar defs in common/stringutil.h
00310 
00311 inline char *opp_strcpy(char *s1,const char *s2)
00312 {
00313     return strcpy(s1, s2 ? s2 : "");
00314 }
00315 
00316 inline int opp_strlen(const char *s)
00317 {
00318     return s ? strlen(s) : 0;
00319 }
00320 
00321 inline char *opp_strdup(const char *s)
00322 {
00323     if (!s || !s[0]) return NULL;
00324     char *p = new char[strlen(s)+1];
00325     strcpy(p,s);
00326     return p;
00327 }
00328 
00329 inline char *opp_strdup(const char *s, int len)
00330 {
00331     if (!s || !s[0]) return NULL;
00332     char *p = new char[len+1];
00333     strncpy(p,s,len);
00334     p[len] = 0;
00335     return p;
00336 }
00337 
00338 inline int opp_strcmp(const char *s1, const char *s2)
00339 {
00340     if (s1)
00341         return s2 ? strcmp(s1,s2) : (*s1 ? 1 : 0);
00342     else
00343         return (s2 && *s2) ? -1 : 0;
00344 }
00345 
00346 #endif //_STRINGUTIL_H_
00347 
00348 // internally used: appends [num] to the given string
00349 inline void opp_appendindex(char *s, unsigned int i)
00350 {
00351    while (*s) s++;
00352    *s++ = '[';
00353    if (i<10)
00354        {*s++ = '0'+i; *s++=']'; *s=0; return;}
00355    if (i<100)
00356        {*s++ = '0'+i/10; *s++='0'+i%10; *s++=']'; *s=0; return;}
00357    sprintf(s,"%d]",i);
00358 }
00359 
00360 inline long double_to_long(double d)
00361 {
00362     // gcc feature: if double d=0xc0000000, (long)d yields 0x80000000 !
00363     // This only happens with long: unsigned long is OK.
00364     // This causes trouble if we in fact want to cast this long to unsigned long, see NED_expr_2.test.
00365     // Workaround follows. Note: even the ul variable is needed: when inlining it, gcc will do the wrong cast!
00366     long l = (long)d;
00367     unsigned long ul = (unsigned long)d;
00368     return d<0 ? l : ul;
00369 }
00370 
00371 // internal
00372 inline std::string double_to_str(double t)
00373 {
00374 #if __cplusplus >= 201103L
00375    return std::to_string(t);
00376 #else
00377    char buf[32];
00378    return gcvt(t,16,buf);
00379 #endif
00380 }
00381 
00382 
00383 NAMESPACE_END
00384 
00385 
00386 #endif
00387 
00388 
Generated on Tue Dec 2 11:16:27 2014 for OMNeT++ Simulation Library by  doxygen 1.6.3