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 
00150 
00155         void operator=(short _i)            {type='D'; d=_i;  }
00156 
00161         void operator=(long _l)           {type='D'; d=_l;  }
00162 
00167         void operator=(double _d)         {type='D'; d=_d;  }
00168 
00176         void operator=(cPar *_p)          {type='P'; p=_p;  }
00177 
00184         void operator=(cPar& _r);         //{type='R'; p=(cPar *)_r.dup();} See after cPar!
00185 
00195         void operator=(MathFuncNoArg _f)  {type='0'; f0=_f;}
00196 
00208         void operator=(MathFunc1Arg  _f)  {type='1'; f1=_f;}
00209 
00222         void operator=(MathFunc2Args _f)  {type='2'; f2=_f;}
00223 
00236         void operator=(MathFunc3Args _f)  {type='3'; f3=_f;}
00237 
00250         void operator=(MathFunc4Args _f)  {type='4'; f4=_f;}
00251 
00265         void operator=(char _op)          {type='@'; op=_op;}
00266     };
00267 
00268   protected:
00269     static char *possibletypes;
00270   private:
00271     char typechar;     // S/B/L/D/F/T/X/C/P/O/I
00272     bool inputflag;
00273     bool changedflag;
00274     bool tkownership;
00275     opp_string promptstr; // prompt text used when the value is being input
00276 
00277     union {    // Take care of 'operator=()' when changing this!!!
00278        struct { bool sht; char *str;            } ls;   // S:long string
00279        struct { bool sht; char str[SHORTSTR+1]; } ss;   // S:short str
00280        struct { long val;                       } lng;  // L:long,B:bool
00281        struct { double val;                     } dbl;  // D:double
00282        struct { MathFunc f; int argc;
00283                 double p1,p2,p3,p4;             } func; // F:math function
00284        struct { cStatistic *res;                } dtr;  // T:distribution
00285        struct { cDoubleExpression *expr;        } cexpr;// C:compiled expression
00286        struct { ExprElem *xelem; int n;         } expr; // X:expression
00287        struct { cPar *par;                      } ind;  // I:indirection
00288        struct { void *ptr;
00289                 VoidDelFunc delfunc;
00290                 VoidDupFunc dupfunc;
00291                 size_t itemsize;                } ptr;  // P:void* pointer
00292        struct { cObject *obj;                   } obj;  // O:object pointer
00293        struct { cXMLElement *node;              } xmlp; // M:XML element pointer
00294     };
00295 
00296   private:
00297     // helper func: destruct old value
00298     void deleteold();
00299 
00300     // helper func: evaluates expression (X)
00301     double evaluate();
00302 
00303     // helper func: rand.num with given distr.(T)
00304     double fromstat();
00305 
00306     // setFromText() helper func.
00307     bool setfunction(char *w);
00308 
00309   protected:
00312 
00318     virtual void beforeChange();
00319 
00325     virtual void afterChange();
00327 
00328   public:
00331 
00335     cPar(const cPar& other);
00336 
00341     explicit cPar(const char *name=NULL);
00342 
00347     explicit cPar(const char *name, cPar& other);
00348 
00352     virtual ~cPar();
00353 
00365     cPar& operator=(const cPar& otherpar);
00367 
00370 
00375     virtual cPolymorphic *dup() const   {return new cPar(*this);}
00376 
00381     virtual std::string info() const;
00382 
00387     virtual void writeContents(std::ostream& os);
00388 
00393     virtual void forEachChild(cVisitor *v);
00394 
00400     virtual void netPack(cCommBuffer *buffer);
00401 
00407     virtual void netUnpack(cCommBuffer *buffer);
00409 
00412 
00416     cPar& setBoolValue(bool b);
00417 
00421     cPar& setLongValue(long l);
00422 
00428     cPar& setStringValue(const char *s);
00429 
00433     cPar& setDoubleValue(double d);
00434 
00440     cPar& setDoubleValue(cStatistic *res);
00441 
00462     cPar& setDoubleValue(ExprElem *x, int n);
00463 
00472     cPar& setDoubleValue(cDoubleExpression *expr);
00473 
00478     cPar& setDoubleValue(MathFuncNoArg f);
00479 
00485     cPar& setDoubleValue(MathFunc1Arg  f, double p1);
00486 
00492     cPar& setDoubleValue(MathFunc2Args f, double p1, double p2);
00493 
00499     cPar& setDoubleValue(MathFunc3Args f, double p1, double p2, double p3);
00500 
00506     cPar& setDoubleValue(MathFunc4Args f, double p1, double p2, double p3, double p4);
00507 
00514     cPar& setPointerValue(void *ptr);
00515 
00520     cPar& setObjectValue(cObject *obj);
00521 
00525     cPar& setXMLValue(cXMLElement *node);
00526 
00546     void configPointer( VoidDelFunc delfunc, VoidDupFunc dupfunc, size_t itemsize=0);
00547 
00553     void takeOwnership(bool tk) {tkownership=tk;}
00554 
00558     bool takeOwnership() const   {return tkownership;}
00560 
00563 
00567     bool boolValue();
00568 
00574     long longValue();
00575 
00579     const char *stringValue();
00580 
00586     double doubleValue();
00587 
00591     void *pointerValue();
00592 
00596     cObject *objectValue();
00597 
00601     cXMLElement *xmlValue();
00603 
00606 
00614     cPar& setRedirection(cPar *par);
00615 
00619     bool isRedirected() const {return typechar=='I';}
00620 
00628     cPar *redirection();
00629 
00633     void cancelRedirection();
00635 
00638 
00644     char type() const;
00645 
00649     bool isNumeric() const;
00650 
00656     bool isConstant() const;
00657 
00661     const char *prompt() ;
00662 
00666     void setPrompt(const char *s);
00667 
00671     void setInput(bool ip);
00672 
00677     bool isInput() const;
00678 
00684     bool changed();
00686 
00689 
00693     cPar& read();
00694 
00699     void convertToConst();
00700 
00705     bool equalsTo(cPar *par);
00707 
00710 
00714     virtual std::string getAsText() const;
00715 
00723     virtual bool setFromText(const char *text, char type='?');
00725 
00728 
00732     cPar& operator=(bool b)          {return setBoolValue(b);}
00733 
00737     cPar& operator=(const char *s)   {return setStringValue(s);}
00738 
00742     cPar& operator=(char c)          {return setLongValue((long)c);}
00743 
00747     cPar& operator=(unsigned char c) {return setLongValue((long)c);}
00748 
00752     cPar& operator=(int i)           {return setLongValue((long)i);}
00753 
00757     cPar& operator=(unsigned int i)  {return setLongValue((long)i);}
00758 
00762     cPar& operator=(short i)  {return setLongValue((long)i);}
00763 
00767     cPar& operator=(unsigned short i)  {return setLongValue((long)i);}
00768 
00772     cPar& operator=(long l)          {return setLongValue(l);}
00773 
00777     cPar& operator=(unsigned long l) {return setLongValue((long)l);}
00778 
00782     cPar& operator=(double d)        {return setDoubleValue(d);}
00783 
00787     cPar& operator=(long double d)   {return setDoubleValue((double)d);}
00788 
00792     cPar& operator=(void *ptr)       {return setPointerValue(ptr);}
00793 
00797     cPar& operator=(cObject *obj)    {return setObjectValue(obj);}
00798 
00802     cPar& operator=(cXMLElement *node) {return setXMLValue(node);}
00803 
00807     operator bool()          {return boolValue();}
00808 
00812     operator const char *()  {return stringValue();}
00813 
00817     operator char()          {return (char)longValue();}
00818 
00822     operator unsigned char() {return (unsigned char)longValue();}
00823 
00827     operator int()           {return (int)longValue();}
00828 
00832     operator unsigned int()  {return (unsigned int)longValue();}
00833 
00837     operator short()  {return (short)longValue();}
00838 
00842     operator unsigned short()  {return (unsigned short)longValue();}
00843 
00847     operator long()          {return longValue();}
00848 
00852     operator unsigned long() {return longValue();}
00853 
00857     operator double()        {return doubleValue();}
00858 
00862     operator long double()   {return doubleValue();}
00863 
00867     operator void *()        {return pointerValue();}
00868 
00872     operator cObject *()     {return objectValue();}
00873 
00877     operator cXMLElement *() {return xmlValue();}
00878 
00880 
00883 
00889     static int cmpbyvalue(cObject *one, cObject *other);
00891 };
00892 
00893 // this function cannot be defined within ExprElem because of declaration order
00894 inline void cPar::ExprElem::operator=(cPar& _r)  {type='R'; p=(cPar *)_r.dup();}
00895 
00896 //==========================================================================
00897 
00906 class SIM_API cModulePar : public cPar
00907 {
00908     friend class cModule;
00909   private:
00910     cModule *omodp;              // owner module
00911 
00912   private:
00913     // helper function
00914     void _construct();
00915 
00916   public:
00919 
00923     cModulePar(const cPar& other);
00924 
00928     explicit cModulePar(const char *name=NULL);
00929 
00933     explicit cModulePar(const char *name, cPar& other);
00934 
00938     virtual ~cModulePar();
00939 
00944     cModulePar& operator=(const cModulePar& otherpar);
00946 
00949 
00954     virtual cPolymorphic *dup() const  {return new cPar(*this);}
00955 
00959     virtual std::string fullPath() const;
00960 
00965     virtual const char *fullPath(char *buffer, int bufsize) const;
00967 
00973     virtual void afterChange();
00975 
00978 
00982     void setOwnerModule(cModule *om)   {omodp=om;}
00983 
00987     cModule *ownerModule() const        {return omodp;}
00989 };
00990 
00991 inline std::ostream& operator<< (std::ostream& os, const cPar& o) {
00992     return os << o.getAsText();
00993 }
00994 
00995 inline std::ostream& operator<< (std::ostream& os, cPar& o) {
00996     return os << o.getAsText();
00997 }
00998 
00999 
01000 //=== operators dealing with cPars
01001 //
01002 // These operators were removed -- see ChangeLog, Jan 17 2000 entry.
01003 //
01004 
01005 //#ifndef NO_CPAR_OPERATIONS
01006 //#ifdef  LONG_CPAR_OPERATIONS
01007 // inline int operator<(cPar& p, cPar& q)  {return (long)p<(long)q;}
01008 // inline int operator<(long d, cPar& p)   {return d<(long)p;}
01009 // inline int operator<(cPar& p, long d)   {return (long)p<d;}
01010 // inline int operator>(cPar& p, cPar& q)  {return (long)p>(long)q;}
01011 // inline int operator>(long d, cPar& p)   {return d>(long)p;}
01012 // inline int operator>(cPar& p, long d)   {return (long)p>d;}
01013 //
01014 // inline int operator<=(cPar& p, cPar& q) {return (long)p<=(long)q;}
01015 // inline int operator<=(long d, cPar& p)  {return d<=(long)p;}
01016 // inline int operator<=(cPar& p, long d)  {return (long)p<=d;}
01017 // inline int operator>=(cPar& p, cPar& q) {return (long)p>=(long)q;}
01018 // inline int operator>=(long d, cPar& p)  {return d>=(long)p;}
01019 // inline int operator>=(cPar& p, long d)  {return (long)p>=d;}
01020 //
01021 // inline long operator+(cPar& p, cPar& q) {return (long)p+(long)q;}
01022 // inline long operator+(long d, cPar& p)  {return d+(long)p;}
01023 // inline long operator+(cPar& p, long d)  {return (long)p+d;}
01024 // inline long operator-(cPar& p, cPar& q) {return (long)p-(long)q;}
01025 // inline long operator-(long d, cPar& p)  {return d-(long)p;}
01026 // inline long operator-(cPar& p, long d)  {return (long)p-d;}
01027 //
01028 // inline long operator*(cPar& p, cPar& q)  {return (long)p*(long)q;}
01029 // inline long operator*(long d, cPar& p)   {return d*(long)p;}
01030 // inline long operator*(cPar& p, long d)   {return (long)p*d;}
01031 // inline long operator/(cPar& p, cPar& q)  {return (long)p/(long)q;}
01032 // inline long operator/(long d, cPar& p)   {return d/(long)p;}
01033 // inline long operator/(cPar& p, long d)   {return (long)p/d;}
01034 //
01035 //#else
01036 //
01037 // inline int operator<(cPar& p, cPar& q)  {return (double)p<(double)q;}
01038 // inline int operator<(double d, cPar& p) {return d<(double)p;}
01039 // inline int operator<(cPar& p, double d) {return (double)p<d;}
01040 // inline int operator>(cPar& p, cPar& q)  {return (double)p>(double)q;}
01041 // inline int operator>(double d, cPar& p) {return d>(double)p;}
01042 // inline int operator>(cPar& p, double d) {return (double)p>d;}
01043 //
01044 // inline int operator<=(cPar& p, cPar& q)  {return (double)p<=(double)q;}
01045 // inline int operator<=(double d, cPar& p) {return d<=(double)p;}
01046 // inline int operator<=(cPar& p, double d) {return (double)p<=d;}
01047 // inline int operator>=(cPar& p, cPar& q)  {return (double)p>=(double)q;}
01048 // inline int operator>=(double d, cPar& p) {return d>=(double)p;}
01049 // inline int operator>=(cPar& p, double d) {return (double)p>=d;}
01050 //
01051 // inline double operator+(cPar& p, cPar& q)  {return (double)p+(double)q;}
01052 // inline double operator+(double d, cPar& p) {return d+(double)p;}
01053 // inline double operator+(cPar& p, double d) {return (double)p+d;}
01054 // inline double operator-(cPar& p, cPar& q)  {return (double)p-(double)q;}
01055 // inline double operator-(double d, cPar& p) {return d-(double)p;}
01056 // inline double operator-(cPar& p, double d) {return (double)p-d;}
01057 //
01058 // inline double operator*(cPar& p, cPar& q)  {return (double)p*(double)q;}
01059 // inline double operator*(double d, cPar& p) {return d*(double)p;}
01060 // inline double operator*(cPar& p, double d) {return (double)p*d;}
01061 // inline double operator/(cPar& p, cPar& q)  {return (double)p/(double)q;}
01062 // inline double operator/(double d, cPar& p) {return d/(double)p;}
01063 // inline double operator/(cPar& p, double d) {return (double)p/d;}
01064 //#endif
01065 //#endif
01066 
01067 #endif
01068 
01069 

Generated on Sat Oct 21 17:47:55 2006 for OMNeT++/OMNEST Simulation Library by  doxygen 1.4.6