Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

cpar.h

00001 //==========================================================================
00002 //   CPAR.H   - header for
00003 //                             OMNeT++
00004 //            Discrete System Simulation in C++
00005 //
00006 //
00007 //  Declaration of the following classes:
00008 //    cPar       : general value holding class
00009 //    cModulePar : specialized cPar that serves as module parameter
00010 //
00011 //==========================================================================
00012 
00013 /*--------------------------------------------------------------*
00014   Copyright (C) 1992-2003 Andras Varga
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
00018 *--------------------------------------------------------------*/
00019 
00020 #ifndef __CPAR_H
00021 #define __CPAR_H
00022 
00023 #include "cobject.h"
00024 
00025 #define SHORTSTR  27
00026 
00027 
00028 //=== classes declared here:
00029 class  cPar;
00030 class  cModulePar;
00031 
00032 //=== class mentioned
00033 class  cStatistic;
00034 
00035 
00036 //==========================================================================
00037 
00043 class cExpression
00044 {
00045   public:
00046     virtual ~cExpression() {}
00047     virtual void getAsText(char *buf, int maxlen) = 0;
00048     virtual bool parseText(const char *text) = 0;
00049     virtual cExpression *dup() = 0;
00050 };
00051 
00077 class cDoubleExpression : public cExpression
00078 {
00079   public:
00080     virtual void getAsText(char *buf, int maxlen);
00081     virtual bool parseText(const char *text);
00082     virtual double evaluate() = 0;
00083 };
00084 
00085 //==========================================================================
00086 
00114 class SIM_API cPar : public cObject
00115 {
00116   public:
00124     struct ExprElem
00125     {
00126         // Type chars:
00127         //   D  double constant
00128         //   P  pointer to "external" cPar (owned by someone else)
00129         //   R  "reference": the cPar will be dup()'ped and the copy kept
00130         //   0/1/2/3  function with 0/1/2/3 arguments
00131         //   @  math operator (+-*%/^=!<{>}?); see cPar::evaluate()
00132         //
00133         char type;    // D/P/R/0/1/2/3/@ (see above)
00134         union {
00135             double d;           // D
00136             cPar *p;            // P/R
00137             MathFuncNoArg f0;   // 0
00138             MathFunc1Arg  f1;   // 1
00139             MathFunc2Args f2;   // 2
00140             MathFunc3Args f3;   // 3
00141             MathFunc4Args f4;   // 4
00142             char op;            // @, op = +-*/%^=!<{>}?
00143         };
00144 
00149         void operator=(int _i)            {type='D'; d=_i;  }
00150 
00155         void operator=(long _l)           {type='D'; d=_l;  }
00156 
00161         void operator=(double _d)         {type='D'; d=_d;  }
00162 
00170         void operator=(cPar *_p)          {type='P'; p=_p;  }
00171 
00178         void operator=(cPar& _r);         //{type='R'; p=(cPar *)_r.dup();} See after cPar!
00179 
00189         void operator=(MathFuncNoArg _f)  {type='0'; f0=_f;}
00190 
00202         void operator=(MathFunc1Arg  _f)  {type='1'; f1=_f;}
00203 
00216         void operator=(MathFunc2Args _f)  {type='2'; f2=_f;}
00217 
00230         void operator=(MathFunc3Args _f)  {type='3'; f3=_f;}
00231 
00244         void operator=(MathFunc4Args _f)  {type='4'; f4=_f;}
00245 
00261         void operator=(char _op)          {type='@'; op=_op;}
00262     };
00263 
00264   protected:
00265     static char *possibletypes;
00266   private:
00267     char typechar;     // S/B/L/D/F/T/X/C/P/O/I
00268     bool inputflag;
00269     bool changedflag;
00270     opp_string promptstr; // prompt text used when the value is being input
00271 
00272     union {    // Take care of 'operator=()' when changing this!!!
00273        struct { bool sht; char *str;            } ls;   // S:long string
00274        struct { bool sht; char str[SHORTSTR+1]; } ss;   // S:short str
00275        struct { long val;                       } lng;  // L:long,B:bool
00276        struct { double val;                     } dbl;  // D:double
00277        struct { MathFunc f; int argc;
00278                 double p1,p2,p3,p4;             } func; // F:math function
00279        struct { cStatistic *res;                } dtr;  // T:distribution
00280        struct { cDoubleExpression *expr;        } cexpr;// C:compiled expression
00281        struct { ExprElem *xelem; int n;         } expr; // X:expression
00282        struct { cPar *par;                      } ind;  // I:indirection
00283        struct { void *ptr;
00284                 VoidDelFunc delfunc;
00285                 VoidDupFunc dupfunc;
00286                 size_t itemsize;                } ptr;  // P:void* pointer
00287        struct { cObject *obj;                   } obj;  // O:object pointer
00288     };
00289 
00290   private:
00291     // helper func: destruct old value
00292     void deleteold();
00293 
00294     // helper func: evaluates expression (X)
00295     double evaluate();
00296 
00297     // helper func: rand.num with given distr.(T)
00298     double fromstat();
00299 
00300     // setFromText() helper func.
00301     bool setfunction(char *w);
00302 
00303   protected:
00306 
00312     virtual void beforeChange();
00313 
00319     virtual void afterChange();
00321 
00322   public:
00325 
00329     cPar(const cPar& other);
00330 
00335     explicit cPar(const char *name=NULL);
00336 
00341     explicit cPar(const char *name, cPar& other);
00342 
00346     virtual ~cPar();
00347 
00359     cPar& operator=(const cPar& otherpar);
00361 
00364 
00369     virtual cObject *dup() const   {return new cPar(*this);}
00370 
00375     virtual void info(char *buf);
00376 
00381     virtual void writeContents(ostream& os);
00382 
00386     virtual void forEach(ForeachFunc f);
00387 
00393     virtual int netPack();
00394 
00400     virtual int netUnpack();
00402 
00405 
00409     cPar& setBoolValue(bool b);
00410 
00414     cPar& setLongValue(long l);
00415 
00421     cPar& setStringValue(const char *s);
00422 
00426     cPar& setDoubleValue(double d);
00427 
00433     cPar& setDoubleValue(cStatistic *res);
00434 
00440     cPar& setDoubleValue(ExprElem *x, int n);
00441 
00449     cPar& setDoubleValue(cDoubleExpression *expr);
00450 
00455     cPar& setDoubleValue(MathFuncNoArg f);
00456 
00462     cPar& setDoubleValue(MathFunc1Arg  f, double p1);
00463 
00469     cPar& setDoubleValue(MathFunc2Args f, double p1, double p2);
00470 
00476     cPar& setDoubleValue(MathFunc3Args f, double p1, double p2, double p3);
00477 
00483     cPar& setDoubleValue(MathFunc4Args f, double p1, double p2, double p3, double p4);
00484 
00491     cPar& setPointerValue(void *ptr);
00492 
00498     cPar& setObjectValue(cObject *obj);
00499 
00519     void configPointer( VoidDelFunc delfunc, VoidDupFunc dupfunc, size_t itemsize=0);
00521 
00524 
00528     bool boolValue();
00529 
00535     long longValue();
00536 
00540     const char *stringValue();
00541 
00547     double doubleValue();
00548 
00552     void *pointerValue();
00553 
00557     cObject *objectValue();
00559 
00562 
00570     cPar& setRedirection(cPar *par);
00571 
00575     bool isRedirected() const {return typechar=='I';}
00576 
00584     cPar *redirection();
00585 
00589     void cancelRedirection();
00591 
00594 
00600     char type() const;
00601 
00605     bool isNumeric() const;
00606 
00610     const char *prompt() ;
00611 
00615     void setPrompt(const char *s);
00616 
00620     void setInput(bool ip);
00621 
00626     bool isInput() const;
00627 
00633     bool changed();
00635 
00638 
00642     cPar& read();
00643 
00648     void convertToConst();
00649 
00654     bool equalsTo(cPar *par);
00656 
00659 
00664     virtual void getAsText(char *buf, int maxlen);
00665 
00673     virtual bool setFromText(const char *text, char tp);
00675 
00678 
00682     cPar& operator=(bool b)          {return setBoolValue(b);}
00683 
00687     cPar& operator=(const char *s)   {return setStringValue(s);}
00688 
00692     cPar& operator=(char c)          {return setLongValue((long)c);}
00693 
00697     cPar& operator=(unsigned char c) {return setLongValue((long)c);}
00698 
00702     cPar& operator=(int i)           {return setLongValue((long)i);}
00703 
00707     cPar& operator=(unsigned int i)  {return setLongValue((long)i);}
00708 
00712     cPar& operator=(long l)          {return setLongValue(l);}
00713 
00717     cPar& operator=(unsigned long l) {return setLongValue((long)l);}
00718 
00722     cPar& operator=(double d)        {return setDoubleValue(d);}
00723 
00727     cPar& operator=(long double d)   {return setDoubleValue((double)d);}
00728 
00732     cPar& operator=(void *ptr)       {return setPointerValue(ptr);}
00733 
00737     cPar& operator=(cObject *obj)    {return setObjectValue(obj);}
00738 
00742     operator bool()          {return boolValue();}
00743 
00747     operator const char *()  {return stringValue();}
00748 
00752     operator char()          {return (char)longValue();}
00753 
00757     operator unsigned char() {return (unsigned char)longValue();}
00758 
00762     operator int()           {return (int)longValue();}
00763 
00767     operator unsigned int()  {return (unsigned int)longValue();}
00768 
00772     operator long()          {return longValue();}
00773 
00777     operator unsigned long() {return longValue();}
00778 
00782     operator double()        {return doubleValue();}
00783 
00787     operator long double()   {return doubleValue();}
00788 
00792     operator void *()        {return pointerValue();}
00793 
00797     operator cObject *()     {return objectValue();}
00799 
00802 
00808     static int cmpbyvalue(cObject *one, cObject *other);
00810 };
00811 
00812 // this function cannot be defined within ExprElem because of declaration order
00813 inline void cPar::ExprElem::operator=(cPar& _r)  {type='R'; p=(cPar *)_r.dup();}
00814 
00815 //==========================================================================
00816 
00825 class SIM_API cModulePar : public cPar
00826 {
00827     friend class cModule;
00828   private:
00829     cModule *omodp;              // owner module
00830 
00831   private:
00832     // helper function
00833     void _construct();
00834 
00835   public:
00838 
00842     cModulePar(const cPar& other);
00843 
00847     explicit cModulePar(const char *name=NULL);
00848 
00852     explicit cModulePar(const char *name, cPar& other);
00853 
00857     virtual ~cModulePar();
00858 
00863     cModulePar& operator=(const cModulePar& otherpar);
00865 
00868 
00873     virtual cObject *dup() const  {return new cPar(*this);}
00874 
00878     virtual const char *fullPath() const;
00879 
00884     virtual const char *fullPath(char *buffer, int bufsize) const;
00886 
00889 
00893     void setOwnerModule(cModule *om)   {omodp=om;}
00894 
00898     cModule *ownerModule() const        {return omodp;}
00900 };
00901 
00902 
00903 //=== operators dealing with cPars
00904 //
00905 // These operators were removed -- see ChangeLog, Jan 17 2000 entry.
00906 //
00907 
00908 //#ifndef NO_CPAR_OPERATIONS
00909 //#ifdef  LONG_CPAR_OPERATIONS
00910 // inline int operator<(cPar& p, cPar& q)  {return (long)p<(long)q;}
00911 // inline int operator<(long d, cPar& p)   {return d<(long)p;}
00912 // inline int operator<(cPar& p, long d)   {return (long)p<d;}
00913 // inline int operator>(cPar& p, cPar& q)  {return (long)p>(long)q;}
00914 // inline int operator>(long d, cPar& p)   {return d>(long)p;}
00915 // inline int operator>(cPar& p, long d)   {return (long)p>d;}
00916 //
00917 // inline int operator<=(cPar& p, cPar& q) {return (long)p<=(long)q;}
00918 // inline int operator<=(long d, cPar& p)  {return d<=(long)p;}
00919 // inline int operator<=(cPar& p, long d)  {return (long)p<=d;}
00920 // inline int operator>=(cPar& p, cPar& q) {return (long)p>=(long)q;}
00921 // inline int operator>=(long d, cPar& p)  {return d>=(long)p;}
00922 // inline int operator>=(cPar& p, long d)  {return (long)p>=d;}
00923 //
00924 // inline long operator+(cPar& p, cPar& q) {return (long)p+(long)q;}
00925 // inline long operator+(long d, cPar& p)  {return d+(long)p;}
00926 // inline long operator+(cPar& p, long d)  {return (long)p+d;}
00927 // inline long operator-(cPar& p, cPar& q) {return (long)p-(long)q;}
00928 // inline long operator-(long d, cPar& p)  {return d-(long)p;}
00929 // inline long operator-(cPar& p, long d)  {return (long)p-d;}
00930 //
00931 // inline long operator*(cPar& p, cPar& q)  {return (long)p*(long)q;}
00932 // inline long operator*(long d, cPar& p)   {return d*(long)p;}
00933 // inline long operator*(cPar& p, long d)   {return (long)p*d;}
00934 // inline long operator/(cPar& p, cPar& q)  {return (long)p/(long)q;}
00935 // inline long operator/(long d, cPar& p)   {return d/(long)p;}
00936 // inline long operator/(cPar& p, long d)   {return (long)p/d;}
00937 //
00938 //#else
00939 //
00940 // inline int operator<(cPar& p, cPar& q)  {return (double)p<(double)q;}
00941 // inline int operator<(double d, cPar& p) {return d<(double)p;}
00942 // inline int operator<(cPar& p, double d) {return (double)p<d;}
00943 // inline int operator>(cPar& p, cPar& q)  {return (double)p>(double)q;}
00944 // inline int operator>(double d, cPar& p) {return d>(double)p;}
00945 // inline int operator>(cPar& p, double d) {return (double)p>d;}
00946 //
00947 // inline int operator<=(cPar& p, cPar& q)  {return (double)p<=(double)q;}
00948 // inline int operator<=(double d, cPar& p) {return d<=(double)p;}
00949 // inline int operator<=(cPar& p, double d) {return (double)p<=d;}
00950 // inline int operator>=(cPar& p, cPar& q)  {return (double)p>=(double)q;}
00951 // inline int operator>=(double d, cPar& p) {return d>=(double)p;}
00952 // inline int operator>=(cPar& p, double d) {return (double)p>=d;}
00953 //
00954 // inline double operator+(cPar& p, cPar& q)  {return (double)p+(double)q;}
00955 // inline double operator+(double d, cPar& p) {return d+(double)p;}
00956 // inline double operator+(cPar& p, double d) {return (double)p+d;}
00957 // inline double operator-(cPar& p, cPar& q)  {return (double)p-(double)q;}
00958 // inline double operator-(double d, cPar& p) {return d-(double)p;}
00959 // inline double operator-(cPar& p, double d) {return (double)p-d;}
00960 //
00961 // inline double operator*(cPar& p, cPar& q)  {return (double)p*(double)q;}
00962 // inline double operator*(double d, cPar& p) {return d*(double)p;}
00963 // inline double operator*(cPar& p, double d) {return (double)p*d;}
00964 // inline double operator/(cPar& p, cPar& q)  {return (double)p/(double)q;}
00965 // inline double operator/(double d, cPar& p) {return d/(double)p;}
00966 // inline double operator/(cPar& p, double d) {return (double)p/d;}
00967 //#endif
00968 //#endif
00969 
00970 #endif
00971 
00972 

Generated at Mon Jun 16 23:37:31 2003 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001