detime.h

Go to the documentation of this file.
00001 
00007 /*
00008   $Id: detime.h,v 1.3 2004/12/12 23:44:44 pecos Exp $
00009 */
00010 
00011 /* *********
00012 *  
00013 *  This file is part of:
00014 *  NePSing, Network Protocol Simulator next generation
00015 *  
00016 *  Copyright (C) 2004  Tommaso Pecorella <tpecorella@mac.com>
00017 *  
00018 *  This library is free software; you can redistribute it and/or
00019 *  modify it under the terms of the GNU Lesser General Public
00020 *  License as published by the Free Software Foundation; either
00021 *  version 2.1 of the License, or (at your option) any later version.
00022 *  
00023 *  This library is distributed in the hope that it will be useful,
00024 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00025 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00026 *  Lesser General Public License for more details.
00027 *  
00028 *  You should have received a copy of the GNU Lesser General Public
00029 *  License along with this library; if not, write to the Free Software
00030 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00031 *  
00032 ********* */
00033 
00034 #ifndef ___DETIME_H___
00035 #define ___DETIME_H___
00036 
00037 #include <iostream>
00038 #include <iomanip>
00039 #include <string>
00040 #include <math.h>
00041 
00042 #include "parameter.h"
00043 
00044 #define ULL_MAX  ~0ULL
00045 
00046 #define PRINT_ABS_TIME          1
00047 #define PRINT_ABS_SUPERSTEP     2
00048 #define PRINT_ABS_BIGSTEP       4
00049 #define PRINT_ABS_STEP          8
00050 #define PRINT_REL_BIGSTEP      16
00051 #define PRINT_REL_STEP         32
00052 #define PRINT_OFFSET           64
00053 #define PRINT_PARENTHESIS     128
00054 
00056 typedef unsigned long long int time_int;
00057 
00059 
00085 class DETime
00086 {
00087 protected:
00088 
00089   //  The number of ticks. Each tick represent a fraction of a second, depending on TimeResolution.
00090   time_int tick;
00091 
00092   //  overflow flag, set if the time is above the maximum representable time.
00093   bool overflow;
00094 
00095   //  Describes how to print this time.
00096   /*
00097    \internal 
00098    PrintMask: bitmask representing the printing format of timestamps.
00099    
00100    PRINT_ABS_TIME               1 : Absolute time
00101    PRINT_ABS_SUPERSTEP          2 : Absolute SuperStep
00102    PRINT_ABS_BIGSTEP            4 : Absolute BigStep
00103    PRINT_ABS_STEP               8 : Absolute Step
00104    PRINT_REL_BIGSTEP           16 : BigStep position in SuperStep
00105    PRINT_REL_STEP              32 : Step position in BigStep
00106    PRINT_OFFSET                64 : Offset
00107    PRINT_PARENTHESIS          128 : Encloses in parenthesis
00108    */
00109   int PrintMask;
00110 
00111   //   The Time Resolution; es: -3 == milliseconds (\f$10^{-3}\f$ seconds).
00112   static int TickSize;
00113 
00114   //   The size of the step, expressed in ticks.
00115   static time_int TicksPerStep;
00116 
00117   //  Number of Ticks in each BigStep.
00118   static time_int TicksPerBigStep;
00119 
00120   //  Number of Ticks in each SuperStep.
00121   static time_int TicksPerSuperStep;
00122 
00123   //  Number of Steps in each BigStep.
00124   static time_int StepsPerBigStep;
00125 
00126   //  Number of BigSteps in each SuperStep.
00127   static time_int BigStepsPerSuperStep;
00128 
00129   //  The default value assigned to printmask.
00130   static int DefaultPrintMask;
00131 
00132   //  Prints the time represented to \c os.
00138   virtual void print(ostream &os) const;
00139 
00140 public:
00141 
00142   //  Main setup function. Used once at the simulation begin, it MUST be never called after.
00151   static void setup(int _TimeResolution,
00152              time_int _TicksPerStep,
00153          time_int _StepsPerBigStep,
00154          time_int _BigStepsPerSuperStep,
00155          int _DefaultPrintMask = DefaultPrintMask);
00156 
00157   //  constructor used to set up the \c overflow bit too. Do NOT use outside detime.cpp.
00158   DETime(time_int _tick,
00159          bool _overflow,
00160          int _PrintMask = PRINT_ABS_TIME);
00161 
00163   DETime();
00164 
00166   DETime(string A);
00167 
00169   DETime(time_int _tick,
00170          int _PrintMask = DefaultPrintMask);
00171 
00173   DETime(const DETime& A);
00174 
00176   DETime( time_int _step,
00177           time_int _bigStep,
00178           time_int _superStep );
00179 
00181   DETime( double A, int _PrintMask = DefaultPrintMask);
00182   
00184   time_int Tick() const { return tick; };
00186   time_int Step() const { return tick / TicksPerStep; };
00188   time_int BigStep() const { return tick / TicksPerBigStep; };
00190   time_int SuperStep() const { return tick / TicksPerSuperStep; };
00191 
00193   time_int RelativeTick()  const { return tick % TicksPerStep; };
00195   time_int RelativeStep()  const { return (tick % TicksPerBigStep)/TicksPerStep; };
00197   time_int RelativeBigStep() const { return (tick % TicksPerSuperStep)/TicksPerBigStep; };
00198 
00200   DETime operator = (const DETime& A);
00201 
00203   DETime operator + (const DETime& A) const;
00205   DETime operator + (const time_int A) const;
00207   DETime operator + (const double A) const { return (*this) + DETime(A); };
00208   
00210   DETime operator += (const DETime& A);
00212   DETime operator += (const time_int A);
00213   
00215   DETime operator - (const DETime& A) const;
00217   DETime operator - (const time_int A) const;
00218 
00219   // Other operators
00220   // WARNING: necessary to test overflow on *
00221   //          the test assumes unsigned long long has 64 bits
00222   
00224   DETime operator * (const time_int A) const;
00226   DETime operator * (const double A) const;
00227 
00229   DETime operator / (const time_int A) const { return(DETime( tick/A, false, PrintMask ) ); };
00231   const time_int operator / (const DETime &A) const { return tick/A.tick; };
00232   
00234   time_int operator % (const time_int A) const { return( tick % A ); };
00235 
00237   bool operator < (const DETime& A) const   { return (tick < A.tick); };
00239   bool operator > (const DETime& A) const   { return (tick > A.tick); };
00241   bool operator <= (const DETime& A) const  { return (tick <= A.tick); };
00243   bool operator >= (const DETime& A) const  { return (tick >= A.tick); };
00245   bool operator == (const DETime& A) const  { return (tick == A.tick); };
00247   bool operator != (const DETime& A) const  { return (tick != A.tick); };
00248 
00249   // returns the time representing the end of the Step
00250   //
00251   // Not needed, same as NextStep()
00252   // DETime EndOfStep();
00253 
00254 /***************************************************************
00255 *                                                              *
00256 *     Some funtions are overloaded to allow efficient coding   *
00257 *                                                              *
00258 ***************************************************************/
00259 
00261   DETime AddStep() const;   
00263   DETime AddStep(time_int S) const;
00264 
00266   DETime AddBigStep() const;
00268   DETime AddBigStep(time_int BS) const;
00269 
00271   DETime AddSuperStep() const;
00273   DETime AddSuperStep(time_int SS) const;
00274 
00276   DETime NextStep() const;
00278   DETime NextStep(time_int S) const;
00279 
00281   DETime NextBigStep() const;
00283   DETime NextBigStep(time_int BS) const;
00284 
00286   DETime NextSuperStep() const;
00288   DETime NextSuperStep(time_int SS) const;
00289 
00291   DETime NextIndexedStep(time_int S) const;
00292 
00294   //DETime NextIndexedStep(int S, int BS) const;
00295 
00297   DETime NextIndexedBigStep(time_int BS) const;
00298 
00300   DETime BeginOfTheNextUsefulStep(void) const;
00301 
00303   DETime ToNextStep() const;
00305   DETime ToNextStep(time_int S) const;
00306 
00308   DETime ToNextBigStep() const;
00310   DETime ToNextBigStep(time_int S) const;
00311 
00313   DETime ToNextSuperStep() const;
00315   DETime ToNextSuperStep(time_int S) const;
00316 
00318   DETime ToNextIndexedStep(time_int S) const;
00319 
00321   DETime ToNextIndexedBigStep(time_int BS) const;
00322 
00324   DETime ToBeginOfTheNextUsefulStep(void) const;
00325 
00327   bool HasOffsetZero() const;
00328 
00330   bool IsInOverFlow() const;
00331 
00333   friend ostream& operator<< ( ostream& os, const DETime& t );
00334 
00336   operator double() { return double(tick) * pow(10.0, TickSize); };
00337 
00338 };
00339 
00340 
00341 // Misc inline operators to override dangerous operations leading to wrong results due to automatic type conversions.
00342 inline DETime operator + (const time_int A, const DETime B) { return B+A; };
00343 inline DETime operator + (const double A, const DETime B) { return DETime(A)+B; };
00344 inline DETime operator - (const time_int A, const DETime B) { return DETime(A) - B; };
00345 inline DETime operator - (const double A, const DETime B) { return DETime(A) - B; };
00346 inline DETime operator * (const time_int A, const DETime B) { return B*A; };
00347 inline DETime operator * (const double A, const DETime B) { return B*A; };
00348 
00349 
00351 //
00352 
00358 
00360 
00372 class DETimeParameter : public GenericParameter
00373 {
00374  public:
00375   DETimeParameter(string aName, string aComment, string aDefault, string aValues)
00376     : GenericParameter(aName, aComment, aDefault, aValues)
00377   {};
00378 
00379   DETime get(string aClass, string aInstance)
00380   {
00381     return DETime(getValue(aClass, aInstance));
00382   };
00383 };
00384 
00386 
00387 #endif
00388 
00389 /*
00390 $Log: detime.h,v $
00391 Revision 1.3  2004/12/12 23:44:44  pecos
00392 Added Copyright message - LGPL
00393 
00394 Revision 1.2  2004/12/11 23:53:25  pecos
00395 Added DoxyGen comments
00396 
00397 Revision 1.1.1.1  2004/07/08 16:59:33  pecos
00398 NePSing framework
00399 
00400 Revision 1.4  2002/02/13 09:59:40  pecos
00401 *** empty log message ***
00402 
00403 Revision 1.3  2002/02/04 12:33:17  pecos
00404 Updated operators
00405 
00406 Revision 1.2  2001/03/16 20:22:50  pecos
00407 various bugs in ToNextXXX(int)
00408 
00409 new functions added:
00410 DETime ToNextBigStep() const;
00411 DETime ToNextBigStep(time_int S) const;
00412 DETime ToNextSuperStep() const;
00413 DETime ToNextSuperStep(time_int S) const;
00414 DETime ToNextIndexedBigStep(time_int BS) const;
00415 
00416 Revision 1.1.1.1  1999/05/24 15:59:57  inesis
00417 INeSiS Project
00418 
00419 Revision 1.1.1.1  1999/05/24 15:17:20  inesis
00420 INeSiS Project
00421 
00422 Revision 1.3  1999/02/05 11:57:20  pecos
00423 *** empty log message ***
00424 
00425 Revision 1.2  1999/01/26 11:24:23  pecos
00426 *** empty log message ***
00427 
00428 Revision 1.1  1999/01/02 09:09:22  nanni
00429 *** empty log message ***
00430 
00431 */

Generated on Wed Dec 22 23:23:47 2004 for NePSing by doxygen 1.3.9.1 ---- Hosted by SourceForge.net Logo