# util.h

    00001 //==========================================================================
    00002 //  UTIL.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-2005 Andras Varga
    00013
    00014   This file is distributed WITHOUT ANY WARRANTY. See the file
    00015   `license' for details on this and other legal matters.
    00016 *--------------------------------------------------------------*/
    00017
    00018 #ifndef __UTIL_H
    00019 #define __UTIL_H
    00020
    00021 #include <stdarg.h>  // for va_list
    00022 #include <stdio.h>   // for sprintf
    00023 #include <typeinfo>  // for type_info
    00024 #include "defs.h"
    00025
    00026 // forward declarations
    00027 class cModule;
    00028
    00029 // logically belongs to csimul.h but must be here because of declaration order
    00030 enum {CTX_BUILD, CTX_INITIALIZE, CTX_EVENT, CTX_FINISH};
    00031
    00032 //
    00033 // #defines provided for backwards compatibility.
    00034 // They may be removed in a future release!
    00035 //
    00036 #define myrandomize      opp_randomize
    00037 #define genk_myrandomize genk_opp_randomize
    00038 #define mystrdup         opp_strdup
    00039 #define mystrcpy         opp_strcpy
    00040 #define mystrcmp         opp_strcmp
    00041 #define mystrmatch       opp_strmatch
    00042 #define fastconcat       opp_concat
    00043 #define indexedname      opp_mkindexedname
    00044
    00045 // windows.h defines min() and max() as macros
    00046 #undef min
    00047 #undef max
    00048
    00054
    00063 SIM_API simtime_t strToSimtime(const char *str);
    00064
    00076 SIM_API simtime_t strToSimtime0(const char *&str);
    00077
    00083 SIM_API char *simtimeToStr(simtime_t t, char *dest=NULL);
    00084
    00090 SIM_API char *simtimeToStrShort(simtime_t t, char *buf=NULL);
    00092
    00093
    00099
    00103 SIM_API double min(double a, double b);
    00104
    00108 SIM_API double max(double a, double b);
    00109
    00114 SIM_API double bool_and(double a, double b);
    00115
    00120 SIM_API double bool_or(double a, double b);
    00121
    00126 SIM_API double bool_xor(double a, double b);
    00127
    00132 SIM_API double bool_not(double a);
    00133
    00138 SIM_API double bin_and(double a, double b);
    00139
    00144 SIM_API double bin_or(double a, double b);
    00145
    00150 SIM_API double bin_xor(double a, double b);
    00151
    00156 SIM_API double bin_compl(double a);
    00157
    00162 SIM_API double shift_left(double a, double b);
    00163
    00168 SIM_API double shift_right(double a, double b);
    00170
    00171
    00183
    00188 SIM_API char *opp_strdup(const char *);
    00189
    00194 SIM_API char *opp_strcpy(char *,const char *);
    00195
    00200 SIM_API int  opp_strcmp(const char *, const char *);
    00201
    00206 SIM_API bool opp_strmatch(const char *, const char *);
    00207
    00212 SIM_API int opp_strlen(const char *);
    00213
    00217 SIM_API char *opp_mkindexedname(char *dest, const char *name, int index);
    00218
    00223 SIM_API char *opp_concat(const char *s1, const char *s2, const char *s3=NULL, const char *s4=NULL);
    00224
    00230 SIM_API char *opp_strprettytrunc(char *dest, const char *src, unsigned maxlen);
    00231
    00236 inline const char *correct(const char *);
    00238
    00244
    00248 inline bool equal(double a, double b, double epsilon);
    00250
    00251 //
    00252 // INTERNAL: a restricted vsscanf implementation used by cStatistic::freadvarsf()
    00253 //
    00254 SIM_API int opp_vsscanf(const char *s, const char *fmt, va_list va);
    00255
    00260
    00266 SIM_API void opp_error(int errcode,...);
    00267
    00274 SIM_API void opp_error(const char *msg,...);
    00275
    00286 SIM_API void opp_warning(int errcode,...);
    00287
    00298 SIM_API void opp_warning(const char *msg,...);
    00299
    00305 SIM_API void opp_terminate(int errcode,...);
    00306
    00313 SIM_API void opp_terminate(const char *msg,...);
    00315
    00316 //
    00317 // INTERNAL: return name of a C++ type -- correcting quirks of various compilers...
    00318 //
    00319 const char *opp_typename(const std::type_info& t);
    00320
    00321 //==========================================================================
    00322
    00323 #define MAX_METHODCALL 1024
    00324
    00342 #define Enter_Method cContextSwitcher __ctx(this); __ctx.methodCall
    00343
    00354 #define Enter_Method_Silent() cContextSwitcher __ctx(this)
    00355
    00366 class SIM_API cContextSwitcher
    00367 {
    00368   private:
    00369     cModule *callerContext;
    00370   public:
    00374     cContextSwitcher(cModule *thisptr);
    00375
    00379     ~cContextSwitcher();
    00380
    00385     void methodCall(const char *fmt,...);
    00386 };
    00387
    00395 class SIM_API cContextTypeSwitcher
    00396 {
    00397   private:
    00398     int contexttype;
    00399   public:
    00400
    00404     cContextTypeSwitcher(int ctxtype);
    00405
    00409     ~cContextTypeSwitcher();
    00410 };
    00411
    00412 //==========================================================================
    00413 //=== Implementation of utility functions:
    00414
    00415 inline bool equal(double a, double b, double epsilon)
    00416 {
    00417    double d = a-b;
    00418    return (d>=0.0 ? d : -d) < epsilon;
    00419 }
    00420
    00421 inline const char *correct(const char *s)
    00422 {
    00423    return s ? s : "";
    00424 }
    00425
    00426 // internally used: appends [num] to the given string
    00427 inline void opp_appendindex(char *s, unsigned int i)
    00428 {
    00429    while (*s) s++;
    00430    *s++ = '[';
    00431    if (i<10)
    00432        {*s++ = '0'+i; *s++=']'; *s=0; return;}
    00433    if (i<100)
    00434        {*s++ = '0'+i/10; *s++='0'+i%10; *s++=']'; *s=0; return;}
    00435    sprintf(s,"%d]",i);
    00436 }
    00437
    00438 #endif
    00439
    00440