Main Page | Modules | Class Hierarchy | Class List | File List | Class Members | Related Pages

cpar.h

00001 //==========================================================================
00002 //   CPAR.H  - part of
00003 //                     OMNeT++/OMNEST
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-2005 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 class  cXMLElement;
00035 
00036 
00037 //==========================================================================
00038 
00044 class SIM_API cExpression
00045 {
00046   public:
00047     virtual ~cExpression() {}
00048     virtual std::string getAsText() = 0;
00049     virtual bool parseText(const char *text) = 0;
00050     virtual cExpression *dup() = 0;
00051 };
00052 
00078 class SIM_API cDoubleExpression : public cExpression
00079 {
00080   public:
00081     virtual std::string getAsText();
00082     virtual bool parseText(const char *text);
00083     virtual double evaluate() = 0;
00084 };
00085 
00086 //==========================================================================
00087 
00116 class SIM_API cPar : public cObject
00117 {
00118   public:
00123     struct ExprElem
00124     {
00125         // Type chars:
00126         //   D  double constant
00127         //   P  pointer to "external" cPar (owned by someone else)
00128         //   R  "reference": the cPar will be dup()'ped and the copy kept
00129         //   0/1/2/3  function with 0/1/2/3 arguments
00130         //   @  math operator (+-*%/^=!<{>}?); see cPar::evaluate()
00131         //
00132         char type;    // D/P/R/0/1/2/3/@ (see above)
00133         union {
00134             double d;           // D
00135             cPar *p;            // P/R
00136             MathFuncNoArg f0;   // 0
00137             MathFunc1Arg  f1;   // 1
00138             MathFunc2Args f2;   // 2
00139             MathFunc3Args f3;   // 3
00140             MathFunc4Args f4;   // 4
00141             char op;            // @, op = +-*/%^=!<{>}?
00142         };
00143 
00148         void operator=(int _i)            {type='D'; d=_i;  }
00149 
00154         void operator=(long _l)           {type='D'; d=_l;  }
00155 
00160         void operator=(double _d)         {type='D'; d=_d;  }
00161 
00169         void operator=(cPar *_p)          {type='P'; p=_p;  }
00170 
00177         void operator=(cPar& _r);         //{type='R'; p=(cPar *)_r.dup();} See after cPar!
00178 
00188         void operator=(MathFuncNoArg _f)  {type='0'; f0=_f;}
00189 
00201         void operator=(MathFunc1Arg  _f)  {type='1'; f1=_f;}
00202 
00215         void operator=(MathFunc2Args _f)  {type='2'; f2=_f;}
00216 
00229         void operator=(MathFunc3Args _f)  {type='3'; f3=_f;}
00230 
00243         void operator=(MathFunc4Args _f)  {type='4'; f4=_f;}
00244 
00258         void operator=(char _op)          {type='@'; op=_op;}
00259     };
00260 
00261   protected:
00262     static char *possibletypes;
00263   private:
00264     char typechar;     // S/B/L/D/F/T/X/C/P/O/I
00265     bool inputflag;
00266     bool changedflag;
00267     bool tkownership;
00268     opp_string promptstr; // prompt text used when the value is being input
00269 
00270     union {    // Take care of 'operator=()' when changing this!!!
00271        struct { bool sht; char *str;            } ls;   // S:long string
00272        struct { bool sht; char str[SHORTSTR+1]; } ss;   // S:short str
00273        struct { long val;                       } lng;  // L:long,B:bool
00274        struct { double val;                     } dbl;  // D:double
00275        struct { MathFunc f; int argc;
00276                 double p1,p2,p3,p4;             } func; // F:math function
00277        struct { cStatistic *res;                } dtr;  // T:distribution
00278        struct { cDoubleExpression *expr;        } cexpr;// C:compiled expression
00279        struct { ExprElem *xelem; int n;         } expr; // X:expression
00280        struct { cPar *par;                      } ind;  // I:indirection
00281        struct { void *ptr;
00282                 VoidDelFunc delfunc;
00283                 VoidDupFunc dupfunc;
00284                 size_t itemsize;                } ptr;  // P:void* pointer
00285        struct { cObject *obj;                   } obj;  // O:object pointer
00286        struct { cXMLElement *node;              } xmlp; // M:XML element pointer
00287     };
00288 
00289   private:
00290     // helper func: destruct old value
00291     void deleteold();
00292 
00293     // helper func: evaluates expression (X)
00294     double evaluate();
00295 
00296     // helper func: rand.num with given distr.(T)
00297     double fromstat();
00298 
00299     // setFromText() helper func.
00300     bool setfunction(char *w);
00301 
00302   protected:
00305 
00311     virtual void beforeChange();
00312 
00318     virtual void afterChange();
00320 
00321   public:
00324 
00328     cPar(const cPar& other);
00329 
00334     explicit cPar(const char *name=NULL);
00335 
00340     explicit cPar(const char *name, cPar& other);
00341 
00345     virtual ~cPar();
00346 
00358     cPar& operator=(const cPar& otherpar);
00360 
00363 
00368     virtual cObject *dup() const   {return new cPar(*this);}
00369 
00374     virtual std::string info() const;
00375 
00380     virtual void writeContents(std::ostream& os);
00381 
00386     virtual void forEachChild(cVisitor *v);
00387 
00393     virtual void netPack(cCommBuffer *buffer);
00394 
00400     virtual void netUnpack(cCommBuffer *buffer);
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 
00455     cPar& setDoubleValue(ExprElem *x, int n);
00456 
00465     cPar& setDoubleValue(cDoubleExpression *expr);
00466 
00471     cPar& setDoubleValue(MathFuncNoArg f);
00472 
00478     cPar& setDoubleValue(MathFunc1Arg  f, double p1);
00479 
00485     cPar& setDoubleValue(MathFunc2Args f, double p1, double p2);
00486 
00492     cPar& setDoubleValue(MathFunc3Args f, double p1, double p2, double p3);
00493 
00499     cPar& setDoubleValue(MathFunc4Args f, double p1, double p2, double p3, double p4);
00500 
00507     cPar& setPointerValue(void *ptr);
00508 
00513     cPar& setObjectValue(cObject *obj);
00514 
00518     cPar& setXMLValue(cXMLElement *node);
00519 
00539     void configPointer( VoidDelFunc delfunc, VoidDupFunc dupfunc, size_t itemsize=0);
00540 
00546     void takeOwnership(bool tk) {tkownership=tk;}
00547 
00551     bool takeOwnership() const   {return tkownership;}
00553 
00556 
00560     bool boolValue();
00561 
00567     long longValue();
00568 
00572     const char *stringValue();
00573 
00579     double doubleValue();
00580 
00584     void *pointerValue();
00585 
00589     cObject *objectValue();
00590 
00594     cXMLElement *xmlValue();
00596 
00599 
00607     cPar& setRedirection(cPar *par);
00608 
00612     bool isRedirected() const {return typechar=='I';}
00613 
00621     cPar *redirection();
00622 
00626     void cancelRedirection();
00628 
00631 
00637     char type() const;
00638 
00642     bool isNumeric() const;
00643 
00647     const char *prompt() ;
00648 
00652     void setPrompt(const char *s);
00653 
00657     void setInput(bool ip);
00658 
00663     bool isInput() const;
00664 
00670     bool changed();
00672 
00675 
00679     cPar& read();
00680 
00685     void convertToConst();
00686 
00691     bool equalsTo(cPar *par);
00693 
00696 
00701     virtual std::string getAsText() const;
00702 
00710     virtual bool setFromText(const char *text, char tp);
00712 
00715 
00719     cPar& operator=(bool b)          {return setBoolValue(b);}
00720 
00724     cPar& operator=(const char *s)   {return setStringValue(s);}
00725 
00729     cPar& operator=(char c)          {return setLongValue((long)c);}
00730 
00734     cPar& operator=(unsigned char c) {return setLongValue((long)c);}
00735 
00739     cPar& operator=(int i)           {return setLongValue((long)i);}
00740 
00744     cPar& operator=(unsigned int i)  {return setLongValue((long)i);}
00745 
00749     cPar& operator=(long l)          {return setLongValue(l);}
00750 
00754     cPar& operator=(unsigned long l) {return setLongValue((long)l);}
00755 
00759     cPar& operator=(double d)        {return setDoubleValue(d);}
00760 
00764     cPar& operator=(long double d)   {return setDoubleValue((double)d);}
00765 
00769     cPar& operator=(void *ptr)       {return setPointerValue(ptr);}
00770 
00774     cPar& operator=(cObject *obj)    {return setObjectValue(obj);}
00775 
00779     cPar& operator=(cXMLElement *node) {return setXMLValue(node);}
00780 
00784     operator bool()          {return boolValue();}
00785 
00789     operator const char *()  {return stringValue();}
00790 
00794     operator char()          {return (char)longValue();}
00795 
00799     operator unsigned char() {return (unsigned char)longValue();}
00800 
00804     operator int()           {return (int)longValue();}
00805 
00809     operator unsigned int()  {return (unsigned int)longValue();}
00810 
00814     operator long()          {return longValue();}
00815 
00819     operator unsigned long() {return longValue();}
00820 
00824     operator double()        {return doubleValue();}
00825 
00829     operator long double()   {return doubleValue();}
00830 
00834     operator void *()        {return pointerValue();}
00835 
00839     operator cObject *()     {return objectValue();}
00840 
00844     operator cXMLElement *() {return xmlValue();}
00845 
00847 
00850 
00856     static int cmpbyvalue(cObject *one, cObject *other);
00858 };
00859 
00860 // this function cannot be defined within ExprElem because of declaration order
00861 inline void cPar::ExprElem::operator=(cPar& _r)  {type='R'; p=(cPar *)_r.dup();}
00862 
00863 //==========================================================================
00864 
00873 class SIM_API cModulePar : public cPar
00874 {
00875     friend class cModule;
00876   private:
00877     cModule *omodp;              // owner module
00878 
00879   private:
00880     // helper function
00881     void _construct();
00882 
00883   public:
00886 
00890     cModulePar(const cPar& other);
00891 
00895     explicit cModulePar(const char *name=NULL);
00896 
00900     explicit cModulePar(const char *name, cPar& other);
00901 
00905     virtual ~cModulePar();
00906 
00911     cModulePar& operator=(const cModulePar& otherpar);
00913 
00916 
00921     virtual cObject *dup() const  {return new cPar(*this);}
00922 
00926     virtual std::string fullPath() const;
00927 
00932     virtual const char *fullPath(char *buffer, int bufsize) const;
00934 
00937 
00941     void setOwnerModule(cModule *om)   {omodp=om;}
00942 
00946     cModule *ownerModule() const        {return omodp;}
00948 };
00949 
00950 inline std::ostream& operator<< (std::ostream& os, const cPar& o) {
00951     return os << o.getAsText();
00952 }
00953 
00954 inline std::ostream& operator<< (std::ostream& os, cPar& o) {
00955     return os << o.getAsText();
00956 }
00957 
00958 
00959 //=== operators dealing with cPars
00960 //
00961 // These operators were removed -- see ChangeLog, Jan 17 2000 entry.
00962 //
00963 
00964 //#ifndef NO_CPAR_OPERATIONS
00965 //#ifdef  LONG_CPAR_OPERATIONS
00966 // inline int operator<(cPar& p, cPar& q)  {return (long)p<(long)q;}
00967 // inline int operator<(long d, cPar& p)   {return d<(long)p;}
00968 // inline int operator<(cPar& p, long d)   {return (long)p<d;}
00969 // inline int operator>(cPar& p, cPar& q)  {return (long)p>(long)q;}
00970 // inline int operator>(long d, cPar& p)   {return d>(long)p;}
00971 // inline int operator>(cPar& p, long d)   {return (long)p>d;}
00972 //
00973 // inline int operator<=(cPar& p, cPar& q) {return (long)p<=(long)q;}
00974 // inline int operator<=(long d, cPar& p)  {return d<=(long)p;}
00975 // inline int operator<=(cPar& p, long d)  {return (long)p<=d;}
00976 // inline int operator>=(cPar& p, cPar& q) {return (long)p>=(long)q;}
00977 // inline int operator>=(long d, cPar& p)  {return d>=(long)p;}
00978 // inline int operator>=(cPar& p, long d)  {return (long)p>=d;}
00979 //
00980 // inline long operator+(cPar& p, cPar& q) {return (long)p+(long)q;}
00981 // inline long operator+(long d, cPar& p)  {return d+(long)p;}
00982 // inline long operator+(cPar& p, long d)  {return (long)p+d;}
00983 // inline long operator-(cPar& p, cPar& q) {return (long)p-(long)q;}
00984 // inline long operator-(long d, cPar& p)  {return d-(long)p;}
00985 // inline long operator-(cPar& p, long d)  {return (long)p-d;}
00986 //
00987 // inline long operator*(cPar& p, cPar& q)  {return (long)p*(long)q;}
00988 // inline long operator*(long d, cPar& p)   {return d*(long)p;}
00989 // inline long operator*(cPar& p, long d)   {return (long)p*d;}
00990 // inline long operator/(cPar& p, cPar& q)  {return (long)p/(long)q;}
00991 // inline long operator/(long d, cPar& p)   {return d/(long)p;}
00992 // inline long operator/(cPar& p, long d)   {return (long)p/d;}
00993 //
00994 //#else
00995 //
00996 // inline int operator<(cPar& p, cPar& q)  {return (double)p<(double)q;}
00997 // inline int operator<(double d, cPar& p) {return d<(double)p;}
00998 // inline int operator<(cPar& p, double d) {return (double)p<d;}
00999 // inline int operator>(cPar& p, cPar& q)  {return (double)p>(double)q;}
01000 // inline int operator>(double d, cPar& p) {return d>(double)p;}
01001 // inline int operator>(cPar& p, double d) {return (double)p>d;}
01002 //
01003 // inline int operator<=(cPar& p, cPar& q)  {return (double)p<=(double)q;}
01004 // inline int operator<=(double d, cPar& p) {return d<=(double)p;}
01005 // inline int operator<=(cPar& p, double d) {return (double)p<=d;}
01006 // inline int operator>=(cPar& p, cPar& q)  {return (double)p>=(double)q;}
01007 // inline int operator>=(double d, cPar& p) {return d>=(double)p;}
01008 // inline int operator>=(cPar& p, double d) {return (double)p>=d;}
01009 //
01010 // inline double operator+(cPar& p, cPar& q)  {return (double)p+(double)q;}
01011 // inline double operator+(double d, cPar& p) {return d+(double)p;}
01012 // inline double operator+(cPar& p, double d) {return (double)p+d;}
01013 // inline double operator-(cPar& p, cPar& q)  {return (double)p-(double)q;}
01014 // inline double operator-(double d, cPar& p) {return d-(double)p;}
01015 // inline double operator-(cPar& p, double d) {return (double)p-d;}
01016 //
01017 // inline double operator*(cPar& p, cPar& q)  {return (double)p*(double)q;}
01018 // inline double operator*(double d, cPar& p) {return d*(double)p;}
01019 // inline double operator*(cPar& p, double d) {return (double)p*d;}
01020 // inline double operator/(cPar& p, cPar& q)  {return (double)p/(double)q;}
01021 // inline double operator/(double d, cPar& p) {return d/(double)p;}
01022 // inline double operator/(cPar& p, double d) {return (double)p/d;}
01023 //#endif
01024 //#endif
01025 
01026 #endif
01027 
01028 

Generated on Fri Apr 1 12:25:32 2005 for OMNeT++/OMNEST Simulation Library by  doxygen 1.3.9.1