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-2001 Andras Varga
00015   Technical University of Budapest, Dept. of Telecommunications,
00016   Stoczek u.2, H-1111 Budapest, Hungary.
00017 
00018   This file is distributed WITHOUT ANY WARRANTY. See the file
00019   `license' for details on this and other legal matters.
00020 *--------------------------------------------------------------*/
00021 
00022 #ifndef __CPAR_H
00023 #define __CPAR_H
00024 
00025 #include "cobject.h"
00026 
00027 #define SHORTSTR  27
00028 
00029 #define NOPAR  NO(cPar)
00030 
00031 //=== classes declared here:
00032 class  cPar;
00033 class  cModulePar;
00034 
00035 //=== class mentioned
00036 class  cStatistic;
00037 
00038 //==========================================================================
00039 
00050 struct sXElem
00051 {
00052     // Type chars:
00053     //   D  double constant
00054     //   P  pointer to "external" cPar (owned by someone else)
00055     //   R  "reference": the cPar will be dup()'ped and the copy kept
00056     //   0/1/2/3  function with 0/1/2/3 arguments
00057     //   @  math operator (+-*%/^=!<{>}?); see cPar::evaluate()
00058     //
00059     char type;    // D/P/R/0/1/2/3/@ (see above)
00060     union {
00061         double d;           // D
00062         cPar *p;            // P/R
00063         MathFuncNoArg f0;   // 0
00064         MathFunc1Arg  f1;   // 1
00065         MathFunc2Args f2;   // 2
00066         MathFunc3Args f3;   // 3
00067         char op;            // @, op = +-*/%^=!<{>}?
00068     };
00069 
00074     void operator=(int _i)            {type='D'; d=_i;  }
00075 
00080     void operator=(long _l)           {type='D'; d=_l;  }
00081 
00086     void operator=(double _d)         {type='D'; d=_d;  }
00087 
00095     void operator=(cPar *_p)          {type='P'; p=_p;  }
00096 
00103     void operator=(cPar& _r);         //{type='R'; p=(cPar *)_r.dup();} See after cPar!
00104 
00117     void operator=(MathFuncNoArg _f)  {type='0'; f0=_f;}
00118 
00131     void operator=(MathFunc1Arg  _f)  {type='1'; f1=_f;}
00132 
00145     void operator=(MathFunc2Args _f)  {type='2'; f2=_f;}
00146 
00159     void operator=(MathFunc3Args _f)  {type='3'; f3=_f;}
00160 
00176     void operator=(char _op)          {type='@'; op=_op;}
00177 };
00178 
00179 //==========================================================================
00180 
00207 class SIM_API cPar : public cObject
00208 {
00209   protected:
00210     static char *possibletypes;
00211   private:
00212     char typechar;     // S/B/L/D/F/T/X/P/O/I
00213     bool inputflag;
00214     bool changedflag;
00215     opp_string promptstr; // prompt text used when the value is being input
00216 
00217     union {    // Take care of 'operator=()' when changing this!!!
00218        struct { bool sht; char *str;            } ls;   // S:long string
00219        struct { bool sht; char str[SHORTSTR+1]; } ss;   // S:short str
00220        struct { long val;                       } lng;  // L:long,B:bool
00221        struct { double val;                     } dbl;  // D:double
00222        struct { MathFunc f; int argc;
00223                 double p1,p2,p3;                } func; // F:math function
00224        struct { cStatistic *res;                } dtr;  // T:distribution
00225        struct { sXElem *xelem; int n;           } expr; // X:expression
00226        struct { cPar *par;                      } ind;  // I:indirection
00227        struct { void *ptr;
00228                 VoidDelFunc delfunc;
00229                 VoidDupFunc dupfunc;
00230                 size_t itemsize;                } ptr;  // P:void* pointer
00231        struct { cObject *obj;                   } obj;  // O:object pointer
00232     };
00233 
00234   private:
00235     // helper func: destruct old value
00236     void deleteold();
00237 
00238     // helper func: evaluates expression (X)
00239     double evaluate();
00240 
00241     // helper func: rand.num with given distr.(T)
00242     double fromstat();
00243 
00244     // setFromText() helper func.
00245     bool setfunction(char *w);
00246 
00247   protected:
00250 
00256     virtual void beforeChange();
00257 
00263     virtual void afterChange();
00265 
00266   public:
00269 
00273     cPar(const cPar& other);
00274 
00279     explicit cPar(const char *name=NULL);
00280 
00285     explicit cPar(const char *name, cPar& other);
00286 
00290     virtual ~cPar();
00291 
00303     cPar& operator=(const cPar& otherpar);
00305 
00308 
00312     virtual const char *className() const {return "cPar";}
00313 
00318     virtual cObject *dup() const   {return new cPar(*this);}
00319 
00324     virtual void info(char *buf);
00325 
00330     virtual const char *inspectorFactoryName() const {return "cParIFC";}
00331 
00336     virtual void writeContents(ostream& os);
00337 
00343     virtual int netPack();
00344 
00350     virtual int netUnpack();
00352 
00355 
00359     cPar& setBoolValue(bool b);
00360 
00364     cPar& setLongValue(long l);
00365 
00371     cPar& setStringValue(const char *s);
00372 
00376     cPar& setDoubleValue(double d);
00377 
00383     cPar& setDoubleValue(cStatistic *res);
00384 
00390     cPar& setDoubleValue(sXElem *x, int n);
00391 
00396     cPar& setDoubleValue(MathFuncNoArg f);
00397 
00403     cPar& setDoubleValue(MathFunc1Arg  f, double p1);
00404 
00410     cPar& setDoubleValue(MathFunc2Args f, double p1, double p2);
00411 
00417     cPar& setDoubleValue(MathFunc3Args f, double p1, double p2, double p3);
00418 
00425     cPar& setPointerValue(void *ptr);
00426 
00432     cPar& setObjectValue(cObject *obj);
00433 
00453     void configPointer( VoidDelFunc delfunc, VoidDupFunc dupfunc, size_t itemsize=0);
00455 
00458 
00462     bool boolValue();
00463 
00469     long longValue();
00470 
00474     const char *stringValue();
00475 
00481     double doubleValue();
00482 
00486     void *pointerValue();
00487 
00491     cObject *objectValue();
00493 
00496 
00504     cPar& setRedirection(cPar *par);
00505 
00509     bool isRedirected() const {return typechar=='I';}
00510 
00518     cPar *redirection();
00519 
00523     void cancelRedirection();
00525 
00528 
00534     char type() const;
00535 
00539     bool isNumeric() const;
00540 
00544     const char *prompt() ;
00545 
00549     void setPrompt(const char *s);
00550 
00554     void setInput(bool ip);
00555 
00560     bool isInput() const;
00561 
00567     bool changed();
00569 
00572 
00576     cPar& read();
00577 
00582     void convertToConst();
00583 
00588     bool equalsTo(cPar *par);
00590 
00593 
00598     virtual void getAsText(char *buf, int maxlen);
00599 
00607     virtual bool setFromText(const char *text, char tp);
00609 
00612 
00616     cPar& operator=(bool b)          {return setBoolValue(b);}
00617 
00621     cPar& operator=(const char *s)   {return setStringValue(s);}
00622 
00626     cPar& operator=(char c)          {return setLongValue((long)c);}
00627 
00631     cPar& operator=(unsigned char c) {return setLongValue((long)c);}
00632 
00636     cPar& operator=(int i)           {return setLongValue((long)i);}
00637 
00641     cPar& operator=(unsigned int i)  {return setLongValue((long)i);}
00642 
00646     cPar& operator=(long l)          {return setLongValue(l);}
00647 
00651     cPar& operator=(unsigned long l) {return setLongValue((long)l);}
00652 
00656     cPar& operator=(double d)        {return setDoubleValue(d);}
00657 
00661     cPar& operator=(long double d)   {return setDoubleValue((double)d);}
00662 
00666     cPar& operator=(void *ptr)       {return setPointerValue(ptr);}
00667 
00671     cPar& operator=(cObject *obj)    {return setObjectValue(obj);}
00672 
00676     operator bool()          {return boolValue();}
00677 
00681     operator const char *()  {return stringValue();}
00682 
00686     operator char()          {return (char)longValue();}
00687 
00691     operator unsigned char() {return (unsigned char)longValue();}
00692 
00696     operator int()           {return (int)longValue();}
00697 
00701     operator unsigned int()  {return (unsigned int)longValue();}
00702 
00706     operator long()          {return longValue();}
00707 
00711     operator unsigned long() {return longValue();}
00712 
00716     operator double()        {return doubleValue();}
00717 
00721     operator long double()   {return doubleValue();}
00722 
00726     operator void *()        {return pointerValue();}
00727 
00731     operator cObject *()     {return objectValue();}
00733 
00736 
00742     static int cmpbyvalue(cObject *one, cObject *other);
00744 };
00745 
00746 // this function cannot be defined within sXElem because of declaration order
00747 inline void sXElem::operator=(cPar& _r)  {type='R'; p=(cPar *)_r.dup();}
00748 
00749 //==========================================================================
00750 
00759 class SIM_API cModulePar : public cPar
00760 {
00761     friend class cModule;
00762   private:
00763     cModule *omodp;              // owner module
00764 
00765   private:
00766     // helper function
00767     void _construct();
00768 
00769   public:
00772 
00776     cModulePar(const cPar& other);
00777 
00781     explicit cModulePar(const char *name=NULL);
00782 
00786     explicit cModulePar(const char *name, cPar& other);
00787 
00791     virtual ~cModulePar();
00792 
00797     cModulePar& operator=(const cModulePar& otherpar);
00799 
00802 
00806     virtual const char *className() const {return "cModulePar";}
00807 
00812     virtual cObject *dup() const  {return new cPar(*this);}
00813 
00818     virtual const char *inspectorFactoryName() const {return "cModuleParIFC";}
00819 
00823     virtual const char *fullPath() const;
00824 
00829     virtual const char *fullPath(char *buffer, int bufsize) const;
00831 
00834 
00838     void setOwnerModule(cModule *om)   {omodp=om;}
00839 
00843     cModule *ownerModule() const        {return omodp;}
00845 };
00846 
00847 
00848 //=== operators dealing with cPars
00849 //
00850 // These operators were removed -- see ChangeLog, Jan 17 2000 entry.
00851 //
00852 
00853 //#ifndef NO_CPAR_OPERATIONS
00854 //#ifdef  LONG_CPAR_OPERATIONS
00855 // inline int operator<(cPar& p, cPar& q)  {return (long)p<(long)q;}
00856 // inline int operator<(long d, cPar& p)   {return d<(long)p;}
00857 // inline int operator<(cPar& p, long d)   {return (long)p<d;}
00858 // inline int operator>(cPar& p, cPar& q)  {return (long)p>(long)q;}
00859 // inline int operator>(long d, cPar& p)   {return d>(long)p;}
00860 // inline int operator>(cPar& p, long d)   {return (long)p>d;}
00861 //
00862 // inline int operator<=(cPar& p, cPar& q) {return (long)p<=(long)q;}
00863 // inline int operator<=(long d, cPar& p)  {return d<=(long)p;}
00864 // inline int operator<=(cPar& p, long d)  {return (long)p<=d;}
00865 // inline int operator>=(cPar& p, cPar& q) {return (long)p>=(long)q;}
00866 // inline int operator>=(long d, cPar& p)  {return d>=(long)p;}
00867 // inline int operator>=(cPar& p, long d)  {return (long)p>=d;}
00868 //
00869 // inline long operator+(cPar& p, cPar& q) {return (long)p+(long)q;}
00870 // inline long operator+(long d, cPar& p)  {return d+(long)p;}
00871 // inline long operator+(cPar& p, long d)  {return (long)p+d;}
00872 // inline long operator-(cPar& p, cPar& q) {return (long)p-(long)q;}
00873 // inline long operator-(long d, cPar& p)  {return d-(long)p;}
00874 // inline long operator-(cPar& p, long d)  {return (long)p-d;}
00875 //
00876 // inline long operator*(cPar& p, cPar& q)  {return (long)p*(long)q;}
00877 // inline long operator*(long d, cPar& p)   {return d*(long)p;}
00878 // inline long operator*(cPar& p, long d)   {return (long)p*d;}
00879 // inline long operator/(cPar& p, cPar& q)  {return (long)p/(long)q;}
00880 // inline long operator/(long d, cPar& p)   {return d/(long)p;}
00881 // inline long operator/(cPar& p, long d)   {return (long)p/d;}
00882 //
00883 //#else
00884 //
00885 // inline int operator<(cPar& p, cPar& q)  {return (double)p<(double)q;}
00886 // inline int operator<(double d, cPar& p) {return d<(double)p;}
00887 // inline int operator<(cPar& p, double d) {return (double)p<d;}
00888 // inline int operator>(cPar& p, cPar& q)  {return (double)p>(double)q;}
00889 // inline int operator>(double d, cPar& p) {return d>(double)p;}
00890 // inline int operator>(cPar& p, double d) {return (double)p>d;}
00891 //
00892 // inline int operator<=(cPar& p, cPar& q)  {return (double)p<=(double)q;}
00893 // inline int operator<=(double d, cPar& p) {return d<=(double)p;}
00894 // inline int operator<=(cPar& p, double d) {return (double)p<=d;}
00895 // inline int operator>=(cPar& p, cPar& q)  {return (double)p>=(double)q;}
00896 // inline int operator>=(double d, cPar& p) {return d>=(double)p;}
00897 // inline int operator>=(cPar& p, double d) {return (double)p>=d;}
00898 //
00899 // inline double operator+(cPar& p, cPar& q)  {return (double)p+(double)q;}
00900 // inline double operator+(double d, cPar& p) {return d+(double)p;}
00901 // inline double operator+(cPar& p, double d) {return (double)p+d;}
00902 // inline double operator-(cPar& p, cPar& q)  {return (double)p-(double)q;}
00903 // inline double operator-(double d, cPar& p) {return d-(double)p;}
00904 // inline double operator-(cPar& p, double d) {return (double)p-d;}
00905 //
00906 // inline double operator*(cPar& p, cPar& q)  {return (double)p*(double)q;}
00907 // inline double operator*(double d, cPar& p) {return d*(double)p;}
00908 // inline double operator*(cPar& p, double d) {return (double)p*d;}
00909 // inline double operator/(cPar& p, cPar& q)  {return (double)p/(double)q;}
00910 // inline double operator/(double d, cPar& p) {return d/(double)p;}
00911 // inline double operator/(cPar& p, double d) {return (double)p/d;}
00912 //#endif
00913 //#endif
00914 
00915 #endif
00916 
00917 

Generated at Sat May 4 15:45:48 2002 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001