Main Page Modules Class Hierarchy Compound List File List Compound Members

# util.h

    00001 //==========================================================================
    00002 //   UTIL.H - header for
    00003 //                             OMNeT++
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 //
    00007 //  Utility functions
    00008 //
    00009 //==========================================================================
    00010
    00011 /*--------------------------------------------------------------*
    00012   Copyright (C) 1992-2003 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 <typeinfo>  // for type_info
    00023 #include "defs.h"
    00024
    00025
    00029 #define NUM_RANDOM_GENERATORS    32
    00030
    00031 #define INTRAND_MAX  0x7ffffffeL  /* = 2**31-2 FIXME */
    00032
    00033
    00034 //
    00035 // #defines provided for backwards compatibility.
    00036 // They may be removed in a future release!
    00037 //
    00038 #define myrandomize      opp_randomize
    00039 #define genk_myrandomize genk_opp_randomize
    00040 #define mystrdup         opp_strdup
    00041 #define mystrcpy         opp_strcpy
    00042 #define mystrcmp         opp_strcmp
    00043 #define mystrmatch       opp_strmatch
    00044 #define fastconcat       opp_concat
    00045 #define indexedname      opp_mkindexedname
    00046
    00047
    00053
    00062 SIM_API simtime_t strToSimtime(const char *str);
    00063
    00074 SIM_API simtime_t strToSimtime0(const char *&str);
    00075
    00081 SIM_API char *simtimeToStr(simtime_t t, char *dest=NULL);
    00083
    00084
    00090
    00094 SIM_API double min(double a, double b);
    00095
    00099 SIM_API double max(double a, double b);
    00100
    00105 SIM_API double bool_and(double a, double b);
    00106
    00111 SIM_API double bool_or(double a, double b);
    00112
    00117 SIM_API double bool_xor(double a, double b);
    00118
    00123 SIM_API double bool_not(double a);
    00124
    00129 SIM_API double bin_and(double a, double b);
    00130
    00135 SIM_API double bin_or(double a, double b);
    00136
    00141 SIM_API double bin_xor(double a, double b);
    00142
    00147 SIM_API double bin_compl(double a);
    00148
    00153 SIM_API double shift_left(double a, double b);
    00154
    00159 SIM_API double shift_right(double a, double b);
    00161
    00162
    00174
    00179 SIM_API char *opp_strdup(const char *);
    00180
    00185 SIM_API char *opp_strcpy(char *,const char *);
    00186
    00191 SIM_API int  opp_strcmp(const char *, const char *);
    00192
    00197 SIM_API bool opp_strmatch(const char *, const char *);
    00198
    00203 SIM_API int opp_strlen(const char *);
    00204
    00208 SIM_API char *opp_mkindexedname(char *dest, const char *name, int index);
    00209
    00214 SIM_API char *opp_concat(const char *s1, const char *s2, const char *s3=NULL, const char *s4=NULL);
    00215
    00221 SIM_API char *opp_strprettytrunc(char *dest, const char *src, unsigned maxlen);
    00222
    00227 inline const char *correct(const char *);
    00228
    00230
    00236
    00240 inline bool equal(double a, double b, double epsilon);
    00242
    00243 //
    00244 // INTERNAL: a restricted vsscanf implementation used by cStatistic::freadvarsf()
    00245 //
    00246 SIM_API int opp_vsscanf(const char *s, const char *fmt, va_list va);
    00247
    00252
    00258 SIM_API void opp_error(int errcode,...);
    00259
    00266 SIM_API void opp_error(const char *msg,...);
    00267
    00278 SIM_API void opp_warning(int errcode,...);
    00279
    00290 SIM_API void opp_warning(const char *msg,...);
    00291
    00297 SIM_API void opp_terminate(int errcode,...);
    00298
    00305 SIM_API void opp_terminate(const char *msg,...);
    00307
    00308 //
    00309 // INTERNAL: return name of a C++ type -- correcting quirks of various compilers...
    00310 //
    00311 const char *opp_typename(const std::type_info& t);
    00312
    00313
    00324 class SIM_API opp_string
    00325 {
    00326   private:
    00327     char *str;
    00328
    00329   public:
    00333     opp_string()               {str = 0;}
    00334
    00338     opp_string(const char *s)  {str = opp_strdup(s);}
    00339
    00343     opp_string(opp_string& s)  {str = opp_strdup(s.str);}
    00344
    00348     ~opp_string()              {delete[] str;}
    00349
    00353     operator const char *() const    {return str;}
    00354
    00360     char *buffer() const        {return str;}
    00361
    00365     char *allocate(unsigned size)
    00366                                {delete[] str;str=new char[size];return str;}
    00367
    00372     const char *operator=(const char *s)
    00373                                {delete[] str;str=opp_strdup(s);return str;}
    00374
    00378     opp_string& operator=(const opp_string& s)
    00379                                {delete[] str;str=opp_strdup(s.str);return *this;}
    00380 };
    00381
    00382 //==========================================================================
    00383 //=== Implementation of utility functions:
    00384
    00385 inline bool equal(double a, double b, double epsilon)
    00386 {
    00387    double d = a-b;
    00388    return (d>=0.0 ? d : -d) < epsilon;
    00389 }
    00390
    00391 inline const char *correct(const char *s)
    00392 {
    00393    return s ? s : "";
    00394 }
    00395
    00396 #endif
    00397
    00398